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

发布于 2024-11-02 14:46:25 字数 1102 浏览 0 评论 0原文

我有以下接口定义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落日海湾 2024-11-09 14:46:25
.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 比这还多。

.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文