使用Castle Windsor在基类中注入原始属性
我有以下接口定义:
public interface ICommandHandler { ILogger Logger { get; set; } bool SendAsync { get; set; } }
我有多个实现ICommandHandler
实现,需要解决。 当Castle Windows自动注入Logger
属性时,当注入ILogger
时,我无法找到一种方法来配置在创建新实例期间Windsor将SendAsync
属性设置为true。
UPDATE
命令处理程序实现从基接口inheritance的通用接口:
public interface ICommandHandler : ICommandHandler where TCommand : Command { void Handle(TCommand command); }
这是我的配置:
var container = new WindsorContainer(); container.Register(Component .For<ICommandHandler>() .ImplementedBy()); container.Register(Component .For<ICommandHandler>() .ImplementedBy()); container.Register(Component .For<ICommandHandler>() .ImplementedBy());
Castle Windsor有什么方法可以做到这一点?
.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));
这样,您可以一次注册和配置所有处理程序,并且当您添加新处理程序时,您不必返回将它们添加到注册代码中。
我建议阅读文档,因为基于约定的注册API比这更多。