better way to inject interceptors into the dynamic query.

This commit is contained in:
David Lebee
2021-02-03 01:26:37 -05:00
parent d01ac1601c
commit de98b3b472
14 changed files with 263 additions and 73 deletions
@@ -16,7 +16,9 @@ namespace PoweredSoft.CQRS.DynamicQuery
{
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices, IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, serviceProvider)
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders,
IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, dynamicQueryInterceptorProviders, serviceProvider)
{
}
@@ -38,7 +40,9 @@ namespace PoweredSoft.CQRS.DynamicQuery
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams, IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, serviceProvider)
IEnumerable<IAlterQueryableService<TSource, TDestination, TParams>> alterQueryableServicesWithParams,
IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders,
IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, dynamicQueryInterceptorProviders, serviceProvider)
{
this.alterQueryableServicesWithParams = alterQueryableServicesWithParams;
}
@@ -16,16 +16,19 @@ namespace PoweredSoft.CQRS.DynamicQuery
private readonly IQueryHandlerAsync queryHandlerAsync;
private readonly IEnumerable<IQueryableProvider<TSource>> queryableProviders;
private readonly IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices;
private readonly IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders;
private readonly IServiceProvider serviceProvider;
public DynamicQueryHandlerBase(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders,
IServiceProvider serviceProvider)
{
this.queryHandlerAsync = queryHandlerAsync;
this.queryableProviders = queryableProviders;
this.alterQueryableServices = alterQueryableServices;
this.dynamicQueryInterceptorProviders = dynamicQueryInterceptorProviders;
this.serviceProvider = serviceProvider;
}
@@ -44,7 +47,9 @@ namespace PoweredSoft.CQRS.DynamicQuery
public virtual IEnumerable<IQueryInterceptor> GetInterceptors()
{
return Enumerable.Empty<IQueryInterceptor>();
var types = dynamicQueryInterceptorProviders.SelectMany(t => t.GetInterceptorsTypes()).Distinct();
foreach (var type in types)
yield return serviceProvider.GetService(type) as IQueryInterceptor;
}
protected async Task<IQueryExecutionResult<TDestination>> ProcessQueryAsync(IDynamicQuery query, CancellationToken cancellationToken = default)
@@ -54,23 +59,6 @@ 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);
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using PoweredSoft.CQRS.Abstractions;
using PoweredSoft.CQRS.Abstractions.Discovery;
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
@@ -95,22 +96,64 @@ 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>
public static IServiceCollection AddDynamicQueryInterceptor<TSource, TDestination, TInterceptor>(this IServiceCollection services)
where TInterceptor : class, IQueryInterceptor
{
return services.AddTransient<IQueryConvertInterceptor<TSource, TDestination>, TService>();
services.TryAddTransient<TInterceptor>();
return services.AddSingleton<IDynamicQueryInterceptorProvider<TSource, TDestination>>(
new DynamicQueryInterceptorProvider<TSource, TDestination>(typeof(TInterceptor)));
}
public static IServiceCollection AddAfterReadInterceptorAsync<TSource, TService>(this IServiceCollection services)
where TService : class, IAfterReadInterceptorAsync<TSource>
public static IServiceCollection AddDynamicQueryInterceptors<TSource, TDestination, T1, T2>(this IServiceCollection services)
where T1 : class, IQueryInterceptor
where T2 : class, IQueryInterceptor
{
return services.AddTransient<IAfterReadInterceptorAsync<TSource>, TService>();
services.TryAddTransient<T1>();
services.TryAddTransient<T2>();
return services.AddSingleton<IDynamicQueryInterceptorProvider<TSource, TDestination>>(
new DynamicQueryInterceptorProvider<TSource, TDestination>(typeof(T1), typeof(T2)));
}
public static IServiceCollection AddAfterReadInterceptorAsync<TSource, TDestination, TService>(this IServiceCollection services)
where TService : class, IAfterReadInterceptorAsync<TSource, TDestination>
public static IServiceCollection AddDynamicQueryInterceptors<TSource, TDestination, T1, T2, T3>(this IServiceCollection services)
where T1 : class, IQueryInterceptor
where T2 : class, IQueryInterceptor
where T3 : class, IQueryInterceptor
{
return services.AddTransient<IAfterReadInterceptorAsync<TSource, TDestination>, TService>();
services.TryAddTransient<T1>();
services.TryAddTransient<T2>();
services.TryAddTransient<T3>();
return services.AddSingleton<IDynamicQueryInterceptorProvider<TSource, TDestination>>(
new DynamicQueryInterceptorProvider<TSource, TDestination>(typeof(T1), typeof(T2), typeof(T3)));
}
public static IServiceCollection AddDynamicQueryInterceptors<TSource, TDestination, T1, T2, T3, T4>(this IServiceCollection services)
where T1 : class, IQueryInterceptor
where T2 : class, IQueryInterceptor
where T3 : class, IQueryInterceptor
where T4 : class, IQueryInterceptor
{
services.TryAddTransient<T1>();
services.TryAddTransient<T2>();
services.TryAddTransient<T3>();
services.TryAddTransient<T4>();
return services.AddSingleton<IDynamicQueryInterceptorProvider<TSource, TDestination>>(
new DynamicQueryInterceptorProvider<TSource, TDestination>(typeof(T1), typeof(T2), typeof(T3), typeof(T4)));
}
public static IServiceCollection AddDynamicQueryInterceptors<TSource, TDestination, T1, T2, T3, T4, T5>(this IServiceCollection services)
where T1 : class, IQueryInterceptor
where T2 : class, IQueryInterceptor
where T3 : class, IQueryInterceptor
where T4 : class, IQueryInterceptor
where T5 : class, IQueryInterceptor
{
services.TryAddTransient<T1>();
services.TryAddTransient<T2>();
services.TryAddTransient<T3>();
services.TryAddTransient<T4>();
services.TryAddTransient<T5>();
return services.AddSingleton<IDynamicQueryInterceptorProvider<TSource, TDestination>>(
new DynamicQueryInterceptorProvider<TSource, TDestination>(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5)));
}
}
}