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