< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.GuidFilterViewModel<T>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/GuidFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 16
Coverable lines: 16
Total lines: 74
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%
set_Operator(...)100%210%
set_Value(...)100%210%
get_IsEmpty()100%210%
BuildExpressionCore()0%2040%
Reset()100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/GuidFilterViewModel.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="GuidFilterViewModel.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Linq.Expressions;
 9using MyNet.Primitives;
 10
 11namespace MyNet.UI.ViewModels.List.Filtering.Filters;
 12
 13/// <summary>
 14/// Filter view model for Guid properties.
 15/// Supports Is/IsNot operators for exact matching.
 16/// </summary>
 17/// <typeparam name="T">The type of the items being filtered.</typeparam>
 18/// <param name="propertyName">The key that identifies this filter condition.</param>
 19/// <param name="property">The expression representing the Guid property to filter on.</param>
 20/// <param name="operatorMode">The binary operator to use (default: Is).</param>
 21public class GuidFilterViewModel<T>(
 22    string propertyName,
 23    Expression<Func<T, Guid>> property,
 24    BinaryOperator operatorMode = BinaryOperator.Is)
 025    : FilterConditionViewModel<T>(propertyName)
 26{
 27    /// <summary>
 28    /// Gets the expression representing the Guid property to filter on.
 29    /// </summary>
 030    public Expression<Func<T, Guid>> Property { get; } = property;
 31
 32    /// <summary>
 33    /// Gets or sets the binary operator used for matching (Is or IsNot).
 34    /// </summary>
 035    public BinaryOperator Operator { get; set => SetProperty(ref field, value); } = operatorMode;
 36
 37    /// <summary>
 38    /// Gets or sets the Guid value to match against.
 39    /// When null, the filter is considered empty.
 40    /// </summary>
 41    public Guid? Value
 42    {
 43        get;
 044        set => SetProperty(ref field, value);
 45    }
 46
 47    /// <summary>
 48    /// Gets a value indicating whether this filter is in an empty state.
 49    /// </summary>
 050    public override bool IsEmpty => Value is null;
 51
 52    /// <inheritdoc />
 53    protected override Expression<Func<T, bool>> BuildExpressionCore()
 54    {
 055        var parameter = Property.Parameters[0];
 056        var propertyBody = Property.Body;
 057        var value = Expression.Constant(Value!.Value, typeof(Guid));
 58
 059        Expression body = Operator switch
 060        {
 061            BinaryOperator.Is => Expression.Equal(propertyBody, value),
 062            BinaryOperator.IsNot => Expression.NotEqual(propertyBody, value),
 063            _ => throw new NotSupportedException($"Operator {Operator} is not supported")
 064        };
 65
 066        return Expression.Lambda<Func<T, bool>>(body, parameter);
 67    }
 68
 69    /// <summary>
 70    /// Resets the filter to its default state by clearing the value.
 71    /// </summary>
 072    public override void Reset() => Value = null;
 73}
 74