Autofac with multiple implementations of the same interface

Autofac implicitly supports this by default via the use of IEnumerable<T>. Instead of having your depending class’s constructor take in a single instance of T, you make it take in an instance of IEnumerable<T> that will contain every T registered:

public interface IMessageHandler
{
    void HandleMessage(Message m);
}

public class MessageProcessor
{
  private IEnumerable<IMessageHandler> _handlers;

  public MessageProcessor(IEnumerable<IMessageHandler> handlers)
  {
      _handlers = handlers;
  }

  public void ProcessMessage(Message m)
  {
      foreach (var handler in _handlers)
      {
          handler.HandleMessage(m);
      }
  }
}

Then in your registration, simply add multiple implementations of T:

var builder = new ContainerBuilder();
builder.RegisterType<FirstHandler>().As<IMessageHandler>();
builder.RegisterType<SecondHandler>().As<IMessageHandler>();
builder.RegisterType<ThirdHandler>().As<IMessageHandler>();
builder.RegisterType<MessageProcessor>();

When MessageProcessor is instantiated, the IEnumerable it receives will contain three items as per the above registrations against IMessageHandler.

You can read more about this on my blog.

Leave a Comment

tech