You can download the source code here.
Repository Pattern
-> A data access pattern.
-> Separates persistence responsibility from business classes.
-> Enables
1) Single Responsibility Principle
2) Separation of Concerns
3) Testability
-> Separates interfaces for each entity in application.
e.g CustomerRepository, OrderRepository
Setup projects in Visual Studio
- Create a new solution in Visual Studio.
- Add a library project for Entity or model classes. Here I created a library project Codify.Entity.
- Add a library project for data access related classes. In demo application Codify.DataAccess refers to this.
- Add a console application to write the test code.
- Below are the projects in the solutions
1) Codify.DataAccess
2) Codify.Entity
3) Codify.TestConsole
The demo app is a simpl Employee – Order management application. The application helps to create Employees, Products and creates orders for the employees using products created. Below is the DB diagram of the tables.
Now add a BaseEntity class to Codify.Entity project. The BaseEntity contains common properties like CreatedDate, CreatedBy etc. which are part of all tables.
1 2 3 4 5 6 7 8 9 |
public class BaseEntity { public DateTime CreatedDate { get; set; } public string CreatedBy { get; set; } public DateTime ModifiedDate { get; set; } public string ModifiedBy { get; set; } public bool IsDeleted { get; set; } public virtual byte[] RecordTimeStamp { get; set; } } |
Add a class file for Employee entity.
1 2 3 4 5 6 7 8 9 10 11 |
public class EmployeeEntity : BaseEntity { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string Country { get; set; } public virtual ICollection<OrderEntity> OrderEntities { get; set; } } |
Add a class file for Product entity
1 2 3 4 5 6 7 8 |
public class ProductEntity : BaseEntity { public int Id { get; set; } public string ProductName { get; set; } public decimal UnitPrice { get; set; } public ICollection<OrderDetailsEntity> OrderDetails { get; set; } } |
Add a class file for Order entity
1 2 3 4 5 6 7 8 |
public class OrderEntity : BaseEntity { public int Id { get; set; } public int EmployeeId { get; set; } public DateTime RequiredDate { get; set; } public string ShipAddress { get; set; } public virtual EmployeeEntity Employee { get; set; } } |
Add a class file for Order Details Entity.
1 2 3 4 5 6 7 8 9 10 11 |
public class OrderDetailsEntity : BaseEntity { public int Id { get; set; } public int OrderId { get; set; } public int ProductId { get; set; } public Decimal UnitPrice { get; set; } public int Quantity { get; set; } public Decimal Discount { get; set; } public virtual OrderEntity Order { get; set; } public virtual ProductEntity Product { get; set; } } |
Below is the class diagram for models or entities.
The project structure is as shown below.
One comment on “Entity Framework Repository Pattern Demo Application”