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.
- 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.
- Next, let's inject this repository in ...