简体   繁体   English

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

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

I have got the following interface definition: 我有以下接口定义:

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

I have multiple implementations that implement ICommandHandler and need to be resolved. 我有多个实现ICommandHandler实现,需要解决。 While Castle Windows automatically injects the Logger property, when an ILogger is injected, I can't find a way to configure the SendAsync property to be set to true by Windsor during the creation of new instances. 当Castle Windows自动注入Logger属性时,当注入ILogger时,我无法找到一种方法来配置在创建新实例期间Windsor将SendAsync属性设置为true。

UPDATE UPDATE

The command handlers implement a generic interface that inherits from the base interface: 命令处理程序实现从基接口继承的通用接口:

public interface ICommandHandler<TCommand> : ICommandHandler
    where TCommand : Command
{
     void Handle(TCommand command);
}

Here is my configuration: 这是我的配置:

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>());

What ways are there to do this with Castle Windsor? Castle Windsor有什么方法可以做到这一点?

.DependsOn(Proprerty.ForKey("sendAsync").Eq(true))

or with anonymous type 或匿名类型

.DependsOn(new{ sendAsync = true })

regarding updated question, it seeems what you need is along the following lines: 关于更新的问题,它看起来你需要的是以下几行:

container.Register(AllTypes.FromThisAssembly()
   .BasedOn(typeof(ICommandHandler<>))
   .WithService.Base()
   .Configure(c => c.DependsOn(new{ sendAsync = true })
                    .Lifestyle.Transient));

That way you register and configure all handlers in one go, and as you add new ones you won't have to go back to add them to registration code. 这样,您可以一次注册和配置所有处理程序,当您添加新处理程序时,您不必返回将它们添加到注册代码中。

I suggest reading through the documentation as there's much more to the convention-based registration API than this. 我建议阅读文档,因为基于约定的注册API比这更多。

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

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