我希望得到一些关于如何为某些特定合同和实现注册装饰器的反馈。 这就是我所拥有的
像这样的标记查询界面
public interface IQueryBase
{
}
像这样的查询处理程序合同
public interface IQueryHandlerNew<TQuery, TResult>
where TQuery : IQueryBase
{
TResult Handle(TQuery request);
Task<TResult> HandleAsync(TQuery request);
}
像这样的实际查询
public sealed class FindCitiesByCountryIdQuery : IQueryBase
{
public int PageNumber { get; set; }
public int CountryId { get; set; }
public int PageSize { get; set; }
}
public class CityReadModel
{
public int CityId { get; set; }
public string CityName { get; set; }
}
最后查询处理程序就像这样
public class FindCitiesByCountryIdQueryHandler : IQueryHandlerNew<FindCitiesByCountryIdQuery, CityReadModel[]>
{
public CityReadModel[] Handle(FindCitiesByCountryIdQuery request)
{
throw new NotImplementedException();
}
public Task<CityReadModel[]> HandleAsync(FindCitiesByCountryIdQuery request)
{
throw new NotImplementedException();
}
}
这是queryhandlers的loggingdecorator
public class QueryHandlerLogginDecorator2<TQuery, TResult> : IQueryHandlerNew<TQuery, TResult> where TQuery : IQueryBase
{
private readonly IQueryHandlerNew<TQuery, TResult> _decoratee;
private readonly ICustomLogger _logger;
public QueryHandlerLogginDecorator2(IQueryHandlerNew<TQuery, TResult> decoratee, ICustomLogger logger)
{
_decoratee = decoratee;
_logger = logger;
}
public TResult Handle(TQuery request)
{
try
{
//Log request information here
return _decoratee.Handle(request);
}
catch (Exception ex)
{
_logger.LogError(ex, string.Format(LogConstants.ErrorLogFormat, DateTime.UtcNow, GetType(), ex.Message));
throw;
}
}
public Task<TResult> HandleAsync(TQuery request)
{
try
{
//Log request information here
return _decoratee.HandleAsync(request);
}
catch (Exception ex)
{
_logger.LogError(ex, string.Format(LogConstants.ErrorLogFormat, DateTime.UtcNow, GetType(), ex.Message));
throw;
}
}
}
当我尝试使用像这样的简单注入器寄存器装饰器注册此设置时
container.RegisterDecorator(typeof(IQueryHandlerNew<>), typeof(QueryHandlerLogginDecorator2<>), Lifestyle.Transient);
它失败并说“使用IQueryHandlerNew需要2个类型参数”
我使用类似的设置来注册具有1个类型参数的装饰器(例如对于存储库)并且它注册得很好。
不确定我在这里做错了什么。 simpleinjector和使用多于1个类型参数注册装饰器是否存在已知限制?
非常感谢任何反馈
提前致谢