我一直发现查看单元测试是学习如何使用开源项目的最佳方式。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 但同样,infor 的最佳位置是单元测试。这是使用 binsor 的代码示例:
for type in AllTypesBased of IController("Company.Web.Controller"):
component type
这两行将把继承 IController 接口的类型注册到容器中:D