-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
015632f
commit 115dacd
Showing
9 changed files
with
354 additions
and
273 deletions.
There are no files selected for viewing
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
120 changes: 120 additions & 0 deletions
120
src/Cuture.EntityFramework.DynamicFilter/EntityDynamicFilterBuilder.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,120 @@ | ||
using System.Linq.Expressions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Extensions; | ||
|
||
/// <summary> | ||
/// <typeparamref name="TEntity"/> 的动态过滤器构建器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
public class EntityDynamicFilterBuilder<TEntity> | ||
{ | ||
#region Private 字段 | ||
|
||
private readonly EntityDynamicQueryFilterMetadata _metadata; | ||
|
||
#endregion Private 字段 | ||
|
||
#region Public 构造函数 | ||
|
||
/// <inheritdoc cref="EntityDynamicFilterBuilder{TEntity}"/> | ||
public EntityDynamicFilterBuilder(EntityDynamicQueryFilterMetadata metadata) | ||
{ | ||
_metadata = metadata ?? throw new ArgumentNullException(nameof(metadata)); | ||
} | ||
|
||
#endregion Public 构造函数 | ||
|
||
#region DelegationFilter | ||
|
||
internal EntityDynamicFilterBuilder<TEntity> AddFilter(Func<IServiceProvider, IDynamicQueryFilter> queryFilterFactory) | ||
{ | ||
_metadata.DynamicQueryFilterFactories.Add(queryFilterFactory); | ||
return this; | ||
} | ||
|
||
#endregion DelegationFilter | ||
|
||
#region InstanceFilter | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加过滤器 <paramref name="queryFilter"/> | ||
/// </summary> | ||
/// <param name="queryFilter"></param> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter(IDynamicQueryFilter<TEntity> queryFilter) | ||
{ | ||
return AddFilter(_ => queryFilter); | ||
} | ||
|
||
#endregion InstanceFilter | ||
|
||
#region TypeFilter | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加过滤器 <typeparamref name="TQueryFilter"/> | ||
/// </summary> | ||
/// <typeparam name="TQueryFilter"></typeparam> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter<TQueryFilter>() where TQueryFilter : IDynamicQueryFilter<TEntity> | ||
{ | ||
return AddFilter(static serviceProvider => serviceProvider.GetRequiredService<TQueryFilter>()); | ||
} | ||
|
||
#endregion TypeFilter | ||
|
||
#region ExpressionFilter | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的过滤器 | ||
/// </summary> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <param name="filterPlace"></param> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter(Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder, DynamicQueryFilterPlace filterPlace = DynamicQueryFilterPlace.Default) | ||
{ | ||
return AddFilter(null!, predicate, order, filterPlace); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的过滤器 | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <param name="filterPlace"></param> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter(string name, Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder, DynamicQueryFilterPlace filterPlace = DynamicQueryFilterPlace.Default) | ||
{ | ||
var queryFilter = new ExpressionDynamicQueryFilter<TEntity>(name, order, filterPlace, predicate); | ||
return AddFilter(_ => queryFilter); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的过滤器 | ||
/// </summary> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <param name="filterPlace"></param> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter(Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder, DynamicQueryFilterPlace filterPlace = DynamicQueryFilterPlace.Default) | ||
{ | ||
return AddFilter(null!, predicateFactory, order, filterPlace); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的过滤器 | ||
/// </summary> | ||
/// <param name="name"></param> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <param name="filterPlace"></param> | ||
/// <returns></returns> | ||
public EntityDynamicFilterBuilder<TEntity> AddFilter(string name, Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder, DynamicQueryFilterPlace filterPlace = DynamicQueryFilterPlace.Default) | ||
{ | ||
return AddFilter(serviceProvider => new ExpressionDynamicQueryFilter<TEntity>(name, order, filterPlace, predicateFactory(serviceProvider))); | ||
} | ||
|
||
#endregion ExpressionFilter | ||
} |
131 changes: 131 additions & 0 deletions
131
src/Cuture.EntityFramework.DynamicFilter/EntityDynamicFilterBuilderExtensions.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,131 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Extensions; | ||
|
||
/// <summary> | ||
/// EntityDynamicFilterBuilder 拓展 | ||
/// </summary> | ||
public static class EntityDynamicFilterBuilderExtensions | ||
{ | ||
#region ExpressionFilter | ||
|
||
#region Head | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的头部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddHeadFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(null!, predicate, order, DynamicQueryFilterPlace.Head); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的头部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="name"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddHeadFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, string name, Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
var queryFilter = new ExpressionDynamicQueryFilter<TEntity>(name, order, DynamicQueryFilterPlace.Head, predicate); | ||
return builder.AddFilter(_ => queryFilter); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的头部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddHeadFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(null!, predicateFactory, order, DynamicQueryFilterPlace.Head); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的头部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="name"></param> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddHeadFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, string name, Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(serviceProvider => new ExpressionDynamicQueryFilter<TEntity>(name, order, DynamicQueryFilterPlace.Head, predicateFactory(serviceProvider))); | ||
} | ||
|
||
#endregion Head | ||
|
||
#region Tail | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的尾部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddTailFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(null!, predicate, order, DynamicQueryFilterPlace.Tail); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加基于表达式 <paramref name="predicate"/> 的尾部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="name"></param> | ||
/// <param name="predicate"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddTailFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, string name, Expression<Func<TEntity, bool>> predicate, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
var queryFilter = new ExpressionDynamicQueryFilter<TEntity>(name, order, DynamicQueryFilterPlace.Tail, predicate); | ||
return builder.AddFilter(_ => queryFilter); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的尾部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddTailFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(null!, predicateFactory, order, DynamicQueryFilterPlace.Tail); | ||
} | ||
|
||
/// <summary> | ||
/// 为实体 <typeparamref name="TEntity"/> 添加从 <paramref name="predicateFactory"/> 获取表达式的尾部过滤器 | ||
/// </summary> | ||
/// <typeparam name="TEntity"></typeparam> | ||
/// <param name="builder"></param> | ||
/// <param name="name"></param> | ||
/// <param name="predicateFactory"></param> | ||
/// <param name="order"></param> | ||
/// <returns></returns> | ||
public static EntityDynamicFilterBuilder<TEntity> AddTailFilter<TEntity>(this EntityDynamicFilterBuilder<TEntity> builder, string name, Func<IServiceProvider, Expression<Func<TEntity, bool>>> predicateFactory, int order = IDynamicQueryFilter.DefaultOrder) | ||
{ | ||
return builder.AddFilter(serviceProvider => new ExpressionDynamicQueryFilter<TEntity>(name, order, DynamicQueryFilterPlace.Tail, predicateFactory(serviceProvider))); | ||
} | ||
|
||
#endregion Tail | ||
|
||
#endregion ExpressionFilter | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Cuture.EntityFramework.DynamicFilter/EntityDynamicQueryFilterMetadata.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,31 @@ | ||
namespace Microsoft.EntityFrameworkCore.Extensions; | ||
|
||
/// <summary> | ||
/// 实体的动态过滤器元数据 | ||
/// </summary> | ||
public class EntityDynamicQueryFilterMetadata | ||
{ | ||
#region Public 属性 | ||
|
||
/// <summary> | ||
/// 动态过滤器工厂列表 | ||
/// </summary> | ||
public List<Func<IServiceProvider, IDynamicQueryFilter>> DynamicQueryFilterFactories { get; } = []; | ||
|
||
/// <summary> | ||
/// 目标类型 | ||
/// </summary> | ||
public Type Type { get; set; } | ||
|
||
#endregion Public 属性 | ||
|
||
#region Public 构造函数 | ||
|
||
/// <inheritdoc cref="EntityDynamicQueryFilterMetadata"/> | ||
public EntityDynamicQueryFilterMetadata(Type type) | ||
{ | ||
Type = type ?? throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
#endregion Public 构造函数 | ||
} |
Oops, something went wrong.