如何避免MediatR请求处理程序中的代码重复?

时间:2020-03-05 19:47:11

标签: c# oop domain-driven-design cqrs mediatr

我正在使用CQRS和MediatR库,试图学习一些最佳实践。 我遇到的一个问题是命令/查询处理程序中的代码重复。我想知道在处理程序之间共享逻辑的最佳方法是什么。

示例: 我有一个定义ID属性的抽象Entity类。所有实体都从该类继承。

public abstract class Entity
{
    public long Id { get; private set; }

    protected Entity(long id)
    {
        Id = id;
    }
    ...
}

然后对于每个实体,我想创建一个GetById查询。这些查询之一如下所示:

public class GetUserByIdQuery : IRequest<UserDto>
{
    public long UserId { get; set; }
    public class Handler : IRequestHandler<GetUserByIdQuery, UserDto>
    {
        private readonly IRepository<User> repository;
        private readonly IMapper mapper;

        public Handler(IUnitOfWork unitOfWork, IMapper mapper)
        {
            repository = unitOfWork.GetRepository<User>();
            this.mapper = mapper;
        }
        public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
        {
            var user = await repository.FindAsync(request.UserId, null, cancellationToken);
            if (user is null)
            {
                throw new EntityNotFoundException();
            }

            return mapper.Map<UserDto>(user);
        }
    }

}

问题是此类对于所有实体看起来都完全相同。没有CQRS,我可能会遇到这样的事情:

public class EntityFinder<TEntity, TDto> where TEntity : Entity
{
    private readonly IRepository<TEntity> repository;
    private readonly IMapper mapper;

    public EntityFinder(IUnitOfWork unitOfWork, IMapper mapper)
    {
        repository = unitOfWork.GetRepository<TEntity>();
        this.mapper = mapper;
    }
    public async Task<TDto> GetByIdAsync(long id)
    {
        var entity = await repository.FindAsync(id);
        if (entity is null)
        {
            throw new EntityNotFoundException();
        }

        return mapper.Map<TDto>(entity);
    }
}

我尝试对通用查询和处理程序执行类似的操作,但是MediatR找不到处理程序(即使尝试将其手动注册到DI容器时也是如此)。

避免这种重复的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

您可以尝试以下代码吗?这样,您可以重用加载代码,同时提供一个端点来处理请求。

public class EntityFinder<TEntity, TDto> where TEntity : Entity
{ ... // Same as your code }

public class GetUserByIdQuery : IRequest<UserDto>
{
    public long UserId { get; set; }
    public class Handler : IRequestHandler<GetUserByIdQuery, UserDto>, EntityFinder<User, UserDto>
    {
        public Handler(IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork, mapper)
        { }
        public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
            => await base.GetByIdAsync(request.UserId);
    }

}