如何在CQRS .NET Core中使用基本处理程序类解决构造函数中的注入

我正在尝试创建一个CQRS模式应用程序。我有处理程序类来管理我的业务。但是在构造函数中会出现很多项目,因此我需要在我创建的每个处理程序中编写它们。那么,有什么解决方案可以将所有这些项目注入基本处理程序类中,并使我的处理程序更纯净吗?

public class Handler : IRequestHandler<Command>

    {
        private readonly DataContext _context;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly string _value;
        private readonly IMapper _mapper;
        private readonly IEventBus _bus;

        public Handler(DataContext context, IHttpContextAccessor httpContextAccessor, IMapper mapper, IEventBus bus)

        {
            _context = context;
            _httpContextAccessor = httpContextAccessor;
            _mapper = mapper;
            _bus = bus;


            if (httpContextAccessor.HttpContext != null)
                _value = _httpContextAccessor.HttpContext.Items["Value"].ToString();
        }


        public async Task<Unit> Handle(Command request, CancellationToken cancellationToken)
        {
            if (true) return Unit.Value;

            throw new Exception("Error Message");
        }
    }
评论