我有很多命令和查询,其中大多数需要相同的接口 DI 来做不同的事情。是否有可能减少我的每个处理程序都需要并且一遍又一遍地重复的这种混乱?
public class GetCoinByIdQueryHandler : IRequestHandler<GetCoinByIdQuery, CoinModel>
{
private readonly EventsContext context;
private readonly ICacheClient cache;
private readonly ILogger logger;
private readonly IMapper mapper;
private readonly Settings settings;
public GetCoinByIdQueryHandler(
EventsContext context, ICacheClient cache, ILogger logger,
IMapper mapper, IOptions<Settings> settings)
{
this.context = context;
this.cache = cache;
this.logger = logger;
this.mapper = mapper;
this.settings = settings.Value;
}
}
这可能与 Mediatr 没有直接关系,但我正在寻找一种更优雅的方式,将所有常见的参数减少为一个 DI'ed 参数。
如果有任何不同,我将使用 Autofac 作为我的 DI 容器。
编辑:可能有所有处理程序从基类继承的基类,并且在基类中可以访问所有接口并将它们设置为基类的属性,但我不知道如何实现这一点。
编辑 2:Autofac 有属性注入,但这似乎不是正确的方法,所以使用 Mediatr 的人,你如何处理一遍又一遍地重复自己。我见过的每个使用 Mediatr 的开源项目似乎都没有解决重复自己的问题。