Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are components removed with Castle 3.0?

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".

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

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

}

How can I remove components with Castle 3.0?

like image 847
Antineutrino Avatar asked Feb 29 '12 14:02

Antineutrino


1 Answers

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.

like image 91
eouw0o83hf Avatar answered Sep 23 '22 03:09

eouw0o83hf