簡體   English   中英

在Castle Windsor中向多個服務注冊多個組件

[英]Register multiple components with multiple services in Castle Windsor

我在兩個單獨的程序集中有一些服務和用於它們的接口,一個用於接口,另一個用於實現。

我正在嘗試使用溫莎城堡進行注冊。 它可以像下面這樣完美地工作:

 container.Register(Component
.For(IService1)
.ImplementedBy(Service1)
.LifeStyle.Transient());

 container.Register(Component
.For(IService2)
.ImplementedBy(Service2)
.LifeStyle.Transient());

這里的事情是...可以一次注冊所有組件嗎? 無需一一注冊。 我已經看到有可能將多個接口注冊到一個簡單的組件,或者為單個接口提供多個實現,但是我還沒有看到是否有可能在一個程序集中注冊多個接口並在另一個程序集中注冊多個實現。

我不知道這是否有任何相關性,但是所有接口都具有相同的基礎(在這種情況下為IService)。

非常感謝。

您正在尋找的是按約定注冊組件 基本上,您要做的是聲明Castle應該考慮哪些實現以及與這些實現相關聯的服務。

container.Register(
    // we want all concrete classes in this assembly
    Classes.FromThisAssembly()
    // but we filter them to keep only the ones in a specific namespace
    .InSameNamespaceAs<RootComponent>()
    // we register thoses classes to interfaces that match the classes names
    .WithService.DefaultInterfaces()
    // setting the lifestyles for all components in this batch
    .LifestyleTransient()
);

基於約定的注冊還有許多其他選項,最好的方法是更長久地瀏覽鏈接的頁面。

暫無
暫無

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

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