Skip to content

Commit db5849c

Browse files
authored
Simplify and minimize expression containers. (#448)
1 parent c74af2c commit db5849c

File tree

6 files changed

+14
-82
lines changed

6 files changed

+14
-82
lines changed

src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public static IQueryable<T> Include<T>(this IQueryable<T> source, IncludeExpress
1616
public static IQueryable<T> ThenInclude<T>(this IQueryable<T> source, IncludeExpressionInfo info)
1717
{
1818
_ = info ?? throw new ArgumentNullException(nameof(info));
19-
_ = info.PreviousPropertyType ?? throw new ArgumentNullException(nameof(info.PreviousPropertyType));
2019

2120
var exp = source.Expression as MethodCallExpression;
2221
var arg = exp.Arguments[0] as ConstantExpression;
@@ -47,7 +46,7 @@ public static IQueryable<T> ThenInclude<T>(this IQueryable<T> source, IncludeExp
4746
return QueryableExtensions.Include(source, $"{previousPropertyName}.{propertyName}");
4847
}
4948

50-
private static string GetPropertyName(this Expression propertySelector, char delimiter = '.', char endTrim = ')')
49+
private static string GetPropertyName(this LambdaExpression propertySelector, char delimiter = '.', char endTrim = ')')
5150
{
5251

5352
var asString = propertySelector.ToString();

src/Ardalis.Specification/Builders/Builder_Include.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static IIncludableSpecificationBuilder<T, TResult, TProperty> Include<T,
101101
{
102102
if (condition)
103103
{
104-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(T), typeof(TProperty));
104+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.Include);
105105
builder.Specification.Add(expr);
106106
}
107107

@@ -139,7 +139,7 @@ public static IIncludableSpecificationBuilder<T, TProperty> Include<T, TProperty
139139
{
140140
if (condition)
141141
{
142-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(T), typeof(TProperty));
142+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.Include);
143143
builder.Specification.Add(expr);
144144
}
145145

@@ -183,7 +183,7 @@ public static IIncludableSpecificationBuilder<TEntity, TResult, TProperty> ThenI
183183
{
184184
if (condition && !Specification<TEntity, TResult>.IsChainDiscarded)
185185
{
186-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty));
186+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude);
187187
builder.Specification.Add(expr);
188188
}
189189
else
@@ -228,7 +228,7 @@ public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TE
228228
{
229229
if (condition && !Specification<TEntity>.IsChainDiscarded)
230230
{
231-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(TPreviousProperty));
231+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude);
232232
builder.Specification.Add(expr);
233233
}
234234
else
@@ -275,7 +275,7 @@ public static IIncludableSpecificationBuilder<TEntity, TResult, TProperty> ThenI
275275
{
276276
if (condition && !Specification<TEntity, TResult>.IsChainDiscarded)
277277
{
278-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(IEnumerable<TPreviousProperty>));
278+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude);
279279
builder.Specification.Add(expr);
280280
}
281281
else
@@ -320,7 +320,7 @@ public static IIncludableSpecificationBuilder<TEntity, TProperty> ThenInclude<TE
320320
{
321321
if (condition && !Specification<TEntity>.IsChainDiscarded)
322322
{
323-
var expr = new IncludeExpressionInfo(navigationSelector, typeof(TEntity), typeof(TProperty), typeof(IEnumerable<TPreviousProperty>));
323+
var expr = new IncludeExpressionInfo(navigationSelector, IncludeTypeEnum.ThenInclude);
324324
builder.Specification.Add(expr);
325325
}
326326
else

src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,16 @@ public class IncludeExpressionInfo
1111
/// </summary>
1212
public LambdaExpression LambdaExpression { get; }
1313

14-
/// <summary>
15-
/// The type of the source entity.
16-
/// </summary>
17-
public Type EntityType { get; }
18-
19-
/// <summary>
20-
/// The type of the included entity.
21-
/// </summary>
22-
public Type PropertyType { get; }
23-
24-
/// <summary>
25-
/// The type of the previously included entity.
26-
/// </summary>
27-
public Type? PreviousPropertyType { get; }
28-
2914
/// <summary>
3015
/// The include type.
3116
/// </summary>
3217
public IncludeTypeEnum Type { get; }
3318

34-
private IncludeExpressionInfo(LambdaExpression expression,
35-
Type entityType,
36-
Type propertyType,
37-
Type? previousPropertyType,
38-
IncludeTypeEnum includeType)
39-
19+
public IncludeExpressionInfo(LambdaExpression expression, IncludeTypeEnum includeType)
4020
{
4121
_ = expression ?? throw new ArgumentNullException(nameof(expression));
42-
_ = entityType ?? throw new ArgumentNullException(nameof(entityType));
43-
_ = propertyType ?? throw new ArgumentNullException(nameof(propertyType));
44-
45-
if (includeType == IncludeTypeEnum.ThenInclude)
46-
{
47-
_ = previousPropertyType ?? throw new ArgumentNullException(nameof(previousPropertyType));
48-
}
4922

5023
LambdaExpression = expression;
51-
EntityType = entityType;
52-
PropertyType = propertyType;
53-
PreviousPropertyType = previousPropertyType;
5424
Type = includeType;
5525
}
56-
57-
/// <summary>
58-
/// Creates instance of <see cref="IncludeExpressionInfo" /> which describes 'Include' query part.<para />
59-
/// Source (entityType) -> Include (propertyType).
60-
/// </summary>
61-
/// <param name="expression">The expression represents a related entity that should be included.</param>
62-
/// <param name="entityType">The type of the source entity.</param>
63-
/// <param name="propertyType">The type of the included entity.</param>
64-
public IncludeExpressionInfo(LambdaExpression expression,
65-
Type entityType,
66-
Type propertyType)
67-
: this(expression, entityType, propertyType, null, IncludeTypeEnum.Include)
68-
{
69-
}
70-
71-
/// <summary>
72-
/// Creates instance of <see cref="IncludeExpressionInfo" /> which describes 'ThenInclude' query part.<para />
73-
/// Source (entityType) -> Include (previousPropertyType) -> ThenInclude (propertyType).
74-
/// </summary>
75-
/// <param name="expression">The expression represents a related entity that should be included as part of the previously included entity.</param>
76-
/// <param name="entityType">The type of the source entity.</param>
77-
/// <param name="propertyType">The type of the included entity.</param>
78-
/// <param name="previousPropertyType">The type of the previously included entity.</param>
79-
public IncludeExpressionInfo(LambdaExpression expression,
80-
Type entityType,
81-
Type propertyType,
82-
Type previousPropertyType)
83-
: this(expression, entityType, propertyType, previousPropertyType, IncludeTypeEnum.ThenInclude)
84-
{
85-
}
8626
}

src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <typeparam name="T">Type of the entity to apply sort on.</typeparam>
77
public class OrderExpressionInfo<T>
88
{
9-
private readonly Lazy<Func<T, object?>> _keySelectorFunc;
9+
private Func<T, object?>? _keySelectorFunc;
1010

1111
/// <summary>
1212
/// Creates instance of <see cref="OrderExpressionInfo{T}" />.
@@ -20,8 +20,6 @@ public OrderExpressionInfo(Expression<Func<T, object?>> keySelector, OrderTypeEn
2020

2121
KeySelector = keySelector;
2222
OrderType = orderType;
23-
24-
_keySelectorFunc = new Lazy<Func<T, object?>>(KeySelector.Compile);
2523
}
2624

2725
/// <summary>
@@ -37,5 +35,5 @@ public OrderExpressionInfo(Expression<Func<T, object?>> keySelector, OrderTypeEn
3735
/// <summary>
3836
/// Compiled <see cref="KeySelector" />.
3937
/// </summary>
40-
public Func<T, object?> KeySelectorFunc => _keySelectorFunc.Value;
38+
public Func<T, object?> KeySelectorFunc => _keySelectorFunc ??= KeySelector.Compile();
4139
}

src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <typeparam name="T">Type of the source from which search target should be selected.</typeparam>
77
public class SearchExpressionInfo<T>
88
{
9-
private readonly Lazy<Func<T, string?>> _selectorFunc;
9+
private Func<T, string?>? _selectorFunc;
1010

1111
/// <summary>
1212
/// Creates instance of <see cref="SearchExpressionInfo{T}" />.
@@ -24,8 +24,6 @@ public SearchExpressionInfo(Expression<Func<T, string?>> selector, string search
2424
Selector = selector;
2525
SearchTerm = searchTerm;
2626
SearchGroup = searchGroup;
27-
28-
_selectorFunc = new Lazy<Func<T, string?>>(Selector.Compile);
2927
}
3028

3129
/// <summary>
@@ -46,5 +44,5 @@ public SearchExpressionInfo(Expression<Func<T, string?>> selector, string search
4644
/// <summary>
4745
/// Compiled <see cref="Selector" />.
4846
/// </summary>
49-
public Func<T, string?> SelectorFunc => _selectorFunc.Value;
47+
public Func<T, string?> SelectorFunc => _selectorFunc ??= Selector.Compile();
5048
}

src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <typeparam name="T">Type of the entity to apply filter on.</typeparam>
77
public class WhereExpressionInfo<T>
88
{
9-
private readonly Lazy<Func<T, bool>> _filterFunc;
9+
private Func<T, bool>? _filterFunc;
1010

1111
/// <summary>
1212
/// Creates instance of <see cref="WhereExpressionInfo{T}" />.
@@ -16,10 +16,7 @@ public class WhereExpressionInfo<T>
1616
public WhereExpressionInfo(Expression<Func<T, bool>> filter)
1717
{
1818
_ = filter ?? throw new ArgumentNullException(nameof(filter));
19-
2019
Filter = filter;
21-
22-
_filterFunc = new Lazy<Func<T, bool>>(Filter.Compile);
2320
}
2421

2522
/// <summary>
@@ -30,5 +27,5 @@ public WhereExpressionInfo(Expression<Func<T, bool>> filter)
3027
/// <summary>
3128
/// Compiled <see cref="Filter" />.
3229
/// </summary>
33-
public Func<T, bool> FilterFunc => _filterFunc.Value;
30+
public Func<T, bool> FilterFunc => _filterFunc ??= Filter.Compile();
3431
}

0 commit comments

Comments
 (0)