Say we have registered component in Castle Windsor:
Sometimes in Unit tests i need to remove registered component from Castle Windsor and to register new one. Prior to 3.0 version we can do this:
Аfter version 3.0 breaking changes we can't do this, because there is no RemoveComponent method in version 3.0.
But we can use the following approach:
Now we can use RegistrationHandlerSelector.UseBadResultRegistrator field to choose which component to use.
Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsGoodResult>());
Sometimes in Unit tests i need to remove registered component from Castle Windsor and to register new one. Prior to 3.0 version we can do this:
Container.Kernel.RemoveComponent(typeof(TestRegistratorWhichReturnsGoodResult).FullName);
Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsBadResult>());
Аfter version 3.0 breaking changes we can't do this, because there is no RemoveComponent method in version 3.0.
But we can use the following approach:
class RegistrationHandlerSelector :IHandlerSelector
{
public static bool UseBadResultRegistrator { private get; set; }
public bool HasOpinionAbout(string key, Type service)
{
return service == typeof(IRegistrator);
}
public IHandler SelectHandler(string key, Type service, IHandler[] handlers)
{
return handlers.First(x => UseBadResultRegistrator ? x.ComponentModel.Implementation == typeof (TestRegistratorWhichReturnsBadResult ) :
x.ComponentModel.Implementation == typeof (TestRegistratorWhichReturnsGoodResult ));
}
}
Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsGoodResult>());
Container.Register(Component.For<IRegistrator>().ImplementedBy<TestRegistratorWhichReturnsBadResult>());
Container.Kernel.AddHandlerSelector(new RegistrationHandlerSelector());
Now we can use RegistrationHandlerSelector.UseBadResultRegistrator field to choose which component to use.
No comments:
Post a Comment