Implementing DI with Native IOC in ASP.NET Core
In this recipe, we will learn in a simple way how to register, resolve, and give a life cycle to an abstraction in ASP.NET Core.
Getting ready
Dependency injection is now native in ASP.NET Core.
Importing Microsoft.Extensions.DependencyInjectionin project.json (formerly web.config), and by a using statement in Startup.cs (formerly global.asax), we can use this internal component to manage DI for our application, represented by the IServiceProvider interface.
How to do it...
We will inject the ServiceProducts class by using a constructor in the HomeController of an ASP.NET MVC 6 application:
- First, we create a class called
Product:

- We then create an interface called
IProductService:

- Next, we create a class called
ProductService:

- Let's use
ProductServicein ourHomeController, creating an instance ofProductServiceby injecting theProductServiceabstraction in theHomeControllerconstructor.ProductServicewill be available for the wholeHomeController...