How to get type of dependency owner when using Windsor Castle container?

Viewed 116

Consider we have multiple services, each of them uses ILoggerFactory interface creating own instance of logger next way:

public class Foo
{
    private readonly ILogger _logger;

    public Foo(ILoggerFactory logger)
    {
        _logger = loggerFactory.CreateForContext(this.GetType());
    }
}

Also consider that we use Windsor Castle for dependency injection, so we have a registration like that:

container.Register(Component
    .For<ILoggerFactory>()
    .LifestyleSingleton()
    .Instance(new LoggerFactory());

Of course, we dont want to repeat CreateForContext() in every class. So, I'm trying to write something like that:

container.Register(Component
    .For<ILogger>()
    .LifestyleTransient()
    .UsingFactoryMethod((kernel,model,context) => 
        kernel.Resolve<ILoggerFactory>().CreateForContext(????));

+

public class Foo
{
    private readonly ILogger _logger;

    public Foo(ILogger logger)
    {
        _logger = logger;
    }
}

But there is a problem - I can't find how to get type of dependency owner. Is there any way to get it or another way to do same things?

Update I implemented ISubDependencyResolver, but it doesn't work with late bound components. So, if you use a lot registrations via UsingFactoryMethod for example, it isn't a great solution.

0 Answers
Related