1

We have a bunch of controllers we register with Castle via a BasedOn query. One of these controllers we would like to add an extra configuration dependency on. It is possible that we could register that parameter with all controllers. The following code is how we worked around the problem, but I would like to know if there is a more elegant/built in solution.

public class ControllersInstaller : IWindsorInstaller
{
    private readonly IAppConfig _appConfig = new AppConfig();
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(FindControllers().Configure(ConfigureComponentRegistration).LifestyleTransient());

        container.Register(Component.For<BarController>()
            .LifestyleTransient()
            .DependsOn(Dependency.OnValue("faxNumber", 
            _appConfig.GetAppSetting<string>("FaxNumber"))));
    }

    private void ConfigureComponentRegistration(ComponentRegistration obj)
    {
        obj.LifestyleTransient();
    }

    private BasedOnDescriptor FindControllers()
    {
        return Classes.FromThisAssembly()
            .BasedOn<IController>()
            .If(Component.IsInSameNamespaceAs<FooController>(true))
            .If(t => t.Name.EndsWith("Controller") && t.Name != "BarController")                
            .LifestyleTransient();
    }
}
wes.stueve
  • 95
  • 10
  • What is your concern about this solution? – Yacoub Massad Jun 30 '16 at 16:47
  • Not really any concern for this one instance, but, if I get more, I don't like having more conditions on the registration. I figured there is a way to override existing registrations or extend them, but couldn't find a good way. – wes.stueve Jul 01 '16 at 18:42

2 Answers2

2

I would suggest that you simply override the existing registration with a more specific version, that has necessary dependency from _appConfig. Therefor, you don't have to use this filter:

t.Name != "BarController"

Check out my answer here, for how to override existing components: https://stackoverflow.com/a/37832194/644891

Community
  • 1
  • 1
andree
  • 3,084
  • 9
  • 34
  • 42
  • This is pretty much what I was looking for and couldn't find the answer because of the noise on the google terms. Thanks so much! – wes.stueve Jul 01 '16 at 18:43
0

You totally can, just use ConfigureFor<> method.

container.Register(FindAllControllers()
   .ConfigureFor<BarController>(x =>
      x.DependsOn(Dependency.OnAppSettingsValue("faxNumber"))
   )
);

As I side note, I'm not sure why your example specifies the lifetime three times. It's sufficient to do it once.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115