2010-08-01 105 views
4

在代碼中,我可以做一些這樣的事在註冊裝配所有類型:溫莎城堡:使用配置文件

container.Register(AllTypes.FromAssemblyNamed("AssemblyName")); 

可以使用配置文件「Windsor.Config」我做同樣的事情???

+4

爲什麼?你爲什麼不想在代碼中做到這一點? – 2010-08-02 00:21:03

+0

好吧,我正在處理一個涉及超過30個模塊的巨大項目。 每個模塊負責爲相應的子系統提供演示UI。 每個子系統都有一個:域,存儲庫,核心,應用程序服務..等 和所有這些程序集必須在主容器中註冊。 所以要麼我必須在模塊的代碼(這意味着我應該將它們全部引用到模塊) 或我必須修改主windsor配置文件(這是討厭),每次我需要註冊一個新組件。 – 2010-08-02 08:29:36

回答

18

回覆您的評論。

還有第三種方法(在Windsor 2.5中,目前處於測試版2 - 最終預計將很快推出)。

你可以有每個模塊的引用溫莎,並且每個模塊都有自己的一套Installers.

比你可以使用新目錄的掃描能力,所有這些組件安裝組件:

// In your root assembly 
var container = new WindsorContainer(); 
container.Install( 
    FromAssembly.This(), 
    FromAssembly.InDirectory(new AssemblyFilter("Modules")), 
    Configuration.FromAppConfig() 
) 

另外,如果組件具有相同的結構,則還可以在單​​個安裝程序中註冊來自多個組件的組件。 See more here.

container.Register(
    AllTypes.FromAssemblyInDirectory(new AssemblyFilter("Modules")) 
     .Where(t=>t.Namespace.EndsWith(".Services")) 
     .WithService.DefaultInterface() 
); 
+0

非常酷!我不知道有關Install()的重載。我通常先解決所有安裝程序,然後分別調用Install()。 – Ryan 2010-08-03 01:51:22

+0

Krzysztof:你是我的英雄:) 不幸的是我現在不能投票。 我會投15票。 – 2010-08-03 14:24:09

1

我非常肯定,只有使用流暢的配置API,才能爲應用程序設置約定,以便在創建新組件時不需要單獨註冊它們,如示例所示。

1

你可以寫一個簡單的工具來做到這一點,如:

AllTypesConfig.xml

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <facilities> 
    <facility id="alltypes"> 
     <assemblies> 
     <item>Castle.Core</item> 
     </assemblies> 
    </facility> 
    </facilities> 
</configuration> 

代碼:

public class AllTypesFacility : AbstractFacility { 
    protected override void Init() { 
     var asmList = FacilityConfig.Children["assemblies"].Children; 
     foreach (var asm in asmList) 
      Kernel.Register(AllTypes.FromAssemblyNamed(asm.Value).Pick()); 
    } 
} 


var container = new WindsorContainer(@"..\..\AllTypesConfig.xml"); 
container.AddFacility("alltypes", new AllTypesFacility()); 
container.Resolve<NullLogger>(); 

如果你需要更多的靈活性,它會越來越難以用XML表示流暢的配置。