< Summary

Information
Class: MyNet.UI.ViewModels.List.Filtering.Filters.ComparableFilterViewModel<T1, T2>
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/ViewModels/List/Filtering/Filters/ComparableFilterViewModel.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 46
Coverable lines: 46
Total lines: 118
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 35
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_From(...)100%210%
set_To(...)100%210%
set_Minimum(...)100%210%
set_Maximum(...)100%210%
get_IsEmpty()0%4260%
BuildExpressionCore()0%870290%
Reset()100%210%

File(s)

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

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ComparableFilterViewModel.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 comparable (numeric, date, etc.) properties.
 15/// Supports range filtering (from/to) and various comparison operators including IsBetween.
 16/// </summary>
 17/// <typeparam name="T">The type of the items being filtered.</typeparam>
 18/// <typeparam name="TValue">The type of the comparable value (must be a struct implementing IComparable).</typeparam>
 19/// <param name="propertyName">The key that identifies this filter condition.</param>
 20/// <param name="property">The expression representing the comparable property to filter on.</param>
 21/// <param name="comparableOperator">The comparison operator to use (default: EqualsTo).</param>
 22public class ComparableFilterViewModel<T, TValue>(
 23    string propertyName,
 24    Expression<Func<T, TValue>> property,
 25    ComplexComparableOperator comparableOperator = ComplexComparableOperator.EqualsTo)
 026    : FilterConditionViewModel<T>(propertyName)
 27    where TValue : struct, IComparable<TValue>
 28{
 29    /// <summary>
 30    /// Gets the expression representing the comparable property to filter on.
 31    /// </summary>
 032    public Expression<Func<T, TValue>> Property { get; } = property;
 33
 34    /// <summary>
 35    /// Gets or sets the comparison operator used for matching.
 36    /// </summary>
 037    public ComplexComparableOperator Operator { get; set => SetProperty(ref field, value); } = comparableOperator;
 38
 39    /// <summary>
 40    /// Gets or sets the lower bound value for comparison.
 41    /// Used as the primary value for single-value operators (EqualsTo, LessThan, etc.).
 42    /// </summary>
 043    public TValue? From { get; set => SetProperty(ref field, value); }
 44
 45    /// <summary>
 46    /// Gets or sets the upper bound value for comparison.
 47    /// Used together with <see cref="From"/> for range operators (IsBetween, IsNotBetween).
 48    /// </summary>
 049    public TValue? To { get; set => SetProperty(ref field, value); }
 50
 51    /// <summary>
 52    /// Gets or sets the minimum allowed value for the filter range.
 53    /// </summary>
 054    public TValue? Minimum { get; set => SetProperty(ref field, value); }
 55
 56    /// <summary>
 57    /// Gets or sets the maximum allowed value for the filter range.
 58    /// </summary>
 059    public TValue? Maximum { get; set => SetProperty(ref field, value); }
 60
 61    /// <summary>
 62    /// Gets a value indicating whether this filter is in an empty state.
 63    /// A comparable filter is empty when both From and To are null or equal to default.
 64    /// </summary>
 065    public override bool IsEmpty => (From is null || From.Equals(default(TValue))) && (To is null || To.Equals(default(T
 66
 67    /// <inheritdoc />
 68    protected override Expression<Func<T, bool>> BuildExpressionCore()
 69    {
 070        var parameter = Property.Parameters[0];
 071        var propertyBody = Property.Body;
 72
 073        Expression body = Operator switch
 074        {
 075            ComplexComparableOperator.EqualsTo when From is not null =>
 076                Expression.Equal(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 077
 078            ComplexComparableOperator.NotEqualsTo when From is not null =>
 079                Expression.NotEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 080
 081            ComplexComparableOperator.LessThan when To is not null =>
 082                Expression.LessThan(propertyBody, Expression.Constant(To.Value, typeof(TValue))),
 083
 084            ComplexComparableOperator.GreaterThan when From is not null =>
 085                Expression.GreaterThan(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 086
 087            ComplexComparableOperator.LessEqualThan when To is not null =>
 088                Expression.LessThanOrEqual(propertyBody, Expression.Constant(To.Value, typeof(TValue))),
 089
 090            ComplexComparableOperator.GreaterEqualThan when From is not null =>
 091                Expression.GreaterThanOrEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 092
 093            ComplexComparableOperator.IsBetween when From is not null && To is not null =>
 094                Expression.AndAlso(
 095                    Expression.GreaterThanOrEqual(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 096                    Expression.LessThanOrEqual(propertyBody, Expression.Constant(To.Value, typeof(TValue)))),
 097
 098            ComplexComparableOperator.IsNotBetween when From is not null && To is not null =>
 099                Expression.OrElse(
 0100                    Expression.LessThan(propertyBody, Expression.Constant(From.Value, typeof(TValue))),
 0101                    Expression.GreaterThan(propertyBody, Expression.Constant(To.Value, typeof(TValue)))),
 0102
 0103            _ => Expression.Constant(true)
 0104        };
 105
 0106        return Expression.Lambda<Func<T, bool>>(body, parameter);
 107    }
 108
 109    /// <summary>
 110    /// Resets the filter to its default state by clearing From and To values.
 111    /// </summary>
 112    public override void Reset()
 113    {
 0114        From = null;
 0115        To = null;
 0116    }
 117}
 118