Dependency Injection with ASP.NET Core 1.0

WEBINAR:On-Demand

Introduction

Dependency is an object that can be used and an injection is the passing of that dependency to a dependent object that would use it. 'Dependency Injection' is a design pattern and, as the name suggests, it is taking a dependency and injecting into a client or caller and it implements inversion of control. Using this pattern, a developer injects dependencies into objects instead of objects creating their own dependencies. This way, more flexibility and decoupling can be achieved.

Most of the applications using MVVM or MVC or other design patterns use DI and use constructor injection in many cases by passing the view model to view at the constructor level. That way, you can maintain the view's code free of any view model logic being coded in it. One more example is that you can decouple the data source connection dependency from the repository class that fetches data from database by passing a connection string as dependency, thus allowing data source to work with any repository by taking the connection string that is sent by caller. Here, the control is inverted by handing the responsibility of creating the connection from the repository class to the caller.

Most of the ASP.NET developers would have implemented DI using one or more of the following approaches:

Constructor injection: Injects the dependency at the constructor level.

Parameter injection: Injects the dependency at the parameter level.

Setter injection: Injects the dependency at the property level.

Interface injection: Injects the dependency at the interface level.

There is some confusion among developers about when to use which injection, and it depends on situation. If the injected object is used by more than one method in the class, the constructor level injection is the most appropriate because dependency is resolved one time at the constructor level but, if the dependency is used only by one method, parameter injection is preferred. But, in some situations, the class depends on another class but also can work without it. For example, in logging purposes, a class can work without logging, but it can write logs if you define dependencies as public properties. Dependency Injection is for objects that have complex dependencies. Controllers, services, adapters, and repositories are the kind of objects that can be added to DI. Static access to services and HttpContext are not good practice.

Sample constructor injection code can be as shown below, where the employee repository is injected at the constructor level and is resolved one time. However, it can be used many times in the class methods.

There are many dependency injection frameworks that automate resolving dependencies. This article focuses on dependency injection implementation details using ASP.NET Core 1.0. If you design an application to use DI, it's helpful to have a class dedicated to creating these classes with their associated dependencies. These are known as Inversion of Control (IoC) containers or Dependency Injection (DI) containers. A container is responsible for providing instances of types that are requested from it.

ASP.NET Core 1.0 has a built-in container that supports constructor injection by default.

ASP.NET's container refers to the types that are managed by ASP.NET Core's IoC container. A built-in container's services can be configured in the ConfigureServices method in the application's Startup class. You can use it everywhere DI with ASP.NET Core 1.0. The first step is to use Framework-Provided Services. ASP.NET Core 1.0 introduces a new method, called ConfigureServices, in the Startup class. Once you know the list of service you have to use in the application, you can define the IServiceCollection and then add those services the collection and input that to the ConfigureServices method.

The next step is to add the service to the DI container. An ASP.NET service can be configured with three main registration options, as shown in the next sections.

Transient

If you need to create a service each time it is requested, Transient lifetime is the best option. This mostly suits stateless services. Mapping is done between the interface and concrete type and, due to this new instance, is created every time it is requested. To register EmpService to Service Collection, you can use the following syntax.

Singleton

If the application requires singleton behavior, Singleton lifetime services can be used because the new instance is created the first time it is requested and then every subsequent request will use the same instance.

An instance also can be directly added to the services container. In such scenarios, this instance will be used for all subsequent requests for Singleton-scoped instances. Singleton service is lazy-loaded the first time it is requested, whereas the Instance service is created in ConfigureServices.

You can crate extension method to the IServiceCollection and register multiple services used in the application in MVC. All services needed by MVC middleware are added by the following extension.

If you want to replace the built-in container with their preferred container, the ConfigureServices signature can be changed to return IServiceProvider; a different container can be configured and returned. There are many IOC containers available for .NET; some of them are listed below:

Spring Framework: This framework offers Dependency Injection and a number of other capabilities.

PicoContainer: Tightly focused DI container framework.

HiveMind: Another DI container framework.

XWork: This is a command pattern framework that very effectively leverages Dependency Injection.

Conclusion

I hope this post gives you knowledge on the DI, a built-in container for DI in ASP.NET Core 1.0, how it works, and about using third-party containers with it.

About the Author

About the Author

Sudha Polapragada works as a Lead Developer with a Government Agency. She spent lots of her programming years working as an ASP.NET programmer and with Microsoft Technologies. She also specializes in Oracle and the Microsoft BI suite. In her free time, she explores features on various development platforms, including various database, front-end technologies, Big Data, and cloud technologies.