Question

I am working on an MVC 5 application that utilizes Castle Windsor. I've separated my code using areas to make it more manageable. I have also updated my instantiated class of IWindorInstaller to include an extra method that will find the controllers from this new area.

My code looks like this:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(FindStandardControllers());
    container.Register(FindAdminControllers());
}

private BasedOnDescriptor FindStandardControllers()
{   
    return Classes.FromThisAssembly()
        .BasedOn<IController>()
        .If(Component.IsInSameNamespaceAs<HomeController>())
        .If(t => t.Name.EndsWith("Controller"))
        .LifestyleTransient();
}

private BasedOnDescriptor FindAdminControllers()
{
    return Classes.FromThisAssembly()
        .BasedOn<IController>()
        .If(Component.IsInSameNamespaceAs<AdminController>())
        .If(t => t.Name.EndsWith("Controller"))
        .LifestyleTransient();
}

All of this works, but is there a better way to tell Windsor to automagically pick up all the controllers from each new area?

I'm especially concerned if I want to include similarly named controllers (eg: HomeController) in each area.

Was it helpful?

Solution

Since all your controllers are inherited from IController you shouldn't specify namespace and suffix.

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(
        Classes.FromThisAssembly()
            .BasedOn<IController>()
            .LifestyleTransient()
    );
}

In case you have controllers with the same name they will be in different namespaces and Windsor will resolve them correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top