Reputation: 568
this is partially a duplication of the same question which has not been yet answered. See here: How can I override a component registered in Castle Windsor?
Since I cannot comment or post any answers to an existing questions I created this question again in the hope that somebody knows the answer to a seemingly basic and simple question.
Keep in mind that:
If Castle Windsor is not able to provide this simple functionality, what other container implementation would you recommend?
Upvotes: 6
Views: 6461
Reputation: 3224
Windsor works with convention that "first registration wins". But, if you have not SPECIFICALLY told it that this component will be overridden, it will throw an exception. So, there are 2 ways to allow existing component to be be overridden:
.IsDefault()
. This will override existing registration..IsFallback()
. This will allow component to be overridden later..Named("NewComponentName")
.I personally prefer .IsDefault()
and use this shorthand extension in my integration tests:
public static class WindsorContainerExtensions
{
public static void Override<TService>(this IWindsorContainer container, TService instance) where TService : class
{
container.Register(Component.For<TService>().Instance(instance).IsDefault());
}
}
Upvotes: 9
Reputation: 1561
Register your service like this, and then the other registration in your tests will overwrite it.
Container.Register(Component.For<ISomething>().ImplementedBy<RealSomething>().IsFallback());
Upvotes: 0
Reputation: 37299
I have no knowledge about other containers but Caslte so my answer is about Castle. If you want to replace what you can do is write an extension method to the IWindsorContainer that will remove and then add.
But I think you should rethink a bit your design:
Can you please explain more about the design and about the relevant classes?
Upvotes: 1
Reputation: 568
Answering to a second part of the question - what containers support registration overriding.
Ninject.
See Bind()/Unbind() methods.
I tried also Autofac but seems that the registration becomes frozen after it being built. So seems that it may not be also possible with Autofac.
Upvotes: 0