dotnet-cqrs/PoweredSoft.CQRS.DynamicQuery/DynamicQueryHandler.cs

69 lines
3.4 KiB
C#
Raw Normal View History

2021-02-02 19:01:29 -05:00
using PoweredSoft.CQRS.DynamicQuery.Abstractions;
using PoweredSoft.DynamicQuery.Core;
using System;
2021-02-02 19:01:29 -05:00
using System.Collections.Generic;
2021-02-02 20:06:56 -05:00
using System.Linq;
2021-02-02 19:01:29 -05:00
using System.Threading;
using System.Threading.Tasks;
namespace PoweredSoft.CQRS.DynamicQuery
{
2021-02-02 19:32:39 -05:00
public class DynamicQueryHandler<TSource, TDestination>
: DynamicQueryHandlerBase<TSource, TDestination>,
2021-02-02 19:01:29 -05:00
PoweredSoft.CQRS.Abstractions.IQueryHandler<IDynamicQuery<TSource, TDestination>, IQueryExecutionResult<TDestination>>
where TSource : class
where TDestination : class
{
2021-02-02 20:06:56 -05:00
public DynamicQueryHandler(IQueryHandlerAsync queryHandlerAsync,
IEnumerable<IQueryableProvider<TSource>> queryableProviders,
IEnumerable<IAlterQueryableService<TSource, TDestination>> alterQueryableServices,
IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders,
IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, dynamicQueryInterceptorProviders, serviceProvider)
2021-02-02 19:01:29 -05:00
{
}
2021-02-02 19:32:39 -05:00
public Task<IQueryExecutionResult<TDestination>> HandleAsync(IDynamicQuery<TSource, TDestination> query, CancellationToken cancellationToken = default)
2021-02-02 19:01:29 -05:00
{
2021-02-02 19:32:39 -05:00
return ProcessQueryAsync(query, cancellationToken);
2021-02-02 19:01:29 -05:00
}
2021-02-02 19:32:39 -05:00
}
2021-02-02 19:01:29 -05:00
2021-02-02 19:32:39 -05:00
public class DynamicQueryHandler<TSource, TDestination, TParams>
: DynamicQueryHandlerBase<TSource, TDestination>,
PoweredSoft.CQRS.Abstractions.IQueryHandler<IDynamicQuery<TSource, TDestination, TParams>, IQueryExecutionResult<TDestination>>
where TSource : class
where TDestination : class
where TParams : class
{
2021-02-02 20:06:56 -05:00
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,
IEnumerable<IDynamicQueryInterceptorProvider<TSource, TDestination>> dynamicQueryInterceptorProviders,
IServiceProvider serviceProvider) : base(queryHandlerAsync, queryableProviders, alterQueryableServices, dynamicQueryInterceptorProviders, serviceProvider)
2021-02-02 20:06:56 -05:00
{
this.alterQueryableServicesWithParams = alterQueryableServicesWithParams;
}
protected override async Task<IQueryable<TSource>> AlterSourceAsync(IQueryable<TSource> source, IDynamicQuery query, CancellationToken cancellationToken)
2021-02-02 19:01:29 -05:00
{
2021-02-02 20:06:56 -05:00
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;
2021-02-02 19:01:29 -05:00
}
2021-02-02 19:32:39 -05:00
public Task<IQueryExecutionResult<TDestination>> HandleAsync(IDynamicQuery<TSource, TDestination, TParams> query, CancellationToken cancellationToken = default)
2021-02-02 19:01:29 -05:00
{
2021-02-02 19:32:39 -05:00
return this.ProcessQueryAsync(query, cancellationToken);
2021-02-02 19:01:29 -05:00
}
}
}