使用 Castle Windsor 在基类中注入原始属性
我有以下接口定义:
public interface ICommandHandler
{
ILogger Logger { get; set; }
bool SendAsync { get; set; }
}
我有多个实现 ICommandHandler
的实现,需要解析。虽然 Castle Windows 自动注入 Logger
属性,但当注入 ILogger
时,我找不到将 SendAsync
属性配置为Windsor 在创建新实例期间将其设置为 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 来执行此操作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者对于更新问题的匿名类型
,您似乎需要的是以下几行:
这样您就可以一次性注册和配置所有处理程序,并且当您添加新处理程序时,您不必返回将它们添加到注册中代码。
我建议阅读文档,因为该约定还有更多内容基于注册的 API 比这还多。
or with anonymous type
regarding updated question, it seeems what you need is along the following lines:
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.