Csharp/C#教程:使用Castle Windsor在基类中注入原始属性分享


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

我有以下接口定义:

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

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

UPDATE

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

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

这是我的配置:

 var container = new WindsorContainer(); container.Register(Component .For<ICommandHandler>() .ImplementedBy()); container.Register(Component .For<ICommandHandler>() .ImplementedBy()); container.Register(Component .For<ICommandHandler>() .ImplementedBy()); 

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

上述就是C#学习教程:使用Castle Windsor在基类中注入原始属性分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/957407.html

(0)
上一篇 2021年11月21日
下一篇 2021年11月21日

精彩推荐