Generic Repository Pattern in C#
In this article, I am going to discuss how to implement both generic and non-generic repository pattern in ASP.NET MVC application using Entity Framework. In most of the real-time application, the generic repository contains the methods which are common for all the entities. But if you want some specific operation for some specific repository. Then you need to create a specific repository with the required operations. Here, in this article, I will show you how to implement both generic and specific repository for an entity.
As we already discussed the repository pattern is used to create an abstraction layer between the data access layer and business logic layer to perform the CRUD operations against the underlying database. We also discussed that the repository design pattern can be implemented in the following two ways. So, I strongly recommended you to read the following two articles before proceeding to this article.
- Non-Generic Repository
- Generic Repository
Generic Repository Pattern
The generic repository pattern is used to define common database operations such as Create, Retrieve Update, Delete, etc. for all the database entities in a single class.
Non-Generic Repository Pattern (Specific Repository)
The non-generic repository pattern is used to define all database operations related to a specific entity within a separate class. For example, if you have two entities let’s say, Employee and Customer, then each entity will have its own implementation repository.
So, before implementing both generic and specific repositories let us first understand the implementation guidelines. That means when to use Generic and when to use Specific and when to use both generic and specific in an application.