I have my implementation of ControllerFactory for handling constructor injection in controllers, however I want to use default way to resolve controllers in case if some of them are not registered:
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IWindsorContainer _container;
public WindsorControllerFactory(IWindsorContainer container)
{
_container = container;
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType != null)
{
try
{
return _container.Resolve(controllerType) as IController;
}
catch(Exception ex)
{
return base.GetControllerInstance(requestContext, controllerType);
}
}
else
{
return base.GetControllerInstance(requestContext, controllerType);
}
}
}
It works, but I really would like to avoid exceptions
Use container.Kernel.HasComponent(Type type)
:
bool isRegistered;
var container = new WindsorContainer();
isRegistered = container.Kernel.HasComponent(typeof(IFoo));
Console.WriteLine(isRegistered);
container.Register(Component.For<IFoo>().ImplementedBy<Foo>());
isRegistered = container.Kernel.HasComponent(typeof(IFoo));
Console.WriteLine(isRegistered);
This outputs:
False
True