Introduction to Repository Pattern
Learn what the repository pattern is, why it is used, and how it is achieved.
We'll cover the following
What is a repository?
To make your complex application code easy to understand and manage, add a layer of abstraction. A repository does just that.
A repository helps achieve abstraction in your application by creating an abstraction layer between data access and business logic (controller). This helps to isolate your application from changes in the data store and facilitates unit testing. This promotes a loosely coupled approach to access your data from the database. It also saved you from writing the same queries across multiple controllers that achieve the same purpose.
How is it achieved?
An interface is created with all of the declarations of the functions responsible for performing CRUD operations, among other functionalities. Then one or more repository classes are created in which your database context is injected. These repository classes implement the interface’s functions and provide their logic for handling operations. One of these repositories is then injected into your controller to provide functionality. You do not need to inject your database context into your controllers.
Get hands-on with 1400+ tech skills courses.