Skip to content

Instantly share code, notes, and snippets.

@Vikaskumargd
Created February 5, 2017 20:52
Show Gist options
  • Save Vikaskumargd/9e2cd65354e2f6e938acc8ce219c26ad to your computer and use it in GitHub Desktop.
Save Vikaskumargd/9e2cd65354e2f6e938acc8ce219c26ad to your computer and use it in GitHub Desktop.
Repository Pattern Plus Unit of Work
using MyUniversity.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
namespace MyUniversity.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal MyUniversityDBContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(MyUniversityDBContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyUniversity.Models;
using MykUniversity.DAL;
namespace MyUniversity.Controllers
{
public class CourseController : Controller
{
private MyUniversityDBContext db = new MyUniversityDBContext();
private UnitOfWork unitOfWork = new UnitOfWork();
//
// GET: /Course/
public ViewResult Index()
{
var course = unitOfWork.CourseRepository.Get(includeProperties: "Department");
return View(course.ToList());
}
//
// GET: /Course/Details/5
public ActionResult Details(int id = 0)
{
Course course = unitOfWork.CourseRepository.GetByID(id);
if (course == null)
{
return HttpNotFound();
}
return View(course);
}
// GET: /Course/Create
public ActionResult Create()
{
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "Name");
return View();
}
// POST: /Course/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Course course)
{
if (ModelState.IsValid)
{
unitOfWork.CourseRepository.Insert(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "Name",
course.DepartmentID);
return View(course);
}
// GET: /Course/Edit/5
public ActionResult Edit(int id = 0)
{
Course course = unitOfWork.CourseRepository.GetByID(id);
if (course == null)
{
return HttpNotFound();
}
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "Name",
course.DepartmentID);
return View(course);
}
// POST: /Course/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Course course)
{
if (ModelState.IsValid)
{
unitOfWork.CourseRepository.Update(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
ViewBag.DepartmentID = new SelectList(db.Department, "DepartmentID", "Name",
course.DepartmentID);
return View(course);
}
// GET: /Course/Delete/5
public ActionResult Delete(int id = 0)
{
Course course = db.Course.Find(id);
if (course == null)
{
return HttpNotFound();
}
return View(course);
}
// POST: /Course/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Course course = db.Course.Find(id);
db.Course.Remove(course);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
using MyUniversity.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyUniversity.DAL
{
public class UnitOfWork : IDisposable
{
private MyUniversityDBContext context = new MyUniversityDBContext();
private GenericRepository<Department> departmentRepository;
private GenericRepository<Course> courseRepository;
public GenericRepository<Department> DepartmentRepository
{
get
{
if (this.departmentRepository == null)
{
this.departmentRepository = new GenericRepository<Department>(context);
}
return departmentRepository;
}
}
public GenericRepository<Course> CourseRepository
{
get
{
if (this.courseRepository == null)
{
this.courseRepository = new GenericRepository<Course>(context);
}
return courseRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment