使用Castle Windsor在基类中注入原始属性 [英] Injecting a primitive property in a base class with Castle Windsor
问题描述
我有以下接口定义:
public interface ICommandHandler
{
ILogger Logger {得到;组; }
bool SendAsync {get;组;我有多个实现实现 ICommandHandler
$ code>并需要解决。当Castle Windows自动注入 Logger
属性时,当注入 ILogger
时,我找不到配置方式创建新实例时,Windsor将要设置为 SendAsync
属性。
更新
命令处理程序实现从基本界面继承的通用接口:
public interface ICommandHandler< TCommand> :ICommandHandler
其中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 (新的{sendAsync = true})
关于更新的问题,它看到你需要的是沿着以下行:
container.Register(AllTypes.FromThisAssembly()
.BasedOn(typeof(ICommandHandler<>))
.WithService.Base()
.Configure(c => c.DependsOn(new {sendAsync = true})
.Lifestyle.Transient));
这样一来你就可以注册和配置所有的处理程序,我不得不回去把它们添加到注册码。
我建议阅读文档,因为基于约定的注册API比这更多。
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. 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.
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?
解决方案 .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.
这篇关于使用Castle Windsor在基类中注入原始属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!