now supports interceptors for dynamic query.

This commit is contained in:
David Lebee 2021-02-02 20:39:59 -05:00
parent 0d3fbbcd01
commit d01ac1601c
3 changed files with 42 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
using PoweredSoft.DynamicQuery.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -15,7 +16,7 @@ namespace PoweredSoft.CQRS.DynamicQuery
{
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices) : base(queryHandlerAsync, queryableProviders, alterQueryableServices)
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices, IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, serviceProvider)
{
}
@ -37,7 +38,7 @@ namespace PoweredSoft.CQRS.DynamicQuery
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams) : base(queryHandlerAsync, queryableProviders, alterQueryableServices)
IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams, IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, serviceProvider)
{
this.alterQueryableServicesWithParams = alterQueryableServicesWithParams;
}

View File

@ -16,14 +16,17 @@ namespace PoweredSoft.CQRS.DynamicQuery
private readonly IQueryHandlerAsync queryHandlerAsync;
private readonly IEnumerable<IQueryableProvider<TSource>> queryableProviders;
private readonly IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices;
private readonly IServiceProvider serviceProvider;
public DynamicQueryHandlerBase(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices)
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IServiceProvider serviceProvider)
{
this.queryHandlerAsync = queryHandlerAsync;
this.queryableProviders = queryableProviders;
this.alterQueryableServices = alterQueryableServices;
this.serviceProvider = serviceProvider;
}
protected virtual Task<IQueryable<TSource>> GetQueryableAsync(IDynamicQuery query, CancellationToken cancellationToken = default)
@ -51,6 +54,23 @@ namespace PoweredSoft.CQRS.DynamicQuery
var options = GetQueryExecutionOptions(source, query);
var interceptors = this.GetInterceptors();
// basic after read service.
var afterReadService1 = this.serviceProvider.GetService(typeof(IAfterReadInterceptorAsync<TSource>));
if (afterReadService1 is IQueryInterceptor ars1)
queryHandlerAsync.AddInterceptor(ars1);
// support of injecting a query convert interceptor.
if (typeof(TSource) != typeof(TDestination))
{
var convertService = this.serviceProvider.GetService(typeof(IQueryConvertInterceptor<TSource, TDestination>));
if (convertService is IQueryInterceptor cs)
queryHandlerAsync.AddInterceptor(cs);
var afterReadService2 = this.serviceProvider.GetService(typeof(IAfterReadInterceptorAsync<TSource, TDestination>));
if (afterReadService2 is IQueryInterceptor ars2)
queryHandlerAsync.AddInterceptor(ars2);
}
foreach (var interceptor in interceptors)
queryHandlerAsync.AddInterceptor(interceptor);

View File

@ -94,5 +94,23 @@ namespace PoweredSoft.CQRS.DynamicQuery
{
return services.AddTransient<IAlterQueryableService<TSource, TDestination, TParams>, TService>();
}
public static IServiceCollection AddQueryConvertInterceptor<TSource, TDestination, TService>(this IServiceCollection services)
where TService : class, IQueryConvertInterceptor<TSource, TDestination>
{
return services.AddTransient<IQueryConvertInterceptor<TSource, TDestination>, TService>();
}
public static IServiceCollection AddAfterReadInterceptorAsync<TSource, TService>(this IServiceCollection services)
where TService : class, IAfterReadInterceptorAsync<TSource>
{
return services.AddTransient<IAfterReadInterceptorAsync<TSource>, TService>();
}
public static IServiceCollection AddAfterReadInterceptorAsync<TSource, TDestination, TService>(this IServiceCollection services)
where TService : class, IAfterReadInterceptorAsync<TSource, TDestination>
{
return services.AddTransient<IAfterReadInterceptorAsync<TSource, TDestination>, TService>();
}
}
}