我有以下界面定义:
public interface ICommandHandler
{
ILogger Logger { get; set; }
bool SendAsync { get; set; }
}
我有多个实现ICommandHandler
的实现,需要解决。当Castle Windows自动注入Logger
属性时,当注入ILogger
时,我无法找到一种方法来配置温莎在创建期间将SendAsync
属性设置为true新实例。
更新
命令处理程序实现从基接口继承的通用接口:
public interface ICommandHandler<TCommand> : ICommandHandler
where TCommand : Command
{
void Handle(TCommand command);
}
这是我的配置:
var container = new WindsorContainer();
container.Register(Component
.For<ICommandHandler<CustomerMovedCommand>>()
.ImplementedBy<CustomerMovedHandler>());
container.Register(Component
.For<ICommandHandler<ProcessOrderCommand>>()
.ImplementedBy<ProcessOrderHandler>());
container.Register(Component
.For<ICommandHandler<CustomerLeftCommand>>()
.ImplementedBy<CustomerLeftHandler>());
Castle Windsor有什么方法可以做到这一点?
答案 0 :(得分:8)
.DependsOn(Proprerty.ForKey("sendAsync").Eq(true))
或匿名类型
.DependsOn(new{ sendAsync = true })
关于更新的问题,它看起来你需要的是以下几行:
container.Register(AllTypes.FromThisAssembly()
.BasedOn(typeof(ICommandHandler<>))
.WithService.Base()
.Configure(c => c.DependsOn(new{ sendAsync = true })
.Lifestyle.Transient));
通过这种方式,您可以一次性注册和配置所有处理程序,当您添加新处理程序时,您无需返回将其添加到注册码中。
我建议reading through the documentation因为基于约定的注册API比这更多。