用简单注射器在cqrs中作为交叉切割关注的分页

时间:2017-10-16 19:04:51

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

在我的应用程序设计中,我尝试将Pagination实现为Cross Cutting ConcernDecorator模式应用于an implementation模式的CQRS
我也有一个multilayered 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可以渲染它的分页 我的设计的主要哲学是查询处理程序应该只应用它的业务逻辑(例如获取所有项目)。它只描述了它是如何做到的,它不一定要执行查询 在我的例子中,queryhandler上的装饰器实际上对查询应用分页并执行它(例如通过在.ToArray()查询上调用Linq to Entities)。
我想要的是我的queryhandler应该像这样实现:

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

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

container.Register(typeof(IQueryHandler<,>), assemblies);
由于明显无效的配置,

无法验证我的设计:我正在向我的消费者注入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
            };
        }
    }

2 个答案:

答案 0 :(得分:1)

  

这是已做出的决定,不应在本主题中讨论。

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

但至少阻止这些查询返回ob_start(); session_start(); include "storescripts/connect_to_mysql.php"; include "func.php"; if(isset($_POST["submit"])) { //STEP DOWN VALUES $name = $_POST["username"]; $password = $_POST["password"]; //CLEANUP VALUES $name = cleanvalues($username); $password = cleanvalues($password); $password = md5($password); function Is_email($user) { //If the username input string is an e-mail, return true if(filter_var($user, FILTER_VALIDATE_EMAIL)) { return true; } else { return false; } } $check_email = Is_email($name); if($check_email) { $query = mysqli_query($conn, "SELECT `user_name` FROM `registered_users` WHERE `email` = '$name' AND `password` = '$pass'"); } elseif(is_numeric($name)) { $query = mysqli_query($conn, "SELECT `user_name` FROM `registered_users` WHERE `number` = '$name' AND `password` = '$pass'"); } else { $query = mysqli_query($conn, "SELECT `user_name` FROM `registered_users` WHERE `user_name` = '$name' AND `password` = '$pass'"); $rows = mysql_num_rows($query); if($rows > 0) { while($row = mysqli_fetch_array($query)) { $dname = cleanvalues2($row["user_name"]); } $checkuser = mysqli_query($conn, "SELECT user_name FROM registered_users WHERE user_name='$dname' AND validated=0"); $checknum = mysqli_num_rows($checkuser); if($checknum > 0) { header("Location:activate.php"); exit(); } else { $checkquery = mysqli_query($conn, "SELECT user_name, password, validated FROM registered_users WHERE user_name='$dname' AND password='$password' AND validated=1") or die(mysql_error()); $num = mysqli_num_rows($checkquery); if($num > 0) { if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM registered_users WHERE username='$dname' AND banned=1")) > 0) { $regmsg = "You Have Been Banished By one of the Administrator! .How do You feel.....lol"; header("location:indx.php?msg=$regmsg"); exit(); } $_SESSION["user"] = $dname; header("location:home.php"); } else { $regmsg = "incorrect username and/or password, make sure you type correctly your username & password !"; header("location:indx.php?msg=$regmsg"); } } } } } ,而是返回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, }; } } 实现可以将分页查询映射到非分页查询。此处IQueryHandlerPaged<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来自此Github回购:https://github.com/dotnetjunkie/solidservices/tree/master/src/Contract/

Paged<T>可以注册如下:

PagedQueryHandler<TQuery, TItem>

使用此类及其注册,您只需将可分页查询处理程序注入到使用者中,例如:

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

答案 1 :(得分:0)

实际问题确实是我做错了,但与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()方法导致了异常。