In this article, we are going to discuss an important concept of ASP.NET Core MVC – Dependency Injection.

If you’ve missed some of the previous articles in the series we recommend visiting the series page: ASP.NET Core MVC Series.

To download this article’s source code visit: Dependency Injection in ASP.NET Core MVC.

We have divided this article into the following sections:

So, let’s get down to it.

Dependency Inversion and Dependency Injection

Dependency Inversion is one of the core software design principles which we use to create modules that are loosely coupled. While creating applications, we can achieve this using a technique called Dependency Injection. Dependency Injection (DI) is the method of injecting the dependent modules into our classes.

We have discussed this in detail in one of our other article Dependency Inversion Principle. There, we discussed the concept of Dependency Injection and how to implement it.

So, in this section, we are going to look at the support for Dependency Injection in an ASP.NET Core MVC application.

Injecting Dependencies into Controllers

ASP.NET Core supports Dependency Injection(DI) between classes and their dependencies. MVC Controllers request dependencies explicitly via constructors. Furthermore, ASP.NET Core has built-in support for dependency injection, hence making the application easier to test and maintain.

We add services as a constructor parameter and the runtime resolves the service from the service container. We typically define services using interfaces.

When we implement a repository pattern in ASP.NET Core MVC application, we make use of Dependency Injection in our controllers. We have explained how to implement a simple data repository in the article section Implementing a simple data repository.

Let’s create an ASP.NET Core MVC application and implement a simple data repository as described in the article.

First of all, let’s create an interface IDataRepository:

Then let’s create a class EmployeeManager implementing the IDataRepository interface:

Next step is to add the service to the service container. We need to do that in the ConfigureServices() method in the Startup.cs class:

By doing so, we have configured the repository using Dependency Injection.

Next, let’s create the EmployeeController with the Index() action method to get the list of all employees:

Here, we first declare a _dataRepository variable of type IDataRepository<Employee>. Later, we inject it through the constructor.

We can also inject a service directly into an action method without using constructor injection. We can use the [FromServices] attribute for that:

TheFromServices attribute specifies that an action parameter should be bound using the request services.

Great. We have learned how to use Dependency Injection to provide dependencies into a Controller.

Now, let’s look at how to Inject dependencies into Views.

Injecting Dependencies into Views

ASP.NET Core supports injecting dependencies into Views. 

When to Inject Dependencies into Views

Injecting services into views is a deviation from the MVC concept. But in some cases, we may have view-specific services which return the data that we use only for populating the view elements. An example is a service which gives a set of values that we need to display in a list control. In such scenarios, we can inject services directly into views. View injection can be useful to populate options in UI elements, such as dropdown lists.

Consider a form to create a book that includes options for specifying a genre. Rendering the data using a standard MVC approach would require the controller to request data access services for this set of options and then populate a Model or ViewBag with the set of options to be bound.

An alternative approach is to inject the services directly into a view. This approach minimizes the amount of code required by the controller as we move the view element construction logic into the view itself.

How to Inject services into Views

We can inject a service into a view using the @inject directive. @inject adds a property to our view and initialize it using DI:

Let’s create a controller action method to display a book creation form:

Then let’s create a Book model class:

For the next step, let’s define a BooksLookupService for supplying the list data for Genres:

Then let’s create a view and inject an instance of BooksLookupService into it:

This will supply the list values into the view.

As the last step, we need to register the types that we request through dependency injection in Startup.ConfigureServices(). If a type is unregistered, it throws a runtime exception.

That’s it. Now let’s run the application and navigate to Create Books form:

create books screen

We can see that the list of Genres is populated by getting the values from the BooksLookupService.

Excellent, we have learned how to inject a dependency directly into the view.

Conclusion

In this article we have learned the following topics:

  • The principle of Dependency Inversion and how to achieve it using Dependency Injection
  • Injecting dependencies into Controllers
  • Injecting dependencies into Views of an ASP.NET Core MVC application

In the next part of this series, we are going to learn about unit testing in ASP.NET Core MVC.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.