2008-09-18 46 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