---
title: "Generic Repository Pattern with Unit of Work (Uow)"  
description: "In this article, we can define a generic repository pattern with a unit of work, that defines a repository pattern more intuitive and efficient."  
author: "Anonymous User"  
published: 2018-08-14  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/13071/generic-repository-pattern-with-unit-of-work-uow  
category: "c#"  
tags: ["asp.net mvc", "mvc", "mvc3", "mvc4", "mvc5"]  
reading_time: 6 minutes  

---

# Generic Repository Pattern with Unit of Work (Uow)

Please follows these step to implement generic type [repository pattern](https://www.mindstick.com/articles/1565/crud-operations-using-generic-repository-pattern-in-asp-dot-net-mvc-4).

**Step 1:** Open [visual studio](https://www.mindstick.com/forum/12863/how-to-insert-a-video-in-asp-dot-net-web-page-in-visual-studio-2010) -> Select new project -> Enter project name like ‘RepPattern’.

**Step 2.** After project [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan) clicks on Solution ‘Rep Pattern’ and add a [class library](https://www.mindstick.com/forum/157445/how-to-use-asp-dot-net-core-apis-in-the-class-library) for ‘DAL’ project.

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/4ef0bb41-6b82-4370-9c6c-fbc5c52bfce3.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/f4e03670-53d3-4c68-9a78-f7649cc7f283.png)

After add class library, delete default class class1.

**Step 3.** Now click on DAL project(RepPattern.Data).

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/cd6a8908-c32e-4be2-9b7e-a95d1bae8833.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/601c9ae9-eef7-4adc-adc7-34633732e9c7.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/5dd10c83-e4de-46ed-9883-456bdd9b8d78.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/388bb045-e1a2-4795-9025-76b4ef01d0a4.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/99288097-22f5-4626-81c3-cc15a85eb0fe.png)

**Step 4.** After add Entity Model, open App.config file and copy connection string and paste into web.config file into base project (RepPattern ) below default connection.

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/245d7a91-c2f6-43d1-89f8-565bd35eb258.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/8ade93d4-236f-46bc-a10a-def9da33a054.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/077f8bc2-0981-4a1f-9943-8581ded544f6.png)

**Step 5.** Now click on project solution and add class library for repository.

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/b314e567-5955-45f8-b075-492b80536fd2.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/b027f2b4-0439-4c8e-af5f-8a92def7fbaa.png)

**Step 6.** Now delete default class1.

**Step 7.** Click on repository project and add new item -> [Interface](https://www.mindstick.com/blog/676/interface).

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/ef4d17e1-0f09-4aca-b023-854ace60335d.png)![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/a743bd2e-c2d7-43d4-ae94-3ea64463e70a.png)

**Step 8.** Define interface public and add following basic methods in Irepository.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace RepPattern.Repository{    public interface IRepository<T> where T : class    {        bool Add(T entity);        bool Update(T entity);        bool Delete(T entity);        bool Delete(object id);        IQueryable<T> GetAll();        T FindById(object id);        T GetBySql(string query, params object[] parameter);        T GetBySql(string query);    }}
```

**Step 9.** Now add a class Repository in repository project and add code like below. And please make sure you install [entity framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework) from manage nugget package and also [add reference](https://www.mindstick.com/forum/548/how-to-add-reference-to-system-web-optimization) of DAL project.

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/6410b3c2-8372-44b0-a2dd-3e3dabdd72de.png)

```
using RepPattern.Data;using System;using System.Collections.Generic;using System.Data.Entity;using System.Linq;using System.Text;
namespace RepPattern.Repository{    public class Repository<T> : IRepository<T> where T : class    {        protected HarshEntities context;        public Repository(HarshEntities _context)        {            context = _context;        }
        public bool Add(T entity)        {            context.Set<T>().Add(entity);            return true;        }
        public bool Update(T entity)        {            context.Entry(entity).State = EntityState.Modified;            return true;        }
        public bool Delete(T entity)        {            context.Set<T>().Remove(entity);            return true;        }
        public bool Delete(object id)        {            var entity = context.Set<T>().Find(id);            context.Set<T>().Remove(entity);            return true;        }
        public IQueryable<T> GetAll()        {            return context.Set<T>().AsQueryable();        }
        public T FindById(object id)        {            return context.Set<T>().Find(id);        }
        public T GetBySql(string query)        {            return context.Database.SqlQuery<T>(query).FirstOrDefault();        }
        public T GetBySql(string query, params object[] parameter)        {            return context.Database.SqlQuery<T>(query, parameter).FirstOrDefault();        }    }}
```

**Step 10.** Again add a class unit of work. And write code like below.

```
using RepPattern.Data;using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace RepPattern.Repository{    public class UnitOfWork : IDisposable    {        private readonly HarshEntities _db;        public UnitOfWork(HarshEntities db)        {            _db = db;        }
        void IDisposable.Dispose()        {            _db.Dispose();        }
        public int SaveChanges()        {            return _db.SaveChanges();        }    }}
```

**Step 11.** Again a interface for IStudentMasterRepository, and declare custom methods.

```
using RepPattern.Data;using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace RepPattern.Repository{    interface IStudentMasterRepository    {        Students GetStudentById(long Id);    }}
Step 12:Add a class StudentMasterRepositoryusing RepPattern.Data;using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace RepPattern.Repository{    public class StudentMasterRepository : Repository<Students>, IStudentMasterRepository    {        private HarshEntities _db;        public StudentMasterRepository(HarshEntities db)            : base(db)        {            _db = db;        }
        public Students GetStudentById(long Id)        {            var data = context.Students.FirstOrDefault(x => x.Id == Id);            return data;        }    }}
```

**Step 13.** Let's declare the property of StudentMasterRepository in Unit of Work like below.

```
using RepPattern.Data;using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace RepPattern.Repository{    public class UnitOfWork : IDisposable    {        private readonly HarshEntities _db;        public UnitOfWork(HarshEntities db)        {            _db = db;        }
        private StudentMasterRepository _studentMaster;        public StudentMasterRepository StudentMasterRepository        {            get            {                if (this._studentMaster == null)                {                    this._studentMaster = new StudentMasterRepository(_db);                }                return this._studentMaster;            }        }
        void IDisposable.Dispose()        {            _db.Dispose();        }
        public int SaveChanges()        {            return _db.SaveChanges();        }    }}
```

**Step 14.** Now we Add a controller in the main project. Now we Add a controller in the main project.

![Generic Repository Pattern with Unit of Work (Uow)](https://www.mindstick.com/mindstickarticle/d7adcbe2-0234-45cd-a13f-d5497577f50d/images/a73d89d8-7e28-4a74-9771-6c2fa14c6ec4.png)

And Write code like below:

```
using RepPattern.Data;using RepPattern.Repository;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;
namespace RepPattern.Controllers{    public class HomeController : Controller    {        public UnitOfWork Uow { get; set; }        public HomeController()        {            Uow = new UnitOfWork(new HarshEntities());        }
        public ActionResult Index()        {            var data = Uow.StudentMasterRepository.GetStudentById(1);            return View();        }    }}
```

\

## *I Hope It's Informative...*

---

Original Source: https://www.mindstick.com/articles/13071/generic-repository-pattern-with-unit-of-work-uow

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
