Implementing DI with StructureMap
Now we will use a DI container called StructureMap to be the external component to resolve our dependencies.
In this recipe, we'll use another DI container, StructureMap. StructureMap is very well received by the community.
StructureMap is also very light, but it has almost every feature that you would expect from a complete DI container library.
How to do it...
We will create the object graph of our dependencies for our application.
To do that, we will create a ConfigureService method that returns an IServiceProvider type.
As in the first recipe, we will do the following:
- Inject the
ServiceProductsclass by using a constructor in theHomeControllerof an ASP.NET MVC 6 application. - Create a class called Product.
- Create an interface called
IProductService. - Create a class called
ProductService. - Modify
HomeControllerto add a constructor that will injectIProductService. - Create an action method called Products in
HomeController. - Create a view called
Products.cshtml. - Now...