In previous tutorial you used basic CRUD (Create, Read, Update and Delete) functionality with entity framework and by using jquery dialog(s) in asp.net mvc. In this you’ll see some ways to use the repository and unit of work patterns for CRUD operations.
The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and facilitate automated unit testing or test-driven development (TDD).
Let’s start with the implementation of Repository and Unit of Work patterns in a tutorial. If you have gone through my previous tutorial then start with where it ends and if you do not have experience about previous tutorial please download the sample of previous tutorial and start with that.
1) In the current solution, add three new class library project:
Our solution explorer will something look like this:
2) Delete Category, SubCategory and Product class from the web project i.e. ManagingMultijQueryForm and add these classes in Sample.Model project. Also add reference of System.ComponentModel.DataAnnotations in order to use the (Required and Display) attribute.
a) Category class (Category.cs):
b) SubCategory class (SubCategory.cs):
c) Product class (Product.cs):
3) In Sample.Data.Contracts add two interface IRepository (for repositories) and IProductUow ( for unit of work). Before creating the interfaces add the reference of Sample.Model in Sample.Data.Contracts
a) IRepository.cs, it is generic interface:
b) IProductUow.cs :
4) In Sample.Data project, add the reference of Sample.Model and Sample.Data.Contracts and install entity framework via package manager console (Tools à Library Package Manager à Package Manager Console) by using following command:
5) Delete ProductDbContext, ProductDbContextInitializer from the web project and add these classes in Sample.Data project:
a) ProductDbContext class (ProductDbContext.cs):
b) ProductDbContextInitializer class (ProductDbContextInitializer.cs)
c) Create new EFRepository class which implements IRepository interface for doing CRUD by using entity framework:
6) Add Helpers folder in Sample.Data project and in that folder create a interface IRepositoryProvider and two classes (RepositoryProvider and RepositoryFactories) in order to create and find repository by using factory pattern:
7) Create ProductUow class that implements IProductUow in Sample.Data project:
8) Now in order to use repository and unit of work in web application project (ManagingMultijQueryForm), configure Ninject via package manager console, use of following command in order to configure Ninject in an application:
a. Install-package Ninject
b. Install-package Ninject.MVC3
9) Replace the Global.asax.cs MvcApplication class with the following:
10) Last and final step, create a BaseController and inherit it in all (ProductController, CategoryController and SubCategoryController) controllers in order to use IProductUow:
Before executing an application please do replace ManagingMultijQueryForm.Models with Sample.Model in all the views and remove the reference of ManagingMultijQueryForm.Models wherever it gives error.
Now you can run and test the application I hope it works fine. You can download the source code from the link given below:
I had taken a reference of Code Camper Spa application which is developed and thought by great John Papa.
Thanks for reading this article.