Autofac: select implementation of a service based on what can be constructed

Viewed 58

Let's say I have an IService and two implementations ServiceA and ServiceB. Then let's assume that ServiceA has ClassA parameter in its constructor (among others) and ServiceB has ClassB parameter. I would then like to:

  1. Register services as follow:
containerBuilder.RegisterType<ServiceA>().AsImplementedInterfaces();
containerBuilder.RegisterType<ServiceB>().AsImplementedInterfaces();
  1. Register an instance of either ClassA or ClassB (this is decided in runtime).
  2. When some other component wants IService, autofac will provide one basing one what can be constructed, i.e. if it has a ClassA object in repository, it will construct and provide ServiceA etc.

How can I achieve that? Note that ClassA and ClassB do not share common interface.

0 Answers
Related