articles

Home / DeveloperSection / Articles / Repository Pattern in MVC

Repository Pattern in MVC

Jonas Stuart2487 17-Jan-2017

Repository act as a middle man between the rest of the application and the data access logic. A repository isolates all the data access code from the rest of the application. In doing so you are benefited by having a single point of change in case modification. It is easier to change in repository because it is another class separate from the application. So we can easily change when necessary without code affecting.

A repository does at least five thing Insert, select by Id, Select All, delete, save.By using repository pattern in our application we get code reusability and maximum code testability. In this article we will do crud operation through repository pattern. I will use generic repository pattern for all operation.

For generic repository we will add a class in the model folder and add the following code.

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using Manish.Models;
 
namespace Manish.Models
{
    public  interfaceIRepository<T> where T : class
    {        
        IEnumerable<T> SelectAll();
        T SelectByID(object id);
        void Insert(T obj);
        void Update(T obj);
        void Delete(object Id);
   
    }
}

In this class I have make the Interface of public type with T type.

Now add another class in the model folder and name it Repository and Inherit IRepository in it

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using Manish.Models;
using System.Data.Entity;
using System.Data;
 
namespace Manish.Models
{
    publicclassRepository<T>:IRepository<T> where T:class
    {
        privatetestdataEntities db = null;
        privateDbSet<T> table = null;
 
      public Repository()
        {
            this.db = newtestdataEntities();
            table = db.Set<T>();
        }
 
      public Repository(testdataEntities db)
        {
            this.db = db;
            table = db.Set<T>();
        }
 
        publicIEnumerable<T> SelectAll()
        {
            return table.ToList();
        }
 
        public T SelectByID(object id)
        {
            return table.Find(id);
        }
 
        publicvoid Insert(T obj)
        {
            table.Add(obj);
            db.SaveChanges();
        }
 
        publicvoid Update(T obj)
        {
            table.Attach(obj);
            db.Entry(obj).State = EntityState.Modified;
            db.SaveChanges();
        }
 
        publicvoid Delete(object Id)
        {
            T existing = table.Find(Id);
            table.Remove(existing);
            db.SaveChanges();    
 
        }      
    }
  
}

In this Repository class I have defined all the method of IRepository class for Implementation. now add one another class name it DML Repository for setting the property for IRepository.

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace Manish.Models
{
    publicclassDMLRepository
    {
        publicIRepository<Student> Student
        {
            get
            {
                returnnewRepository<Student>();
            }
        }
        publicIRepository<Login> Login
        {
            get
            {
                returnnewRepository<Login>();
            }
        }
    }
}

Now add a class in the controller folder name it base controller. And add the following code.

using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using Manish.Models;
using Manish.Controllers;
using System.Web.Mvc;
 
namespace Manish.Controllers
{
    publicclassBaseController :Controller
    {
        protectedDMLRepository Uow { get; set; }
        public BaseController()
        {
            Uow = newDMLRepository();
        }
    }
}

Now in the home controller inherit base controller

using System;

using

System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using Manish.Controllers;

using Manish.Models;

using

System.Web.Security;

 

 

namespace Manish.Controllers

{

    public class HomeController : BaseController

    {

 

        //

        // GET: /Home/

 

 

        public ActionResult Index()

        {                       

 

            ViewBag.Users =

Uow.Student.SelectAll();         

                    

            return View();

 

        }

        [AcceptVerbs(HttpVerbs.Post)]

        public ActionResult Index(Student model)

        {

            if (model.Id == 0)

            {

                Uow.Student.Insert(model);

            }

            else

            {

               

Uow.Student.SelectByID(model.Id);

                Uow.Student.Update(model);

            }

            ViewBag.Users =

Uow.Student.SelectAll();

            return PartialView("studentlist");

        }

        public ActionResult Delete(int id)

        {

            Uow.Student.Delete(id);

            ViewBag.Users =

Uow.Student.SelectAll();

            return PartialView("studentlist");

        }

        public ActionResult Edit(int id)

        {

            var data = Uow.Student.SelectByID(id);

            return Json(data, JsonRequestBehavior.AllowGet);

        }

      

        

        }

 

 

    }

}

You can also visit these useful related post

Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC using Entity Framework

What is Repository Pattern in ASP.NET MVC?

Crud Operations Using Generic Repository Pattern in ASP.NET MVC 4



Updated 20-Dec-2017

Leave Comment

Comments

Liked By