簡體   English   中英

使用Castle Windsor在基類中注入原始屬性

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

我有以下接口定義:

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

我有多個實現ICommandHandler實現,需要解決。 當Castle Windows自動注入Logger屬性時,當注入ILogger時,我無法找到一種方法來配置在創建新實例期間Windsor將SendAsync屬性設置為true。

UPDATE

命令處理程序實現從基接口繼承的通用接口:

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有什么方法可以做到這一點?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM