Injecting the context
Dependency injection in the controller
Open the Controllers/MoviesController.cs
file and inject the context as shown
The constructor, with the help of Dependency Injection, receives an object of MvcMovieContext
. The database context is used in each of the CRUD methods in the controller.
.NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern.
Using the same technique of dependency injection, we can inject any other service into any controller which requires it, thereby keeping the usage and implementation separate we will look at other uses of dependency injection later.
Last updated