From be0d8a3f7524ffb700eebef03907caa078586ecf Mon Sep 17 00:00:00 2001 From: Fati Iseni Date: Sun, 9 Mar 2025 13:58:35 +0100 Subject: [PATCH] Simplify and minimize expression containers. --- .../Extensions/IncludeExtensions.cs | 3 +- .../Builders/Builder_Include.cs | 12 ++-- .../Expressions/IncludeExpressionInfo.cs | 62 +------------------ .../Expressions/OrderExpressionInfo.cs | 6 +- .../Expressions/SearchExpressionInfo.cs | 6 +- .../Expressions/WhereExpressionInfo.cs | 7 +-- 6 files changed, 14 insertions(+), 82 deletions(-) diff --git a/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs b/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs index 50970aa2..b22a45d4 100644 --- a/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs +++ b/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs @@ -16,7 +16,6 @@ public static IQueryable Include(this IQueryable source, IncludeExpress public static IQueryable ThenInclude(this IQueryable source, IncludeExpressionInfo info) { _ = info ?? throw new ArgumentNullException(nameof(info)); - _ = info.PreviousPropertyType ?? throw new ArgumentNullException(nameof(info.PreviousPropertyType)); var exp = source.Expression as MethodCallExpression; var arg = exp.Arguments[0] as ConstantExpression; @@ -47,7 +46,7 @@ public static IQueryable ThenInclude(this IQueryable source, IncludeExp return QueryableExtensions.Include(source, $"{previousPropertyName}.{propertyName}"); } - private static string GetPropertyName(this Expression propertySelector, char delimiter = '.', char endTrim = ')') + private static string GetPropertyName(this LambdaExpression propertySelector, char delimiter = '.', char endTrim = ')') { var asString = propertySelector.ToString(); diff --git a/src/Ardalis.Specification/Builders/Builder_Include.cs b/src/Ardalis.Specification/Builders/Builder_Include.cs index 945d920f..f31087b9 100644 --- a/src/Ardalis.Specification/Builders/Builder_Include.cs +++ b/src/Ardalis.Specification/Builders/Builder_Include.cs @@ -101,7 +101,7 @@ public static IIncludableSpecificationBuilder Include Include ThenI { if (condition && !Specification.IsChainDiscarded) { - var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty)); + var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude); builder.Specification.Add(expr); } else @@ -228,7 +228,7 @@ public static IIncludableSpecificationBuilder ThenInclude.IsChainDiscarded) { - var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty)); + var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude); builder.Specification.Add(expr); } else @@ -275,7 +275,7 @@ public static IIncludableSpecificationBuilder ThenI { if (condition && !Specification.IsChainDiscarded) { - var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(IEnumerable)); + var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude); builder.Specification.Add(expr); } else @@ -320,7 +320,7 @@ public static IIncludableSpecificationBuilder ThenInclude.IsChainDiscarded) { - var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(IEnumerable)); + var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude); builder.Specification.Add(expr); } else diff --git a/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs b/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs index 4867a8ed..0a5db08b 100644 --- a/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs +++ b/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs @@ -11,76 +11,16 @@ public class IncludeExpressionInfo /// public LambdaExpression LambdaExpression { get; } - /// - /// The type of the source entity. - /// - public Type EntityType { get; } - - /// - /// The type of the included entity. - /// - public Type PropertyType { get; } - - /// - /// The type of the previously included entity. - /// - public Type? PreviousPropertyType { get; } - /// /// The include type. /// public IncludeTypeEnum Type { get; } - private IncludeExpressionInfo(LambdaExpression expression, - Type entityType, - Type propertyType, - Type? previousPropertyType, - IncludeTypeEnum includeType) - + public IncludeExpressionInfo(LambdaExpression expression, IncludeTypeEnum includeType) { _ = expression ?? throw new ArgumentNullException(nameof(expression)); - _ = entityType ?? throw new ArgumentNullException(nameof(entityType)); - _ = propertyType ?? throw new ArgumentNullException(nameof(propertyType)); - - if (includeType == IncludeTypeEnum.ThenInclude) - { - _ = previousPropertyType ?? throw new ArgumentNullException(nameof(previousPropertyType)); - } LambdaExpression = expression; - EntityType = entityType; - PropertyType = propertyType; - PreviousPropertyType = previousPropertyType; Type = includeType; } - - /// - /// Creates instance of which describes 'Include' query part. - /// Source (entityType) -> Include (propertyType). - /// - /// The expression represents a related entity that should be included. - /// The type of the source entity. - /// The type of the included entity. - public IncludeExpressionInfo(LambdaExpression expression, - Type entityType, - Type propertyType) - : this(expression, entityType, propertyType, null, IncludeTypeEnum.Include) - { - } - - /// - /// Creates instance of which describes 'ThenInclude' query part. - /// Source (entityType) -> Include (previousPropertyType) -> ThenInclude (propertyType). - /// - /// The expression represents a related entity that should be included as part of the previously included entity. - /// The type of the source entity. - /// The type of the included entity. - /// The type of the previously included entity. - public IncludeExpressionInfo(LambdaExpression expression, - Type entityType, - Type propertyType, - Type previousPropertyType) - : this(expression, entityType, propertyType, previousPropertyType, IncludeTypeEnum.ThenInclude) - { - } } diff --git a/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs b/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs index e1e1acf8..bf1c1828 100644 --- a/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs +++ b/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs @@ -6,7 +6,7 @@ /// Type of the entity to apply sort on. public class OrderExpressionInfo { - private readonly Lazy> _keySelectorFunc; + private Func? _keySelectorFunc; /// /// Creates instance of . @@ -20,8 +20,6 @@ public OrderExpressionInfo(Expression> keySelector, OrderTypeEn KeySelector = keySelector; OrderType = orderType; - - _keySelectorFunc = new Lazy>(KeySelector.Compile); } /// @@ -37,5 +35,5 @@ public OrderExpressionInfo(Expression> keySelector, OrderTypeEn /// /// Compiled . /// - public Func KeySelectorFunc => _keySelectorFunc.Value; + public Func KeySelectorFunc => _keySelectorFunc ??= KeySelector.Compile(); } diff --git a/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs b/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs index 45b5b208..f83392d3 100644 --- a/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs +++ b/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs @@ -6,7 +6,7 @@ /// Type of the source from which search target should be selected. public class SearchExpressionInfo { - private readonly Lazy> _selectorFunc; + private Func? _selectorFunc; /// /// Creates instance of . @@ -24,8 +24,6 @@ public SearchExpressionInfo(Expression> selector, string search Selector = selector; SearchTerm = searchTerm; SearchGroup = searchGroup; - - _selectorFunc = new Lazy>(Selector.Compile); } /// @@ -46,5 +44,5 @@ public SearchExpressionInfo(Expression> selector, string search /// /// Compiled . /// - public Func SelectorFunc => _selectorFunc.Value; + public Func SelectorFunc => _selectorFunc ??= Selector.Compile(); } diff --git a/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs b/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs index 4b10428d..c22132aa 100644 --- a/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs +++ b/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs @@ -6,7 +6,7 @@ /// Type of the entity to apply filter on. public class WhereExpressionInfo { - private readonly Lazy> _filterFunc; + private Func? _filterFunc; /// /// Creates instance of . @@ -16,10 +16,7 @@ public class WhereExpressionInfo public WhereExpressionInfo(Expression> filter) { _ = filter ?? throw new ArgumentNullException(nameof(filter)); - Filter = filter; - - _filterFunc = new Lazy>(Filter.Compile); } /// @@ -30,5 +27,5 @@ public WhereExpressionInfo(Expression> filter) /// /// Compiled . /// - public Func FilterFunc => _filterFunc.Value; + public Func FilterFunc => _filterFunc ??= Filter.Compile(); }