Dependency injection is a pattern in which an object receives the objects it depends on from an external composition mechanism instead of creating them itself. In ASP.NET Core, a controller can delegate dependency creation to the application handling the request. section 5.4.1
The injected object is the dependency. The application controls which implementation is supplied and can control its lifetime. This separation also makes testing easier because a controller can receive a fake context instead of constructing a real one. section 5.4.1 section 7.0
A practical ASP.NET Core arrangement has three parts:
- Register the dependency with the application service collection, allowing the framework to provide it when constructing the controller. section 5.4.1
- Replace production dependencies with mocks or fakes in tests when specific return values or isolated behavior are needed. section 7.0
Injecting an EF Core context
CatalogController accepts CatalogContext in its constructor and queries the stored _dbContext field. Program registration with builder.Services.AddDbContext<CatalogContext>() lets ASP.NET Core supply that dependency. A test can provide a fake context instead of allowing the controller to create one. section 5.4.1




















