There are two things that you have to do to create an overriding instance:
- Assign it a unique name
- Call the
IsDefault
method
So to get the example to work:
this.WindsorContainer.Register(
Component.For<IMediaPlayerProxyFactory>()
.Instance(mockMediaPlayerProxyFactory)
.IsDefault()
.Named("OverridingFactory")
);
Because I plan to use this overriding patten in many tests, I’ve created my own extension method:
public static class TestWindsorExtensions
{
public static ComponentRegistration<T> OverridesExistingRegistration<T>(
this ComponentRegistration<T> componentRegistration
) where T : class
{
return componentRegistration
.Named(Guid.NewGuid().ToString())
.IsDefault();
}
}
Now the example can be simplified to:
this.WindsorContainer.Register(
Component.For<IMediaPlayerProxyFactory>()
.Instance(mockMediaPlayerProxyFactory)
.OverridesExistingRegistration()
);
Later Edit
Version 3.1 introduces the IsFallback
method. If I register all my initial components with IsFallback
, then any new registrations will automatically override these initial registrations. I would have gone down that path if the functionality was available at the time.
https://github.com/castleproject/Windsor/blob/master/docs/whats-new-3.1.md#fallback-components