Is it possible in Castle Windsor to inject a named component without registering it explicitly?

19 Views Asked by At

(I use logging as an example here, but it could be any type.)

I have a multiple components that would like to write to the same log file. I know that I can resolve ILoggerFactory and get the same logger in different components that way:

public MyClass(ILoggerFactory factory)
{
   myLogger = factory.GetLogger("MagicString");
}

I'd rather just specify the shared logger name in the registration of MyClass via a dependency and skip the whole process of injecting a factory and calling it. Under the hood, Windsor is already doing this in the process of naming components, so is there some way I can leverage that? The issue I see is that I would have to register a concrete instance with a specific name, when I'd rather not do that and let the IOC build it for me.

container.Register
(
    Component.For<ILogger>()
        .Instance(new Logger("magicstring"))
        .Named("magicstring"),

    Component.For<IMyClass>()
          .ImplementedBy<MyClass>()
          .DependsOn
          (
              Dependency.OnComponent(typeof(ILogger), "magicstring");
          )      
)

There's a bunch of things I don't like about this. Is my only option to implement my own Factory type that maps names to shared instances?

0

There are 0 best solutions below