Skip to Main Content
ASP.NET Core MVC 2.0 Cookbook
book

ASP.NET Core MVC 2.0 Cookbook

by Jason De Oliveira, Engin Polat, Stephane Belkheraz
February 2018
Intermediate to advanced content levelIntermediate to advanced
668 pages
15h 18m
English
Packt Publishing
Content preview from ASP.NET Core MVC 2.0 Cookbook

How to do it...

We've already talked about injecting dependencies with ASP.NET Core in Chapter 5, SOLID Principles, Inversion of Control, and Dependency Injection. We learned that the IoC mechanism is internal to ASP.NET Core. It's done by a constructor, and its life cycle has to be configured in the Configure method in Startup.cs. We'll make some adjustments, and everything will work automatically.

  1. First, let's see the repository to inject in the controller:
public interface IProductRepository{  int GetCountProducts();}public class ProductRepository : IProductRepository{  public int GetCountProducts()  {    return 10;  }}

As we can see, this repository has only one method that retrieves a list of strings.

  1. Next, let's inject this repository in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET Core and Vue.js

ASP.NET Core and Vue.js

Devlin Basilan Duldulao
Mastering ASP.NET Web API

Mastering ASP.NET Web API

Mithun Pattankar, Malendra Hurbuns

Publisher Resources

ISBN: 9781785886751Other