Repository Design Pattern in C#

Author - Webner
4.03.2020
|
0 Comments
||

Introduction to Repository Design Pattern

There are many Design Patterns in .NET, for example, entity design pattern but to overcome some disadvantages of other patterns we prefer to use the Repository Design Pattern pattern. Nowadays, most applications need to access data from one or more than one data source.
For example, if you have an EmployeeContainer and SalaryContainer

In EmployeeContainer you may need to access data from SalaryContainer.
repository design

As in the above diagram, the action methods of the Employee controller directly call the Entity Framework data context class and perform queries to get the data from the database. They also perform Crud operations using the data context and DbSet. The Entity Framework further interacts with the SQL Server database.

The drawback of entity framework pattern

Database queries(CRUD) operations write on the controller method and design can cause code duplication. Further, if we need to change the controller, we can do small changes in logic.

Solution:-

Repository Design Pattern acts as a mediator between the application and the data logic.
repository design 1

Now we can see the Controller is not interacting directly with the entity framework of Database context DBSET. but it is using EmployeRepository to interact with DBSET.

A project in visual studio in the below images explains it in a better way:

1.
repository design 2

2.
repository 3

3.
repository 4

4.
repository 5

5.
repository 6

6.
repository 7

7.
repository 8

8.
repository 9

9.
repository 10

These steps will create an Entity model and Dbcontext as shown below:

Model
repository 11

Dbcontext
repository 12

Now we need to create Employee repository:

IEmployeeRepository


namespace NGTS.LeadMarket.Services.Crm.Data.Repositories
{
public interface IEmployeeRepository {
IEnumerable GetAll();
Employee GetById(int EmployeeID);
void Insert(Employee employee);
void Update(Employee employee);
void Delete(int EmployeeID);
void Save();
}
}

EmployeeRepository

public class EmployeeRepository: IEmployeeRepository
{
private readonly EmployeeDBContext _context;
public EmployeeRepository()
{
_context = new EmployeeDBContext();
}
public EmployeeRepository(EmployeeDBContext context)
{
_context = context;
}
public IEnumerable GetAll()
{
return _context.Employees.ToList();
}
public Employee GetById(int EmployeeID)
{
return _context.Employees.Find(EmployeeID);
}
public void Insert(Employee employee)
{
_context.Employees.Add(employee);
}
}

After this create a controller or call this repository from a controller action to perform database queries.

public class EmployeeController: Controller
{
private IEmployeeRepository _employeeRepository;
public EmployeeController()
{
_employeeRepository = new EmployeeRepository(new EmployeeDBContext());
}
public EmployeeController(IEmployeeRepository employeeRepository)
{
_employeeRepository = employeeRepository;
}
[HttpGet] public ActionResult Index()
{
var model = _employeeRepository.GetAll();
return View(model);
}
}

How can we overcome disadvantages of patterns?

There are many Design Patterns in .NET, for example, entity design pattern but to overcome some disadvantages of other patterns we prefer to use the Repository Design Pattern pattern.

What is drawback of entity framework pattern?

Database queries(CRUD) operations write on the controller method and design can cause code duplication. Further, if we need to change the controller, we can do small changes in logic.

What is Repository Design Pattern in C#?

The Repository Design Pattern in C# is one of the most used design patterns in the real-time application.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *