2008-09-18 27 views
4

從我讀過的關於Windsor/Microkernel的內容中,理論上可以做到使用代碼使用xml文件所做的一切。事實上 - 如果我錯了,請糾正我 - 看起來Windsor圖層的主要貢獻是爲microkernel已經可以做的事情添加xml配置。Windsor容器:在代碼和Xml中註冊東西

但是,最近我一直在努力研究如何在代碼中實現一些稍微複雜的功能(即how to assign a default constructor argument value)。現在,當我在生產版本中使用xml時,我正在爲我的測試註冊代碼組件,這會變得非常棘手。這不是由於他們的文檔的不幸狀態以及我能找到的唯一關注xml註冊的文章所致。

有沒有人知道列出如何在代碼中註冊東西的源代碼(最好是用xml等價物)?摒棄這一點,是否有人知道一個開源/示例項目,其中有非Castle使用的Castle Windsor/Microkernel?

回答

6

我總是發現在單元測試中學習如何使用開源項目的最佳方式。 Castle有一個流暢的界面,可以讓你在代碼中完成任何事情。從WindsorDotNet2Tests測試用例:

[Test] 
    public void ParentResolverIntercetorShouldNotAffectGenericComponentInterceptor() 
    { 
     WindsorContainer container = new WindsorContainer(); 
     container.AddComponent<MyInterceptor>(); 

     container.Register(
      Component.For<ISpecification>() 
       .ImplementedBy<MySpecification>() 
       .Interceptors(new InterceptorReference(typeof(MyInterceptor))) 
       .Anywhere 
      ); 
     container.AddComponent("repos", typeof(IRepository<>), typeof(TransientRepository<>)); 

     ISpecification specification = container.Resolve<ISpecification>(); 
     bool isProxy = specification.Repository.GetType().FullName.Contains("Proxy"); 
     Assert.IsFalse(isProxy); 
    } 

而對於更多的,檢查出的ComponentRegistrationTestCaseAllTypesTestCase

還爲做一個DSL,這是我的首選選項,因爲它確實簡化了操作,並提供很容易擴展。 DSL被稱爲Binsor,您可以在這裏閱讀更多信息:http://www.ayende.com/Blog/archive/7268.aspx但是,infor最好的地方是單元測試。這是什麼可能的binsor一個代碼示例:

for type in AllTypesBased of IController("Company.Web.Controller"): 
    component type 

這兩個行會永遠註冊鍵入繼承了一個IController接口放入容器:d

+0

多謝多謝了精彩的響應!我將不得不深入研究它。 WindsorContaienr上的.Register方法來自哪裏?我沒有通過我的intellisense看到它。它是一種擴展方法嗎? – 2008-09-18 15:26:13