< Summary

Information
Class: MyNet.UI.ViewModels.Crud.ChildItemViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/ChildItemViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 13
Coverable lines: 13
Total lines: 58
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Attach(...)100%210%
Detach()0%620%
OnParentItemChanged(...)100%210%
DisposeManagedResources()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/Crud/ChildItemViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ChildItemViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Diagnostics.CodeAnalysis;
 9using MyNet.UI.Commands;
 10
 11namespace MyNet.UI.ViewModels.Crud;
 12
 13/// <summary>
 14/// Item view model that follows a parent <see cref="IItemViewModel{T}"/> and updates when the parent's item changes.
 15/// </summary>
 16/// <typeparam name="T">The item type.</typeparam>
 17/// <remarks>
 18/// Initializes a new instance of the <see cref="ChildItemViewModel{T}"/> class.
 19/// </remarks>
 020public abstract class ChildItemViewModel<T>(ICommandFactory? commandFactory = null) : ItemViewModel<T>(commandFactory)
 21{
 22    [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Disposed in Cleanup.")]
 23    private IDisposable? _parentSubscription;
 24
 25    /// <summary>
 26    /// Attaches to a parent item view model.
 27    /// </summary>
 28    public virtual void Attach(IItemViewModel<T> parent)
 29    {
 030        ArgumentNullException.ThrowIfNull(parent);
 31
 032        Detach();
 033        SetItem(parent.Item);
 034        _parentSubscription = parent.ObserveItemChanged(OnParentItemChanged);
 035    }
 36
 37    /// <summary>
 38    /// Detaches from the parent item view model.
 39    /// </summary>
 40    public virtual void Detach()
 41    {
 042        _parentSubscription?.Dispose();
 043        _parentSubscription = null;
 044    }
 45
 46    /// <summary>
 47    /// Called when the parent's item changes.
 48    /// </summary>
 049    protected virtual void OnParentItemChanged(T? item) => SetItem(item);
 50
 51    /// <inheritdoc />
 52    protected override void DisposeManagedResources()
 53    {
 054        Detach();
 055        base.DisposeManagedResources();
 056    }
 57}
 58