Dependency Injection Key Differences between .NET Core and ASP.NET

Leslie Ramírez
3 min readFeb 13, 2022

--

Hello there👋👋, this time I want to share with you some useful information about how things work in .NET framework and .NET Core, the key difference of implementing dependency injection in the ASP.NET MVC and .NET Core. So, let’s get started!

One quick definition of Dependency injection is a software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern.

A dependency is an object that another object depends on.

So, the first thing you need to know is,” Dependency injection isn’t built into ASP.NET MVC” you can add it to your project by an inversion of control (IOC) container. There are many solutions to implement like:

  • Autofac
  • Unity
  • Ninject Structure
  • MapAnd others.

This time we are going to implement using Autofac. Is an addictive IoC container for .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.

So how you structuring the application:

The idea behind inversion of control is that rather than a tie the classes in your application together and let classes “new up” their dependencies, you switch it around so dependencies are instead passed in during class construction.

Implementation of Dependency Injection in ASP.NET Framework

As I mentioned before we can not add DI in our ASP.NET without an Inversion of control container as Autofac, so let’s see how we can do it.

  1. The first step is adding the Autofac reference in your project.
  2. And create your container:

Note: You need to create, wire dependency and manage lifetime for a set of components when you are using an inversion of control solution, for the previous explained.

We need to register all of our components (classes) and expose their services (interfaces) so things can get wired up nicely. During application execution, you’ll need to make use of the components your registered.

Highlights of using containers:

  • The container itself is a lifetime scope, and you can technically just resolve things from the container. It is not recommended to resolve from the container directly, however.
  • When you resolve a component, depending on the instance scope you define, a new instance of the object gets created.
  • Some components may need to be disposed (like they implement IDisposable) — Autofac can handle disposing those components for you when the lifetime scope is disposed.

If you want a step-by-step tutorial, you can follow the official documentation of Autofac.

Implementation of Dependency Injection in NET Core Framework

.NET core expects that we use dependency injection, it required in order to bring support to some framework features.

The steps for implement the dependency injection:

  1. Registration of the dependency in a service container. .NET provides a built-in service container, IServiceProvider.
  2. Services are typically registered at the app’s start-up and appended to an IServiceCollection.
  3. Once all services are added, you use BuildServiceProvider to create the service container.

There are two ways of register our services.

  • The first one is registering our services into Program.cs this way:
  • We can also register groups of services with extension method:

For more implementation details you can go to the Dependency injection in ASP.NET Core documentation page.

Conclusions

Remember that dependency injections solve the following problems:

  • The use of an interface or base class to abstract the dependency implementation.
  • Injection of the service into the constructor of the class where it’s used. The framework takes on the responsibility of creating an instance of the dependency and disposing of it when it’s no longer needed.

Thanks for reading and have a wonderful day. ❤️ 🧡 💛 💚

--

--

Leslie Ramírez

I am Microsoft MVP in the award category: Developers technologies, a professional with several years in software development specifically in .NET technology.