In this article, I will demonstrate you what repository pattern is and what are the benefits of repository pattern. I will also explain the generic repository pattern with their advantage.
I believe you all know the concept behind DRY. DRY stands for Don’t Repeat Yourself. It is a principal for OOPs. Nowadays with enterprise project which can be multi-tier architecture or multi-layer architecture, there is a need to implement DRY.
We can implement the features of DRY using the Repository Pattern.
Repository Pattern
Sometimes, it is required to define some logic between our business logic and data access logic. That logic can be defined using Repository pattern. Basically Repository pattern is a mediator between two layers. Repository pattern are mostly used where we need to modify the data before passing to the next stage.
Benefits of Repository Pattern
It centralizes data logic or business logic and service logic.
It gives a substitution point for the unit tests.
Provides a flexible architecture.
If you want to modify the data access logic or business access logic, you don’t need to change the repository logic.
Generic Repository Pattern
Sometimes, it is required or it is best to make all the repository logic in one place using the generic logic. We can create only one repository class which will be responsible to handle the entire scenario. It can be a logic between repository and data access or it can be a logic between business logic and repository. In all cases we only write single repository. The main benefit of Generic Repository Pattern is reusability of code.
Benefits of Generic Repository Pattern
It reduces redundancy of code.
It force programmer to work using the same pattern.
It creates possibility of less error.
If you use this pattern then it is easy to maintain the centralized data access logic.
Thanks for reading the article.