added alter queryable service.
This commit is contained in:
parent
972a4060ca
commit
0d3fbbcd01
@ -6,6 +6,15 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Demo.DynamicQueries
|
||||
{
|
||||
public class SearchContactParamsService : IAlterQueryableService<Contact, Contact, SearchContactParams>
|
||||
{
|
||||
public Task<IQueryable<Contact>> AlterQueryableAsync(IQueryable<Contact> query, IDynamicQueryParams<SearchContactParams> dynamicQuery, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var safe = dynamicQuery.GetParams()?.SearchDisplayName;
|
||||
return Task.FromResult(query.Where(t => t.DisplayName.Contains(safe)));
|
||||
}
|
||||
}
|
||||
|
||||
public class ContactQueryableProvider : IQueryableProvider<Contact>
|
||||
{
|
||||
public Task<IQueryable<Contact>> GetQueryableAsync(object query, CancellationToken cancelllationToken = default)
|
||||
|
@ -63,7 +63,8 @@ namespace Demo
|
||||
{
|
||||
services.AddTransient<IQueryableProvider<Contact>, ContactQueryableProvider>();
|
||||
services.AddDynamicQuery<Contact>();
|
||||
services.AddDynamicQueryWithParams<Contact, SearchContactParams>(name: "SearchContacts");
|
||||
services.AddDynamicQueryWithParams<Contact, SearchContactParams>(name: "SearchContacts")
|
||||
.AddAlterQueryableWithParams<Contact, SearchContactParams, SearchContactParamsService>();
|
||||
}
|
||||
|
||||
private void AddCommands(IServiceCollection services)
|
||||
|
@ -2,17 +2,18 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PoweredSoft.CQRS.DynamicQuery.Abstractions
|
||||
{
|
||||
public interface IAlterQueryableService<TSource, TDestination>
|
||||
{
|
||||
Task<IQueryable<TSource>> AlterQueryableAsync(IQueryable<TSource> query, IDynamicQuery dynamicQuery);
|
||||
Task<IQueryable<TSource>> AlterQueryableAsync(IQueryable<TSource> query, IDynamicQuery dynamicQuery, CancellationToken cancellationToken = default);
|
||||
}
|
||||
public interface IAlterQueryableService<TSource, TDestination, TParams>
|
||||
where TParams : class
|
||||
{
|
||||
Task<IQueryable<TSource>> AlterQueryableAsync(IQueryable<TSource> query, IDynamicQueryParams<TParams> dynamicQuery);
|
||||
Task<IQueryable<TSource>> AlterQueryableAsync(IQueryable<TSource> query, IDynamicQueryParams<TParams> dynamicQuery, CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
|
||||
using PoweredSoft.DynamicQuery.Core;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -12,7 +13,9 @@ namespace PoweredSoft.CQRS.DynamicQuery
|
||||
where TSource : class
|
||||
where TDestination : class
|
||||
{
|
||||
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync, IEnumerable<IQueryableProvider<TSource>> queryableProviders) : base(queryHandlerAsync, queryableProviders)
|
||||
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
|
||||
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
|
||||
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices) : base(queryHandlerAsync, queryableProviders, alterQueryableServices)
|
||||
{
|
||||
}
|
||||
|
||||
@ -29,8 +32,27 @@ namespace PoweredSoft.CQRS.DynamicQuery
|
||||
where TDestination : class
|
||||
where TParams : class
|
||||
{
|
||||
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync, IEnumerable<IQueryableProvider<TSource>> queryableProviders) : base(queryHandlerAsync, queryableProviders)
|
||||
private readonly IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams;
|
||||
|
||||
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
|
||||
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
|
||||
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
|
||||
IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams) : base(queryHandlerAsync, queryableProviders, alterQueryableServices)
|
||||
{
|
||||
this.alterQueryableServicesWithParams = alterQueryableServicesWithParams;
|
||||
}
|
||||
|
||||
protected override async Task<IQueryable<TSource>> AlterSourceAsync(IQueryable<TSource> source, IDynamicQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
source = await base.AlterSourceAsync(source, query, cancellationToken);
|
||||
|
||||
if (query is IDynamicQueryParams<TParams> withParams)
|
||||
{
|
||||
foreach (var it in alterQueryableServicesWithParams)
|
||||
source = await it.AlterQueryableAsync(source, withParams, cancellationToken);
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
public Task<IQueryExecutionResult<TDestination>> HandleAsync(IDynamicQuery<TSource, TDestination, TParams> query, CancellationToken cancellationToken = default)
|
||||
|
@ -15,12 +15,15 @@ namespace PoweredSoft.CQRS.DynamicQuery
|
||||
{
|
||||
private readonly IQueryHandlerAsync queryHandlerAsync;
|
||||
private readonly IEnumerable<IQueryableProvider<TSource>> queryableProviders;
|
||||
private readonly IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices;
|
||||
|
||||
public DynamicQueryHandlerBase(IQueryHandlerAsync queryHandlerAsync,
|
||||
IEnumerable<IQueryableProvider<TSource>> queryableProviders)
|
||||
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
|
||||
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices)
|
||||
{
|
||||
this.queryHandlerAsync = queryHandlerAsync;
|
||||
this.queryableProviders = queryableProviders;
|
||||
this.alterQueryableServices = alterQueryableServices;
|
||||
}
|
||||
|
||||
protected virtual Task<IQueryable<TSource>> GetQueryableAsync(IDynamicQuery query, CancellationToken cancellationToken = default)
|
||||
@ -56,9 +59,12 @@ namespace PoweredSoft.CQRS.DynamicQuery
|
||||
return result;
|
||||
}
|
||||
|
||||
protected virtual Task<IQueryable<TSource>> AlterSourceAsync(IQueryable<TSource> source, IDynamicQuery query, CancellationToken cancellationToken)
|
||||
protected virtual async Task<IQueryable<TSource>> AlterSourceAsync(IQueryable<TSource> source, IDynamicQuery query, CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.FromResult(source);
|
||||
foreach (var t in alterQueryableServices)
|
||||
source = await t.AlterQueryableAsync(source, query, cancellationToken);
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
protected virtual IQueryCriteria CreateCriteriaFromQuery(IDynamicQuery query)
|
||||
|
Loading…
Reference in New Issue
Block a user