2

I have a service interface, lets say IAddonServiceX which is implemented by many, but not all addons to a system. I want to have an IEnumerable so that I could iterate and execute methods for every addon that registered this service? Ho can this be done with autofac?

Thomas Maierhofer
  • 2,665
  • 18
  • 33
  • What have you tried? Autofac has implicit support for it, so you just need register the implementation with `builder.RegisterType.As(); builder.RegisterType.As()...`, and where you want to use it you just depend on `IEnumberable`... – nemesv Nov 06 '13 at 06:42
  • Is it not true that builder.RegisterType.As() overrides the registration builder.RegisterType.As();? so I can't have access to both of them? – Thomas Maierhofer Nov 06 '13 at 07:05
  • It only overrides which the default instance if you write `container.Resolve` you will get `Impl2` but if you write `container.Resolve>` you will get back both Impl1 and Impl2 – nemesv Nov 06 '13 at 08:20
  • Ok this is it, may you post it as answer? – Thomas Maierhofer Nov 14 '13 at 09:03

1 Answers1

7

Autofac has implicit support for it, so you just need register your implementations with

builder.RegisterType<Impl1>.As<IAddonServiceX>();    
builder.RegisterType<Impl2>.As<IAddonServiceX>();

And when you are resolving an IEnumberable<IAddonServiceX> with container.Resolve<IEnumberable<IAddonServiceX>> or using IEnumberable<IAddonServiceX> in your constructor then Autofac will provide you all the implementations.

nemesv
  • 138,284
  • 16
  • 416
  • 359