分页作为带有简单注入器的 cqrs 中的横切关注点
Pagination as cross-cutting concern in cqrs with Simple Injector
在我的应用程序设计中,我试图实现 Pagination as a Cross-Cutting Concern with the Decorator pattern applied to an implementation of the CQRS 模式。
我也有一个 multi-layered architecture,我认为分页 不是 业务逻辑的一部分(因此是一个横切关注点)。这是已经做出的决定,不应在本主题中讨论。
在我的设计中,意图是表示层可以使用具有特定封闭通用类型的分页查询
IQueryHandler<GetAllItemsQuery, PaginatedQuery<Item>>
具有以下签名:
public class GetAllItemsQuery : PaginatedQuery<Item>
public class PaginatedQuery<TModel> :
IQuery<PaginatedResult<TModel>>, IQuery<IEnumerable<TModel>>
public class PaginatedResult<TModel>
这个想法是,消费者应该收到一个特定模型的 PaginatedResult
,其中包含分页的项目和一些元数据(例如,在没有应用分页的情况下执行的查询的项目总数),以便UI 可以呈现它的分页。
我设计的主要理念是查询处理程序应该只应用它的 业务逻辑 (例如获取所有项目)。它只描述了它将如何执行此操作,不一定必须执行查询。
在我的例子中,查询处理程序上的装饰器实际上对查询应用分页并执行它(例如,通过调用 .ToArray()
on a Linq to Entities 查询)。
我想要的是我的查询处理程序应该像这样实现:
public class GetAllItemsQueryHandler : IQueryHandler<GetAllItemsQuery, IEnumerable<Item>>
因此处理程序的 return 类型是 IEnumerable<Item>
。这样处理程序就被强制为Single Responsible。
我面临的问题可能是我使用 Simple Injector 的方式。因为我正在注册 IQueryHandler<,>
就像
container.Register(typeof(IQueryHandler<,>), assemblies);
这不会验证我的设计,因为明显的无效配置:我正在将 IQueryHandler<GetAllItemsQuery, PaginatedResult<Item>>
注入我的消费者,但实际上并没有实现它。相反,处理程序实现 IQueryHandler<GetAllItemsQuery, IEnumerable<Item>>
.
因此,作为解决方案,我尝试实施 Interceptor and register that conditionally (note the usage of C# 7.0 local functions):
Type PaginationInterceptorFactory(TypeFactoryContext typeContext)
{
// IQueryHandler<TQuery, TResult> where TResult is PaginatedResult<TModel>
var queryType = typeContext.ServiceType.GetGenericArguments()[0]; // TQuery
var modelType = typeContext.ServiceType.GetGenericArguments()[1].GetGenericArguments()[0]; // TModel in PaginatedResult<TModel> as TResult
return typeof(PaginatedQueryHandlerInterceptor<,>).MakeGenericType(queryType, modelType);
}
bool PaginationInterceptorPredicate(PredicateContext predicateContext) =>
predicateContext.ServiceType.GetGenericArguments()[0].IsPaginatedQuery(); // if TQuery is of type PaginatedQuery<>
container.RegisterConditional(typeof(IQueryHandler<,>), PaginationInterceptorFactory, Lifestyle.Singleton, PaginationInterceptorPredicate);
但这给了我一个验证异常:
System.InvalidOperationException occurred
Message=The configuration is invalid. Creating the instance for type [TYPE] failed. This operation is only valid on generic types.
Source=SimpleInjector
StackTrace:
at SimpleInjector.InstanceProducer.VerifyExpressionBuilding()
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt()
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()
Inner Exception 1:
ActivationException: This operation is only valid on generic types.
Inner Exception 2:
InvalidOperationException: This operation is only valid on generic types.
异常并不清楚操作是什么以及为什么它无效。也许我做错了什么?
拦截器的实现如下:
public class PaginatedQueryHandlerInterceptor<TQuery, TModel> : IQueryHandler<TQuery, PaginatedResult<TModel>>
where TQuery : PaginatedQuery<TModel>
{
private readonly IQueryHandler<TQuery, IEnumerable<TModel>> _queryHandler;
public PaginatedQueryHandlerInterceptor(IQueryHandler<TQuery, IEnumerable<TModel>> queryHandler)
{
_queryHandler = queryHandler;
}
public PaginatedResult<TModel> Handle(TQuery query)
{
return (dynamic) _queryHandler.Handle(query);
}
}
和装饰者:
public class PaginationQueryHandlerDecorator<TQuery, TResult> : IQueryHandler<TQuery, TResult>
where TQuery : class, IQuery<TResult>
{
private readonly IQueryHandler<TQuery, TResult> _decoratee;
public PaginationQueryHandlerDecorator(
IQueryHandler<TQuery, TResult> decoratee)
{
_decoratee = decoratee;
}
public TResult Handle(TQuery query)
{
query.ThrowIfNull(nameof(query));
var result = _decoratee.Handle(query);
if (query.IsPaginationQuery(out var paginatedQuery))
{
return Paginate(result, paginatedQuery.Pagination);
}
return result;
}
private static TResult Paginate(TResult result, Pagination pagination)
{
return Paginate(result as dynamic, pagination.Page, pagination.ItemsPerPage);
}
private static PaginatedResult<TModel> Paginate<TModel>(IEnumerable<TModel> result, int page, int itemsPerPage)
{
var items = result as TModel[] ?? result.ToArray();
var paginated = items.Skip(page * itemsPerPage).Take(itemsPerPage).ToArray();
return new PaginatedResult<TModel>
{
Items = paginated,
Count = items.Length
};
}
}
This is a decision already made and should not be discussed in this topic.
嗯....如果你坚持:)
但至少要防止这些查询 returning IEnumerable<T>
,而是 return IQueryable<T>
。使用 IEnumerable<T>
将导致 所有 数据从数据库中 return 编辑,即使您翻页也是如此。
也就是说,我不确定您的代码有什么问题,但我想建议一种稍微不同的方法:
public class PagedQueryHandler<TQuery, TItem>
: IQueryHandler<PagedQuery<TQuery, TItem>, Paged<TItem>>
where TQuery : IQuery<IQueryable<TItem>>
{
private readonly IQueryHandler<TQuery, IQueryable<TItem>> handler;
public PagedQueryHandler(IQueryHandler<TQuery, IQueryable<TItem>> handler)
{
this.handler = handler;
}
public Paged<TItem> Handle(PagedQuery<TQuery, TItem> query)
{
var paging = query.PageInfo ?? new PageInfo();
IQueryable<TItem> items = this.handler.Handle(query.Query);
return new Paged<TItem>
{
Items = items.Skip(paging.PageIndex * paging.PageSize)
.Take(paging.PageSize).ToArray(),
Paging = paging,
};
}
}
此通用 IQueryHandler
实现可以将分页查询映射到非分页查询。这里Paged<T>
和PageInfo
和PagedQuery<TQuery, TItem>
定义如下:
public class Paged<T>
{
public PageInfo Paging { get; set; }
public T[] Items { get; set; }
}
public class PageInfo
{
public int PageIndex { get; set; }
public int PageSize { get; set; } = 20;
}
public class PagedQuery<TQuery, TItem> : IQuery<Paged<TItem>>
where TQuery : IQuery<IQueryable<TItem>>
{
public TQuery Query { get; set; }
public PageInfo PageInfo { get; set; }
}
PageInfo
和 Paged<T>
源自此 Github 存储库:https://github.com/dotnetjunkie/solidservices/tree/master/src/Contract/
PagedQueryHandler<TQuery, TItem>
可以注册如下:
container.Register(typeof(IQueryHandler<,>), typeof(PagedQueryHandler<,>));
有了这个 class 及其注册,您可以简单地将可分页查询处理程序注入消费者,例如:
public class ItemsController
{
IQueryHandler<PagedQuery<GetAllItemsQuery, Item>, Paged<Item>> handler;
public ItemsController(
IQueryHandler<PagedQuery<GetAllItemsQuery, Item>, Paged<Item>> handler)
{
this.handler = handler;
}
public ActionResult Index(PagedQuery<GetAllItemsQuery, Item> query)
{
return View(this.handler.Handle(query));
}
}
实际问题确实是我做错了,但与Simple Injector无关。
使用 RegisterConditional
时,在谓词中调用了此扩展方法:
bool PaginationInterceptorPredicate(PredicateContext predicateContext) =>
predicateContext.ServiceType.GetGenericArguments()[0].IsPaginatedQuery();
IsPaginatedQuery
执行错误导致异常:
public static bool IsPaginatedQuery(this Type queryType) =>
queryType.GetInterfaces().Any(i => i.GetGenericTypeDefinition() == typeof(PaginatedQuery<>));
因为查询还实现了一个非泛型接口IPagination
,因此GetGenericTypeDefinition()
方法导致了异常。
在我的应用程序设计中,我试图实现 Pagination as a Cross-Cutting Concern with the Decorator pattern applied to an implementation of the CQRS 模式。
我也有一个 multi-layered architecture,我认为分页 不是 业务逻辑的一部分(因此是一个横切关注点)。这是已经做出的决定,不应在本主题中讨论。
在我的设计中,意图是表示层可以使用具有特定封闭通用类型的分页查询
IQueryHandler<GetAllItemsQuery, PaginatedQuery<Item>>
具有以下签名:
public class GetAllItemsQuery : PaginatedQuery<Item>
public class PaginatedQuery<TModel> :
IQuery<PaginatedResult<TModel>>, IQuery<IEnumerable<TModel>>
public class PaginatedResult<TModel>
这个想法是,消费者应该收到一个特定模型的 PaginatedResult
,其中包含分页的项目和一些元数据(例如,在没有应用分页的情况下执行的查询的项目总数),以便UI 可以呈现它的分页。
我设计的主要理念是查询处理程序应该只应用它的 业务逻辑 (例如获取所有项目)。它只描述了它将如何执行此操作,不一定必须执行查询。
在我的例子中,查询处理程序上的装饰器实际上对查询应用分页并执行它(例如,通过调用 .ToArray()
on a Linq to Entities 查询)。
我想要的是我的查询处理程序应该像这样实现:
public class GetAllItemsQueryHandler : IQueryHandler<GetAllItemsQuery, IEnumerable<Item>>
因此处理程序的 return 类型是 IEnumerable<Item>
。这样处理程序就被强制为Single Responsible。
我面临的问题可能是我使用 Simple Injector 的方式。因为我正在注册 IQueryHandler<,>
就像
container.Register(typeof(IQueryHandler<,>), assemblies);
这不会验证我的设计,因为明显的无效配置:我正在将 IQueryHandler<GetAllItemsQuery, PaginatedResult<Item>>
注入我的消费者,但实际上并没有实现它。相反,处理程序实现 IQueryHandler<GetAllItemsQuery, IEnumerable<Item>>
.
因此,作为解决方案,我尝试实施 Interceptor and register that conditionally (note the usage of C# 7.0 local functions):
Type PaginationInterceptorFactory(TypeFactoryContext typeContext)
{
// IQueryHandler<TQuery, TResult> where TResult is PaginatedResult<TModel>
var queryType = typeContext.ServiceType.GetGenericArguments()[0]; // TQuery
var modelType = typeContext.ServiceType.GetGenericArguments()[1].GetGenericArguments()[0]; // TModel in PaginatedResult<TModel> as TResult
return typeof(PaginatedQueryHandlerInterceptor<,>).MakeGenericType(queryType, modelType);
}
bool PaginationInterceptorPredicate(PredicateContext predicateContext) =>
predicateContext.ServiceType.GetGenericArguments()[0].IsPaginatedQuery(); // if TQuery is of type PaginatedQuery<>
container.RegisterConditional(typeof(IQueryHandler<,>), PaginationInterceptorFactory, Lifestyle.Singleton, PaginationInterceptorPredicate);
但这给了我一个验证异常:
System.InvalidOperationException occurred
Message=The configuration is invalid. Creating the instance for type [TYPE] failed. This operation is only valid on generic types.
Source=SimpleInjector
StackTrace:
at SimpleInjector.InstanceProducer.VerifyExpressionBuilding()
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt()
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()
Inner Exception 1:
ActivationException: This operation is only valid on generic types.
Inner Exception 2:
InvalidOperationException: This operation is only valid on generic types.
异常并不清楚操作是什么以及为什么它无效。也许我做错了什么?
拦截器的实现如下:
public class PaginatedQueryHandlerInterceptor<TQuery, TModel> : IQueryHandler<TQuery, PaginatedResult<TModel>>
where TQuery : PaginatedQuery<TModel>
{
private readonly IQueryHandler<TQuery, IEnumerable<TModel>> _queryHandler;
public PaginatedQueryHandlerInterceptor(IQueryHandler<TQuery, IEnumerable<TModel>> queryHandler)
{
_queryHandler = queryHandler;
}
public PaginatedResult<TModel> Handle(TQuery query)
{
return (dynamic) _queryHandler.Handle(query);
}
}
和装饰者:
public class PaginationQueryHandlerDecorator<TQuery, TResult> : IQueryHandler<TQuery, TResult>
where TQuery : class, IQuery<TResult>
{
private readonly IQueryHandler<TQuery, TResult> _decoratee;
public PaginationQueryHandlerDecorator(
IQueryHandler<TQuery, TResult> decoratee)
{
_decoratee = decoratee;
}
public TResult Handle(TQuery query)
{
query.ThrowIfNull(nameof(query));
var result = _decoratee.Handle(query);
if (query.IsPaginationQuery(out var paginatedQuery))
{
return Paginate(result, paginatedQuery.Pagination);
}
return result;
}
private static TResult Paginate(TResult result, Pagination pagination)
{
return Paginate(result as dynamic, pagination.Page, pagination.ItemsPerPage);
}
private static PaginatedResult<TModel> Paginate<TModel>(IEnumerable<TModel> result, int page, int itemsPerPage)
{
var items = result as TModel[] ?? result.ToArray();
var paginated = items.Skip(page * itemsPerPage).Take(itemsPerPage).ToArray();
return new PaginatedResult<TModel>
{
Items = paginated,
Count = items.Length
};
}
}
This is a decision already made and should not be discussed in this topic.
嗯....如果你坚持:)
但至少要防止这些查询 returning IEnumerable<T>
,而是 return IQueryable<T>
。使用 IEnumerable<T>
将导致 所有 数据从数据库中 return 编辑,即使您翻页也是如此。
也就是说,我不确定您的代码有什么问题,但我想建议一种稍微不同的方法:
public class PagedQueryHandler<TQuery, TItem>
: IQueryHandler<PagedQuery<TQuery, TItem>, Paged<TItem>>
where TQuery : IQuery<IQueryable<TItem>>
{
private readonly IQueryHandler<TQuery, IQueryable<TItem>> handler;
public PagedQueryHandler(IQueryHandler<TQuery, IQueryable<TItem>> handler)
{
this.handler = handler;
}
public Paged<TItem> Handle(PagedQuery<TQuery, TItem> query)
{
var paging = query.PageInfo ?? new PageInfo();
IQueryable<TItem> items = this.handler.Handle(query.Query);
return new Paged<TItem>
{
Items = items.Skip(paging.PageIndex * paging.PageSize)
.Take(paging.PageSize).ToArray(),
Paging = paging,
};
}
}
此通用 IQueryHandler
实现可以将分页查询映射到非分页查询。这里Paged<T>
和PageInfo
和PagedQuery<TQuery, TItem>
定义如下:
public class Paged<T>
{
public PageInfo Paging { get; set; }
public T[] Items { get; set; }
}
public class PageInfo
{
public int PageIndex { get; set; }
public int PageSize { get; set; } = 20;
}
public class PagedQuery<TQuery, TItem> : IQuery<Paged<TItem>>
where TQuery : IQuery<IQueryable<TItem>>
{
public TQuery Query { get; set; }
public PageInfo PageInfo { get; set; }
}
PageInfo
和 Paged<T>
源自此 Github 存储库:https://github.com/dotnetjunkie/solidservices/tree/master/src/Contract/
PagedQueryHandler<TQuery, TItem>
可以注册如下:
container.Register(typeof(IQueryHandler<,>), typeof(PagedQueryHandler<,>));
有了这个 class 及其注册,您可以简单地将可分页查询处理程序注入消费者,例如:
public class ItemsController
{
IQueryHandler<PagedQuery<GetAllItemsQuery, Item>, Paged<Item>> handler;
public ItemsController(
IQueryHandler<PagedQuery<GetAllItemsQuery, Item>, Paged<Item>> handler)
{
this.handler = handler;
}
public ActionResult Index(PagedQuery<GetAllItemsQuery, Item> query)
{
return View(this.handler.Handle(query));
}
}
实际问题确实是我做错了,但与Simple Injector无关。
使用 RegisterConditional
时,在谓词中调用了此扩展方法:
bool PaginationInterceptorPredicate(PredicateContext predicateContext) =>
predicateContext.ServiceType.GetGenericArguments()[0].IsPaginatedQuery();
IsPaginatedQuery
执行错误导致异常:
public static bool IsPaginatedQuery(this Type queryType) =>
queryType.GetInterfaces().Any(i => i.GetGenericTypeDefinition() == typeof(PaginatedQuery<>));
因为查询还实现了一个非泛型接口IPagination
,因此GetGenericTypeDefinition()
方法导致了异常。