Dependency Injection in .NET Core

dependency Injection in .NET Core

I was thinking about the topic for my first article here and I reminded myself that I gave a prelection some time ago on creating services as part of the .NET Core course, which we organized with my science club EKA.NET for students of our university. Today’s article will focus on something that is a key element of creating and operating services in .NET Core technology, or Dependency Injection.

In writing this article I assume that the reader knows the basics of programming in C#.

What is Dependency Injection?

The definition from Wikipedia says that it’s a design and software architecture pattern to remove direct relationships between components in favour of a plug-in architecture. A more practical definition is that it’s a mechanism that allows providing dependencies necessary for the correct operation of a given object.

What is PLUG-IN architecture then?

It’s defined as an additional module to a computer program that extends the capabilities of the output product.

Implementation ways of Dependency Injection

There are three ways of injecting dependencies:

  • Constructor Injection
  • Setter Injection
  • Method Injection

So now I will write to you more about these methods and try to explain them with examples.

Constructor Injection

This method is described as the simplest and most commonly used. As the name suggests, it consists of injecting dependencies by giving them in the constructor.

public class MyClass
{
  private IMyService _myService;
  public MyClass(IMyService myService)
  {
    _myService = myService;
  }
}

And then, when we create a new object of our class, it looks like this:

MyService myService = new MyService();
MyClass myClass = new MyClass(myService);

This method ensures that the object we create is always consistent because dependencies are forced. On the other hand, it may be a limitation that these dependencies cannot be changed.

Setter Injection

With this method, when calling a class, you must specify which service from this service will be used as the value assigned in the setter.

public class MyClass
{
  private IMyService _myService;
  private MyParticularService myParticularService;
  public IMyService MyService
  {
    get
    {
      return _myService;
    }
    set
    {
      _myService = value;
    }
  }
  public MyClass()
  {
    _myService = new MyParticularService();
  } 
}

In this case, creating a new object is as follows:

MyAnotherSpecificService myAnotherSpecificService = new MyAnotherSpecificService();
MyClass myClass = new MyClass()
{
   MyService = myAnotherSpecificService
}

This method is in a way the opposite of the previous one, because it provides flexibility and, on the other hand, it doesn’t require certain dependencies.

Sign up for the newsletter to keep up to date with new articles!

Method Injection

Here, as the name itself indicates, when calling the method, it’s necessary to specify which service is one of the parameters.

public class MyClass
{
  private IMyService _myService;
  private IMySecondService _mySecondService;
  public MyClass()
  {
     _myService = new MyService();
     _mySecondService = new MySecondService();
  }
        
  public void MyMethod(string value, string value2, IMyService myService)
  {
     myService.serviceMethod(value, value2);
  }
}

And when creating a new class object, it looks like this:

MyService myService = new MyService();
MyClass myClass = new MyClass();
myClass.MyMethod(value, value2, myService);

This approach also provides flexibility but causes difficulties with complex methods.

DI in ASP.NET Core

ASP.NET Core is designed from scratch to support Dependency Injection. It allows us to register application services in ConfigureServices method of Startup.cs. This method contains an IServiceCollection type parameter that is used to register application services and looks like this:

Summary

Dependency injection is an inseparable element of web programming and services. I explained it on the example of .NET Core, but it is also used in other technologies or languages. I’ll tell you more about the .NET Core services in another article, but now I wanted to mention it briefly to show you what is the use of injecting dependencies in practice. I hope I wrote it clearly and it will be useful for you. Let me know what you think about this article! If something is unclear or the article contains some error, please write in a comment!

2 Comments

  1. Mateusz Rus
    February 15, 2020

    Good luck!

    Best regards, Mateusz.

    Reply
    1. bushdev
      February 19, 2020

      Thanks a lot, Mateusz!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top