Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Castle Windsor and The Composite design pattern

I have designed a telemetry logger for few separate platforms using the composite pattern

public interface ILogger
{
    void Log();
}

public class A : ILogger
{
    public void Log(...);
}

public class B : ILogger
{
    public void Log(...);
}

public class Many : ILogger
{
    private readonly List<ILogger> m_loggers;

    public Many(IEnumerable<ILogger> loggers)
    {
        m_loggers = loggers.ToList();
    }

    public void Log()
    {
        m_loggers.ForEach(c => c.Log());
    }
}

Now i want to be able to get an instance of "Many" from Windsor container but have encountered a few problems:

  • if all ILoggers are in the container how can i make sure i get the "Many" implementation and not "A" or "B" ?

  • I tried following this example Castle Windsor: How do I inject all implementations of interface into a ctor? and use container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel)); to register a class with IEnumerable dependancy but if that class also implements IComponent wont it create a circular dependency ?

Is what I'm attempting even possible ?

like image 379
MichaelB Avatar asked Oct 13 '15 04:10

MichaelB


1 Answers

First of all this is Composite Design Pattern not Component.

The way you do it in Castle Windsor in your case should look like this

container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel));
container.Register(Component.For<ILogger>().ImplementedBy<Many>());
container.Register(Component.For<ILogger>().ImplementedBy<A>());
container.Register(Component.For<ILogger>().ImplementedBy<B>());

This works because Castle Windsor have internal understanding of patterns like Composite or Decorator so no circular dependency will be created in this case. Just bare in mind that order of registration is important in this case.

More on registering different patterns in Castle Windsor can be found here.

like image 173
Krzysztof Branicki Avatar answered Sep 18 '22 11:09

Krzysztof Branicki