如何组件与城堡3.0去掉? [英] How are components removed with Castle 3.0?
问题描述
我有IWindsorContaner它存在的整个应用程序生命周期。对于单元测试也可以根据自己的类型登记嘲笑/存根等。当测试完成与夹具被设置在测试满心欢喜的注册组件是去除与所谓的取消注册的自创建方法
I have IWindsorContaner which exists for the whole application lifetime. For Unittests it is possible to register mocks/stubs, etc. under their Type. When the test is finished and the fixture is disposed the registered components in forthe test are remove with a self created method called "Unregister".
现在,我想更新到最新版本的城堡是3.0。按照3.0发行说明像
Now, I want to update to the latest Castle version which is 3.0. According to the 3.0 release notes something like
public void Unregister(string contextName, string registrationName)
{
IWindsorContainer context = GetOrCreateContext(contextName);
context.Kernel.RemoveComponent(registrationName);
}
是不可能的了,因为IKernel.RemoveComponent方法已被删除。说明解决这个问题是不是真的足够了(尝试利用IHandlerSelectors)。
is not possible anymore, because the IKernel.RemoveComponent method has been removed. The description to fix this is not really sufficient ("Try utilizing IHandlerSelectors.").
夹具的简化版本,我使用的单元测试代码:
A simplified version of the fixture I use for unittests:
public sealed class DependencyInjectionFixture : IDisposable
{
private Stack<Type> registeredTypes = new Stack<Type>();
// Registering of mocks/stubs, etc
public void RegisterSingleton<T>(T singleton, string objectName)
{
registeredTypes.Push(typeof(T));
IWindsorContainer context = GetOrCreateContext(contextName);
context.Register(Component.For(typeof(T))
.Named(objectName)
.Instance(singleton)
.LifeStyle.Singleton);
}
// Called when tests ends
public void Dispose()
{
IWindsorContainer context = GetOrCreateContext(contextName);
while (registeredTypes.Count > 0)
context.Kernel.RemoveComponent(CSApplicationContext.GetRegistrationNameFor(registeredTypes.Pop()));
}
}
如何删除的组件与城堡3.0?
How can I remove components with Castle 3.0?
推荐答案
,只需要创建一个新的 IWindsorContainer
并绑定该到任何 GetOrCreateContext
的核对。然后,你就会有一个全新的容器,没有任何束缚吧。
Instead of trying to remove all components, just create a new IWindsorContainer
and bind that to whatever GetOrCreateContext
is checking against. Then you'll have a fresh new container that has nothing bound to it.
这篇关于如何组件与城堡3.0去掉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!