Updated February 14, 2023
Introduction to Entity Framework Repository Pattern
Entity Framework Repository Pattern develops an abstract layer between the business logic layer and the application’s data access layer. The EF Repository directly communicates to the DAL (Data Access Layer), receives the data, and supplies it to the BAL (Business Logic Layer). The essential thing to using the Repository Pattern is to separate the DAL and BAL instead of that if you make any changes in this logic that will not directly affect other logic.
What is Entity Framework Repository Pattern?
The Data-Driven Web Application required a precise approach to accessing the data. The essential characteristic of this approach is to separate the queries, physical database, and other DAL (Data Access Logic) from the application. A repository Pattern is the accepted approach to accomplish such isolation. The fundamental thing to using the Repository Pattern is to split the DAL (Data Access Layer) and BAL (Business Logic Layer) instead; if you make any changes in this logic, they will not directly affect it on other logic.
The above images clear the Repository Pattern, built as an Abstraction Layer between the DAL (Data Access Layer) and BLL (Business Logic Layer) of the application. The Abstraction Layer contains several methods like Select, Insert, Delete, and so on; they are the CRUD Operations wrapped by the repository. The repository uses the Entity Framework Data Context Class to achieve the CRUD Operations.
The Abstraction Layer is generally called the Repository Layer. It communicates directly with DAL (Data Access Layer); it retrieves the data and presents it to the BLL (Business Logic Layer). The most crucial benefit of using the Repository Pattern is isolating the DAL and BLL.
Using Entity Framework Repository Pattern
The Entity Framework Repository directly communicates to the DAL (Data Access Layer), receives the data, and supplies it to the BAL (Business Logic Layer). Entity Framework Repository Pattern is used to build up an abstract layer between the two logic layers of the application. The primary business applications required to access the data exist in the data store; thus, the simplest method is to code everything in the core application.
Consider one example we have a controller called the EmployeeController. The Employee Controller Class contains various action methods which perform the CURD Operations (Create, Read, Update and Delete) on the essential database. Let’s assume you were using the Entity Framework for Database Access.
In the several Actions of EmployeeController, it directly instantiates the Entity Framework Data Context and executes the query to get the data. It performs CRUD Operations using Data Context and DbSet; the Entity Framework talks to the SQL Server Database. The above image design causes code repetition, and the controller is at risk of modifying the changes in the DAL (Data Access Logic). If the application alters the Employee from two controllers, each controller will repeat a similar code, and further modifications are required at two places. To deal with this issue, we are going for the Repository Pattern introduced to solve this problem.
The repository Pattern is the intermediate between the whole application and the DAL (Data Access Logic). The repository isolates the Data Access Code from the rest of the application by following the repository help in simple to significant changes without affecting the entire code. In addition, testing our Controllers becomes more straightforward because the testing framework is required not to execute against the actual Database Access Code.
Let’s see the following image, which contains the repository; the previous image is modified with the introduction of the repository.
The Employee Controller will not directly talk to the Entity Framework Data Context in this approach. In addition, there will be no queries or operations of the database in the Action Methods. The Repository Employee wraps the entire operations. The Employee Repository uses the Entity Framework Data Context to retrieve the jobs done. To observe that the Employee Repository only contains the methods like Insert(), Update(), SeleteAll(), SelectByID() and Delete(). The EmployeeController uses those methods to perform actions. Employee Repository gives the In-Memory Collections like Interface to the Consumers.
Entity Framework Repository Pattern Setting in the Project
To Create the Employee Repository – It contains the five operations like selecting all records, selecting a particular ID, Inserting, Deleting and Updating. So in the repository, we need to code the five operations to enforce that we define the interface called IEmployeeRepository contains all methods which implement the interface in the class. So the IEmployeeRepository interface looks as follows.
Code:
public interface IEmployeeRepository
{
IEnumerable<Employee> SelectAll();
Employee SelectByID(string id);
void Insert(Employee obj);
void Update(Employee obj);
void Delete(string id);
void Save();
}
To include the EmployeeRepository Class in the project and implement IEmployeeRepository to it, look at the following code, which explains the entire code of EmployeeRepository Class.
Code:
public class EmployeeRepository : IEmployeeRepository
{
private EmployeeEntities dbCon = null;
public EmployeeRepository()
{
this.dbCon = new EmployeeEntities();
}
public EmployeeRepository(EmployeeEntities dbCon)
{
this.dbCon = dbCon;
}
public IEnumerable<Employee> SelectAll()
{
return dbCon.Employees.ToList();
}
public Employee SelectByID(string id)
{
return dbCon.Employees.Find(id);
}
public void Insert(Employee obj)
{
dbCon.Employees.Add(obj);
}
public void Update(Employee obj)
{
dbCon.Entry(obj).State = EntityState.Modified;
}
public void Delete(string id)
{
Employee existing = dbCon.Employees.Find(id);
dbCon.Employees.Remove(existing);
}
public void Save()
{
dbCon.SaveChanges();
}
}
The EmployeeRepository Class implements the CRUD Operations.
Benefits of Repository Pattern
A repository Pattern is defined as the logic between Data Access Logic and Business Logic. The repository Pattern is the mediator between two layers. It is used frequently when we require altering the data before transferring it to the next stage.
In the Repository Pattern, there are a few benefits which are mentioned below:
- A repository Pattern is the centralized Service Logic and Data Logic or Business Logic.
- This gives the substitution point for the Unit Testing.
- It provides a flexible architecture.
- If there is any modification in the Data Access Logic (DAL) or Business Access Logic (BAL), we do not need to modify the Repository Logic.
Conclusion
This article about the Entity Framework Repository Pattern helps isolate the Data Access Code from the rest of the application. This separation supports code reusability, reduces future changes to the code, and makes it simpler to test the controller class.
Recommended Articles
This is a guide to Entity Framework Repository Pattern. Here we discuss the introduction, using the entity framework repository pattern and benefits. You may also have a look at the following articles to learn more –