Admity Junior School Syllabus

Half yearly Exam ( 2019-2020 )
Repository Pattern Implementation Guidelines

If you will use one of the above implementations, then with the generic repository, you cannot use specific operation for an entity and in case of non-generic implementation, you have to write code for common CRUD operations for each entity. So better way is, just create a generic repository for commonly used CRUD operation and for specific operation create a non-generic repository and inherit from the generic repository. The below diagram explains the above things.

Using Both Generic and Non-Generic Repository Pattern in c#

Half yearly Exam ( 2019-2020 )

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.

  1. Non-Generic Repository
  2. 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.