简体   繁体   中英

Are there better ways to instantiate interfaces using dependency injection?

I find myself doing lots of this;

IService service1;
IService service2;

SomeController(IService1 service1, IService2 service2)
{
    this.service1 = service1;
    this.service2 = service2;
}

What would be alternatives to injecting and instantiating interfaces like this? It's starting to feel redundant - but, I'm a noob and don't yet know quite why or how.

There are four types of DI in C#

  • Constructor Injection
  • Setter or property Injection
  • Method Injection
  • Service Locator Injection

You have used the Constructor Injection already which is the most common way of doing it, the other alternatives are not necessarily better, but you can choice which one is more convenient for you to working with, you can find an example of each one here: https://www.c-sharpcorner.com/article/understanding-the-dependency-injection-using-constructor-property-and-method-in/

You can also find a good explanation of When to use Property Dependency Injection over Constructor Injection and vice versa? here :

The Constructor Dependency Injection in C# is the standard for dependency injection. It ensures that all the dependency objects are initialized before we are going to invoke any methods or properties of the dependency object, as a result, it avoids the null reference exceptions.

The Setter/Property Dependency Injection in C# is rarely used in real-time applications. For example, if I have a class which has several methods but those methods does not depend on any other objects. Now I need to create a new method within the same class but that new method now depends on another object. If we use the constructor dependency injection here, then we need to change all the existing constructor calls where we created this class object. This can be a very difficult task if the project is a big one. Hence, in such scenarios, the Setter or Property Dependency Injection can be a good choice.

You are using the "best" way to inject dependencies that is available in.Net at the moment, constructor injection. This is also the recommended way to inject dependencies by Mark Seemann in his book Dependency Injection in.Net :

CONSTRUCTOR INJECTION should be your default choice for DI. It addresses the most common scenario where a class requires one or more DEPENDENCIES, and no reasonable LOCAL DEFAULTS are available.

Unless you have no other good reason to prefer another way of dependency injection, this should be the way you inject your dependencies.

Yes it may seem a bit redundant, but it is easy to read and every developer that has worked with dependency injection before should be very familiar with code that looks like this so you should not worry about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM