1

I'm preparing to use Dependency Injection using Microsoft.Extensions.DependencyInjection for the first time.

So I've been reading on it and apparently all examples I've seen regarding getting a concrete implementation of an interface involves a factory that will get injected either a func<IEnumerable<TInterface>> or IEnumerable<TInterface> and involves instancianting all implementations to search and return a specific implementation.

This doesn't sound too bad if there are very few implementations and all of them are light, which they are in all examples I've seen - but examples are not real use cases, but what if this is not the case?

Is this really the only option? Everytime I need to inject ImplementationN of TInterface it instanciates all implementations of TInterface?

537mfb
  • 1,374
  • 1
  • 16
  • 32
  • 1
    Are you looking for the equivalent of [Autofac's named services](https://docs.autofac.org/en/latest/advanced/keyed-services.html)? – gunr2171 Jan 11 '23 at 13:07
  • 1
    `services.AddScoped();` may help if you have default constructor or `services.AddScoped(c => new ImplementationN(param));` if you was to call any specific constructor by passing params – Mukul Keshari Jan 11 '23 at 13:43
  • If your service only works with one implementation of a dependency, then you either register that implementation with a different type, or build your own lookup method. – Jeremy Lakeman Jan 11 '23 at 23:57
  • @gunr2171 I will look into how `AutoFac` does it - that will certainly help. thanks – 537mfb Jan 12 '23 at 09:20
  • @MukulKeshari my question was'not about registering them but I appreciate the part about registering a func to retrieve an implementation. But your methods still require a single implementation of TInterface to work – 537mfb Jan 12 '23 at 09:25
  • @JeremyLakeman My question does mention choosing one from N implementations of `TInterface` - Someone marked this question as a possible duplicate of another whose answer was a `RepositoryResolver` and that looks promissing. Thanks for the comment – 537mfb Jan 12 '23 at 09:30

1 Answers1

3

Microsoft.Extensions.DependencyInjection Doesn't support this feature, you need to implement it by yourself with some sort of resolver class or by registering your consumer with factory method where you manually inject needed implementation.

Take a look at AutoFac keyed services, or Simple Injector conditional registration (thats what i use by myself, in my opinion its the best way) they allows you to create rules on registration time and determine what implementation injected in what consumers.

Also Microsoft.Extensions.DependencyInjection is featurless, and forces you to write alot of features included in other DI container libraries by yourself.

There is a good book about dependency injection "Dependency Injection Principles, Practices, and Patterns" by Steven van Deursen and Mark Seemann it have a good reasoning at the end why its a bad practice to use Microsoft DI container compared to Autofac and Simple Injector. I highly recommend you to read this book if you are interested in dependency injection.

Steven
  • 166,672
  • 24
  • 332
  • 435
Kliment Nechaev
  • 480
  • 1
  • 6
  • 13
  • being featurless is partly why I'm going with it, this is more of a learning experiment as I never (knowngly) used dependency injection. Well, I would create the dependencies myself and pass them on to the constructor. Looking at `AutoFac` and `Simple Injector` might help me with this leaning experience - thanks – 537mfb Jan 12 '23 at 09:19
  • 1
    the `RepositoryResolver` from the answer in https://stackoverflow.com/questions/39072001/dependency-injection-resolving-by-name is very close to what I am asking and looks like it's what `AutoFac` and `Simple Inject` use – 537mfb Jan 12 '23 at 09:32