繁体   English   中英

使用Castle Windsor在基类中注入原始属性

[英]Injecting a primitive property in a base class with Castle Windsor

我有以下接口定义:

public interface ICommandHandler
{
    ILogger Logger { get; set; }
    bool SendAsync { get; set; }
}

我有多个实现ICommandHandler实现,需要解决。 当Castle Windows自动注入Logger属性时,当注入ILogger时,我无法找到一种方法来配置在创建新实例期间Windsor将SendAsync属性设置为true。

UPDATE

命令处理程序实现从基接口继承的通用接口:

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有什么方法可以做到这一点?

.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比这更多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM