-
-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added QueryContext extension methods for Select and Include. (#8013)
- Loading branch information
1 parent
13c074d
commit b2dc87e
Showing
15 changed files
with
415 additions
and
53 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/GreenDonut/src/GreenDonut.Data/Extensions/GreenDonutQueryContextExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// ReSharper disable once CheckNamespace | ||
using System.Linq.Expressions; | ||
using GreenDonut.Data.Internal; | ||
|
||
namespace GreenDonut.Data; | ||
|
||
/// <summary> | ||
/// Provides extension methods for the <see cref="QueryContext{TEntity}"/>. | ||
/// </summary> | ||
public static class GreenDonutQueryContextExtensions | ||
{ | ||
public static QueryContext<TEntity> Include<TEntity, TValue>( | ||
this QueryContext<TEntity> context, | ||
Expression<Func<TEntity, TValue>>? propertySelector) | ||
{ | ||
ArgumentNullException.ThrowIfNull(context); | ||
|
||
if(propertySelector is null) | ||
{ | ||
return context; | ||
} | ||
|
||
var normalizedSelector = ExpressionHelpers.Rewrite(propertySelector); | ||
|
||
if(context.Selector is null) | ||
{ | ||
return context with | ||
{ | ||
Selector = normalizedSelector | ||
}; | ||
} | ||
else | ||
{ | ||
return context with | ||
{ | ||
Selector = ExpressionHelpers.Combine(context.Selector, normalizedSelector) | ||
}; | ||
} | ||
} | ||
|
||
public static QueryContext<TEntity> Select<TEntity>( | ||
this QueryContext<TEntity> context, | ||
Expression<Func<TEntity, TEntity>>? selector) | ||
8000 | { | |
ArgumentNullException.ThrowIfNull(context); | ||
|
||
if(selector is null) | ||
{ | ||
return context; | ||
} | ||
|
||
if(context.Selector is null) | ||
{ | ||
return context with | ||
{ | ||
Selector = selector | ||
}; | ||
} | ||
else | ||
{ | ||
return context with | ||
{ | ||
Selector = ExpressionHelpers.Combine(context.Selector, selector) | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/GreenDonut/test/GreenDonut.Data.EntityFramework.Tests/CapturePagingQueryInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace GreenDonut.Data; | ||
|
||
public sealed class CapturePagingQueryInterceptor : PagingQueryInterceptor | ||
{ | ||
public List<QueryInfo> Queries { get; } = new(); | ||
|
||
public override void OnBeforeExecute<T>(IQueryable<T> query) | ||
{ | ||
Queries.Add( | ||
new QueryInfo | ||
{ | ||
ExpressionText = query.Expression.ToString(), | ||
QueryText = query.ToQueryString() | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.