c# - 分页作为带有简单注入(inject)器的 cqrs 中的横切关注点

标签 c# .net design-patterns pagination simple-injector

在我的应用程序设计中,我试图实现 Pagination作为Cross-Cutting ConcernDecorator应用于 an implementation 的图案的 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() 查询上调用 Linq to Entities)。
我想要的是我的查询处理程序应该像这样实现:

public class GetAllItemsQueryHandler : IQueryHandler<GetAllItemsQuery, IEnumerable<Item>>

所以处理程序的返回类型是IEnumerable<Item> .这样处理程序就被强制为 Single Responsible . 我面临的问题可能是我使用 Simple Injector 的方式.因为我正在注册我的 IQueryHandler<,>喜欢

container.Register(typeof(IQueryHandler<,>), assemblies);

这不会验证我的设计,因为明显的无效配置:我正在注入(inject) IQueryHandler<GetAllItemsQuery, PaginatedResult<Item>>进入我的消费者,但实际上并没有实现它。相反,处理程序实现了 IQueryHandler<GetAllItemsQuery, IEnumerable<Item>> .

因此,作为一种解决方案,我尝试实现一个 Interceptor并有条件地注册(注意 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.

好吧....如果你坚持的话:)

但至少要防止这些查询返回 IEnumerable<T> , 但返回 IQueryable<T>反而。 IEnumerable<T>的使用将导致从数据库返回所有数据,即使您翻页也是如此。

也就是说,我不确定您的代码有什么问题,但我想建议一种稍微不同的方法:

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>PageInfoPagedQuery<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; }
}

PageInfoPaged<T>源自此 Github 存储库:https://github.com/dotnetjunkie/solidservices/tree/master/src/Contract/

PagedQueryHandler<TQuery, TItem>可以注册如下:

container.Register(typeof(IQueryHandler<,>), typeof(PagedQueryHandler<,>));

通过此类及其注册,您可以简单地将可分页查询处理程序注入(inject)消费者,例如:

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));
    }
}

关于c# - 分页作为带有简单注入(inject)器的 cqrs 中的横切关注点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46777396/

相关文章:

c# - 如何删除 ListViewItem 上的选择边框

c# - 使用 Autofixture 控制对象树的生成深度

.net - .NET 程序集名称是否应该包含版本号?

.net - .NET 6.0 可以安装在 Windows 2012 R2 上吗?

c# - 为什么.NET RSACryptoServiceProvider 会抛出 "Safe handle has been closed"异常?

design-patterns - 你的域模型对象中应该有多少逻辑

c# - Linq:如何将行转换为具有计数的列(交叉表数据)?

c# - 是否可以为 linq-to-objects 编译查询

java - 使用 java 遍历 CIDR 中的所有 IP 地址

design-patterns - 层次结构和设计模式问题