0

I need some help understanding the Dependency Injection. For instance, if i create a MVC projekt with a Person class and a PeopleController. In this case, will the PeopleController be using dependency injection, because the PeopleController class depends on the Controller-class, provided by Microsoft? Or am I not getting it right?

MKCPH93
  • 1
  • 1
  • 1
    *PeopleController class depends on the Controller*: no, it inherits from the Controller class, that is not dependency injection. Have you read https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0#overview-of-dependency-injection? – Peter Bons Jun 05 '21 at 14:15

1 Answers1

0

Dependency Injection is done at runtime by a dependency injection framework. .NET has one built in already as an extension.

Inheriting from a superclass or implementing an interface does not involve dependency injection at all, this is done statically in the language itself.

When a class is using dependency injection, it will either take in its dependencies as arguments in the constructor, which is the usual way of doing it, or by having its properties have values injected directly into them, which requires an [attribute] marking the property for dependency injection.

An example of this is the classic:

public PeopleController(ILogger logger) {...}

As you can see, this constructor for the PeopleController class is taking an argument of type ILogger, which is an interface. The dependency injection framework notices this and when instantiating this controller it will inject an instance of a class that implements the ILogger interface.

Usually you try to inject through interfaces, as this decouples your code and follows dependency inversion, which really is what dependency injection is all about. When talking about dependency injection it is usually called inversion of control.

Another example, let's say you've got this PeopleController, which wants an IPeopleService injected:

public class PeopleController 
{
  private readonly IPeopleService _peopleService; 

  public PeopleController(IPeopleService peopleService)
  {
      _peopleService = peopleService;
  }

  public void MethodThatUsesPeopleService() {
    _peopleService.makePeopleHappy();
  }
}

The IPeopleService interface:

public interface IPeopleService
{
  void makePeopleHappy();
}

Now, you've got a controller which wants a service injected, so you need to tell the dependency injection framework which class to associate with the interface.

You could create have two different implementations of the same interface:

public class GoodPeopleService : IPeopleService
{
  public void makePeopleHappy()
  {
    Console.WriteLine("People are now happy!");
  }
}

public class BadPeopleService : IPeopleService
{
  public void makePeopleHappy()
  {
    Console.WriteLine("People are NOT happy now HAHA!");
  }
}

And depending on which one of them you tell the dependency injection framework to associate with the IPeopleService interface, you'll get different behaviour out of your PeopleController.

As for details on how to register a class to an interface, check out Microsoft's Documentation. Here's a snippet of what it can look like:

services.AddScoped<IPeopleService, GoodPeopleService>();

See service lifetimes for info about which extension method to call when registering your services.

As a side note: the dependency injection is done at runtime, meaning there are no static guarantees of an instance of the requested interface or class existing at compile time. Though with the newer features of C#, there are dependency injection frameworks that aim to fix just that. (See Jab and StrongInject)

Martin Jonsson
  • 139
  • 2
  • 9