Resolve Query Interceptor and queryable Interceptor provider :)
This commit is contained in:
@@ -14,6 +14,10 @@ namespace PoweredSoft.DynamicQuery
|
||||
{
|
||||
public class QueryHandler : QueryHandlerBase, IQueryHandler
|
||||
{
|
||||
public QueryHandler(IEnumerable<IQueryInterceptorProvider> queryableInterceptorProviders) : base(queryableInterceptorProviders)
|
||||
{
|
||||
}
|
||||
|
||||
protected virtual IQueryExecutionResult<TRecord> FinalExecute<TSource, TRecord>()
|
||||
{
|
||||
CommonBeforeExecute<TSource>();
|
||||
@@ -127,25 +131,25 @@ namespace PoweredSoft.DynamicQuery
|
||||
|
||||
public IQueryExecutionResult<TSource> Execute<TSource>(IQueryable<TSource> queryable, IQueryCriteria criteria)
|
||||
{
|
||||
Reset(queryable, criteria, new QueryExecutionOptions());
|
||||
Reset<TSource, TSource>(queryable, criteria, new QueryExecutionOptions());
|
||||
return FinalExecute<TSource, TSource>();
|
||||
}
|
||||
|
||||
public IQueryExecutionResult<TRecord> Execute<TSource, TRecord>(IQueryable<TSource> queryable, IQueryCriteria criteria)
|
||||
{
|
||||
Reset(queryable, criteria, new QueryExecutionOptions());
|
||||
Reset<TSource, TRecord>(queryable, criteria, new QueryExecutionOptions());
|
||||
return FinalExecute<TSource, TRecord>();
|
||||
}
|
||||
|
||||
public IQueryExecutionResult<TSource> Execute<TSource>(IQueryable<TSource> queryable, IQueryCriteria criteria, IQueryExecutionOptions options)
|
||||
{
|
||||
Reset(queryable, criteria, options);
|
||||
Reset<TSource, TSource>(queryable, criteria, options);
|
||||
return FinalExecute<TSource, TSource>();
|
||||
}
|
||||
|
||||
public IQueryExecutionResult<TRecord> Execute<TSource, TRecord>(IQueryable<TSource> queryable, IQueryCriteria criteria, IQueryExecutionOptions options)
|
||||
{
|
||||
Reset(queryable, criteria, options);
|
||||
Reset<TSource, TRecord>(queryable, criteria, options);
|
||||
return FinalExecute<TSource, TRecord>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace PoweredSoft.DynamicQuery
|
||||
{
|
||||
public IAsyncQueryableService AsyncQueryableService { get; }
|
||||
|
||||
public QueryHandlerAsync(IAsyncQueryableService asyncQueryableService)
|
||||
public QueryHandlerAsync(IAsyncQueryableService asyncQueryableService, IEnumerable<IQueryInterceptorProvider> queryInterceptorProviders) : base(queryInterceptorProviders)
|
||||
{
|
||||
AsyncQueryableService = asyncQueryableService;
|
||||
}
|
||||
@@ -157,25 +157,25 @@ namespace PoweredSoft.DynamicQuery
|
||||
|
||||
public Task<IQueryExecutionResult<TSource>> ExecuteAsync<TSource>(IQueryable<TSource> queryable, IQueryCriteria criteria, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Reset(queryable, criteria, new QueryExecutionOptions());
|
||||
Reset<TSource, TSource>(queryable, criteria, new QueryExecutionOptions());
|
||||
return FinalExecuteAsync<TSource, TSource>(cancellationToken);
|
||||
}
|
||||
|
||||
public Task<IQueryExecutionResult<TRecord>> ExecuteAsync<TSource, TRecord>(IQueryable<TSource> queryable, IQueryCriteria criteria, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Reset(queryable, criteria, new QueryExecutionOptions());
|
||||
Reset<TSource, TRecord>(queryable, criteria, new QueryExecutionOptions());
|
||||
return FinalExecuteAsync<TSource, TRecord>(cancellationToken);
|
||||
}
|
||||
|
||||
public Task<IQueryExecutionResult<TSource>> ExecuteAsync<TSource>(IQueryable<TSource> queryable, IQueryCriteria criteria, IQueryExecutionOptions options, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Reset(queryable, criteria, options);
|
||||
Reset<TSource, TSource>(queryable, criteria, options);
|
||||
return FinalExecuteAsync<TSource, TSource>(cancellationToken);
|
||||
}
|
||||
|
||||
public Task<IQueryExecutionResult<TRecord>> ExecuteAsync<TSource, TRecord>(IQueryable<TSource> queryable, IQueryCriteria criteria, IQueryExecutionOptions options, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Reset(queryable, criteria, options);
|
||||
Reset<TSource, TRecord>(queryable, criteria, options);
|
||||
return FinalExecuteAsync<TSource, TRecord>(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,9 @@ namespace PoweredSoft.DynamicQuery
|
||||
{
|
||||
public abstract class QueryHandlerBase : IInterceptableQueryHandler
|
||||
{
|
||||
protected List<IQueryInterceptor> Interceptors { get; } = new List<IQueryInterceptor>();
|
||||
private readonly IEnumerable<IQueryInterceptorProvider> queryableInterceptorProviders;
|
||||
|
||||
protected List<IQueryInterceptor> AddedInterceptors { get; } = new List<IQueryInterceptor>();
|
||||
protected IQueryCriteria Criteria { get; set; }
|
||||
protected IQueryable QueryableAtStart { get; private set; }
|
||||
protected IQueryable CurrentQueryable { get; set; }
|
||||
@@ -26,8 +28,21 @@ namespace PoweredSoft.DynamicQuery
|
||||
protected bool HasGrouping => Criteria.Groups?.Any() == true;
|
||||
protected bool HasPaging => Criteria.PageSize.HasValue && Criteria.PageSize > 0;
|
||||
|
||||
protected virtual void Reset(IQueryable queryable, IQueryCriteria criteria, IQueryExecutionOptions options)
|
||||
protected IReadOnlyList<IQueryInterceptor> Interceptors { get; set; }
|
||||
|
||||
protected virtual void ResetInterceptors<TSource, TResult>(IQueryCriteria criteria, IQueryable<TSource> queryable)
|
||||
{
|
||||
Interceptors = ResolveInterceptors<TSource, TResult>(criteria, queryable);
|
||||
}
|
||||
|
||||
public QueryHandlerBase(IEnumerable<IQueryInterceptorProvider> queryableInterceptorProviders)
|
||||
{
|
||||
this.queryableInterceptorProviders = queryableInterceptorProviders;
|
||||
}
|
||||
|
||||
protected virtual void Reset<TSource, TResult>(IQueryable<TSource> queryable, IQueryCriteria criteria, IQueryExecutionOptions options)
|
||||
{
|
||||
ResetInterceptors<TSource, TResult>(criteria, queryable);
|
||||
Criteria = criteria ?? throw new ArgumentNullException("criteria");
|
||||
QueryableAtStart = queryable ?? throw new ArgumentNullException("queryable");
|
||||
CurrentQueryable = QueryableAtStart;
|
||||
@@ -54,8 +69,8 @@ namespace PoweredSoft.DynamicQuery
|
||||
{
|
||||
if (interceptor == null) throw new ArgumentNullException("interceptor");
|
||||
|
||||
if (!Interceptors.Contains(interceptor))
|
||||
Interceptors.Add(interceptor);
|
||||
if (!AddedInterceptors.Contains(interceptor))
|
||||
AddedInterceptors.Add(interceptor);
|
||||
}
|
||||
|
||||
protected virtual IGroup InterceptGroup<TSource>(IGroup group)
|
||||
@@ -473,5 +488,16 @@ namespace PoweredSoft.DynamicQuery
|
||||
|
||||
await AfterReadInterceptors<TSource, TRecord>(pairs);
|
||||
}
|
||||
|
||||
public IReadOnlyList<IQueryInterceptor> ResolveInterceptors<TSource, TResult>(IQueryCriteria criteria, IQueryable<TSource> queryable)
|
||||
{
|
||||
var providedInterceptors = queryableInterceptorProviders.SelectMany(t => t.GetInterceptors<TSource, TResult>(criteria, queryable)).ToList();
|
||||
var final = providedInterceptors
|
||||
.Concat(AddedInterceptors)
|
||||
.Distinct(new QueryInterceptorEqualityComparer())
|
||||
.ToList();
|
||||
|
||||
return final;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
using PoweredSoft.DynamicQuery.Core;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace PoweredSoft.DynamicQuery
|
||||
{
|
||||
public class QueryInterceptorEqualityComparer : IEqualityComparer<IQueryInterceptor>
|
||||
{
|
||||
public bool Equals(IQueryInterceptor x, IQueryInterceptor y)
|
||||
{
|
||||
return x.GetType() == y.GetType();
|
||||
}
|
||||
|
||||
public int GetHashCode(IQueryInterceptor obj)
|
||||
{
|
||||
return obj.GetType().GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user