articles

Home / DeveloperSection / Articles / Generic Repository Pattern with Unit of Work (Uow)

Generic Repository Pattern with Unit of Work (Uow)

Anonymous User2790 14-Aug-2018

Please follows these step to implement generic type repository pattern.

Step 1: Open visual studio -> Select new project -> Enter project name like ‘RepPattern’.

Step 2. After project creation clicks on Solution ‘Rep Pattern’ and add a class library for ‘DAL’ project.

Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)

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)Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)

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)Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)

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

Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)

Step 6. Now delete default class1.

Step 7. Click on repository project and add new item -> Interface.

Generic Repository Pattern with Unit of Work (Uow)Generic Repository Pattern with Unit of Work (Uow)

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 from manage nugget package and also add reference of DAL project.

Generic Repository Pattern with Unit of Work (Uow)

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 StudentMasterRepository

using 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)

 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... 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By