Windsor Container:在Code vs Xml中注册内容(Windsor Container: Registering things in Code vs Xml)

从我读到的关于Windsor / Microkernel的内容来看,理论上可以使用带代码的xml文件来完成所有工作。 事实上 - 如果我错了,请纠正我 - 似乎Windsor层的主要贡献是为Microkernel已经可以做的事情添加xml配置。

但是,我最近一直在努力寻找如何在代码中实现一些稍微复杂的功能(即如何分配默认的构造函数参数值 )。 现在,当我要在我的生产版本中使用xml时,我正在为我的测试注册代码中的组件,这就变得非常棘手。 对于他们的文档的不幸状态以及我能找到的唯一文章专注于xml注册这一事实并没有帮助。

有没有人知道一个源代码,它列出了如何在代码中注册事物(最好用xml等价物)? 除了存在之外,是否有人只是知道一个开源/示例项目,其中有很多非xml使用Castle Windsor / Microkernel?

From what I've read about Windsor/Microkernel it is in theory possible to do everything that you can do using xml files with code. As a matter of fact - and please correct me if I'm wrong - it seems like the major contribution of the Windsor layer is to add xml configuration for things Microkernel can already do.

However, I have been struggling lately with finding out how to implement some slightly more complicated functionality in code though (ie. how to assign a default constructor argument value). Now while I am going to use xml in my production release, I am registering components in code for my tests and this is getting to be quite problematic. This is not helped by the unfortunate state of their documentation and the fact that the only articles I can find focus on xml registration.

Does anybody know a source which lists how to register things in code (preferably with the xml equivalent)? Baring the existence of that, does anyone simply know of an open source/sample project where there is significant non-xml use of Castle Windsor/Microkernel?

最满意答案

我总是发现单元测试是学习如何使用开源项目的最佳方式。 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); }

有关更多信息,请查看ComponentRegistrationTestCase和AllTypesTestCase

还有一个用于执行此操作的DSL,这是我的首选选项,因为它真正简化了事物并提供了很多易于扩展的功能。 DSL被称为Binsor,你可以在这里阅读更多信息: http ://www.ayende.com/Blog/archive/7268.aspx但同样,最好的地方是单元测试。 这是binsor可能的代码示例:

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

这两行将注册将IController接口继承到容器中的类型:D

I've always found looking at the unit test the best way to learn how to use an open source project. Castle has a fluent interface that will allow you to do everything in code. From the WindsorDotNet2Tests test case:

[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); }

And for more, check out the the ComponentRegistrationTestCase and AllTypesTestCase

There is also a DSL for doing it, this is my prefered option, as it really simplifies things and offers alot of easy extensibility. The DSL is called Binsor, which you can read more about here: http://www.ayende.com/Blog/archive/7268.aspx But again, the best place for infor is the Unit Tests. This is a code example of whats possible with binsor:

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

Those two lines will register ever type that inherits the IController interface into the container :D

Windsor Container:在Code vs Xml中注册内容(Windsor Container: Registering things in Code vs Xml)

从我读到的关于Windsor / Microkernel的内容来看,理论上可以使用带代码的xml文件来完成所有工作。 事实上 - 如果我错了,请纠正我 - 似乎Windsor层的主要贡献是为Microkernel已经可以做的事情添加xml配置。

但是,我最近一直在努力寻找如何在代码中实现一些稍微复杂的功能(即如何分配默认的构造函数参数值 )。 现在,当我要在我的生产版本中使用xml时,我正在为我的测试注册代码中的组件,这就变得非常棘手。 对于他们的文档的不幸状态以及我能找到的唯一文章专注于xml注册这一事实并没有帮助。

有没有人知道一个源代码,它列出了如何在代码中注册事物(最好用xml等价物)? 除了存在之外,是否有人只是知道一个开源/示例项目,其中有很多非xml使用Castle Windsor / Microkernel?

From what I've read about Windsor/Microkernel it is in theory possible to do everything that you can do using xml files with code. As a matter of fact - and please correct me if I'm wrong - it seems like the major contribution of the Windsor layer is to add xml configuration for things Microkernel can already do.

However, I have been struggling lately with finding out how to implement some slightly more complicated functionality in code though (ie. how to assign a default constructor argument value). Now while I am going to use xml in my production release, I am registering components in code for my tests and this is getting to be quite problematic. This is not helped by the unfortunate state of their documentation and the fact that the only articles I can find focus on xml registration.

Does anybody know a source which lists how to register things in code (preferably with the xml equivalent)? Baring the existence of that, does anyone simply know of an open source/sample project where there is significant non-xml use of Castle Windsor/Microkernel?

最满意答案

我总是发现单元测试是学习如何使用开源项目的最佳方式。 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); }

有关更多信息,请查看ComponentRegistrationTestCase和AllTypesTestCase

还有一个用于执行此操作的DSL,这是我的首选选项,因为它真正简化了事物并提供了很多易于扩展的功能。 DSL被称为Binsor,你可以在这里阅读更多信息: http ://www.ayende.com/Blog/archive/7268.aspx但同样,最好的地方是单元测试。 这是binsor可能的代码示例:

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

这两行将注册将IController接口继承到容器中的类型:D

I've always found looking at the unit test the best way to learn how to use an open source project. Castle has a fluent interface that will allow you to do everything in code. From the WindsorDotNet2Tests test case:

[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); }

And for more, check out the the ComponentRegistrationTestCase and AllTypesTestCase

There is also a DSL for doing it, this is my prefered option, as it really simplifies things and offers alot of easy extensibility. The DSL is called Binsor, which you can read more about here: http://www.ayende.com/Blog/archive/7268.aspx But again, the best place for infor is the Unit Tests. This is a code example of whats possible with binsor:

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

Those two lines will register ever type that inherits the IController interface into the container :D