Injecting a Repository in a Controller
Explore how to update Startup.cs and controllers to inject repository patterns into ASP.NET Core applications. Understand how this approach simplifies data access, supports switching data sources like mock or SQL repositories, and reduces code duplication in controllers.
We'll cover the following...
We'll cover the following...
Updating Startup.cs
To allow repository injection into your controllers, the ConfigureServices() method inside Startup.cs needs to be updated by adding the following two lines of code.
One of them is commented out so you can select and choose which repository you want to inject at a given time. If you want to inject the mock repository, you will remove the comment tag (//) from the first line and place it at the start of the second line.
Now you can inject your ...