我總是發現在單元測試中學習如何使用開源項目的最佳方式。 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
多謝多謝了精彩的響應!我將不得不深入研究它。 WindsorContaienr上的.Register方法來自哪裏?我沒有通過我的intellisense看到它。它是一種擴展方法嗎? – 2008-09-18 15:26:13