The MVC framework is one of the common design patterns in modern web applications. It provides a complete separation of concerns. But even MVC is not a silver bullet for all problems. On top of MVC, we can implement various design patterns to solve the specific problem.
In this example, we will first implement a sample application using pure MVC architecture and then we will see how to improve our code standards by implementing the repository design pattern. So, let's start with one example.
I will suggest you create one MVC application. Let's create the table first then we will set up the Entity Framework in the application. Here is the table structure.

We have added the Entity Framework file (.edmx) file and the model is like this.

Now let's implement a Company controller and implement CRUD operations. Have a look, we have messed up the database operations within the controller. Now if you decide to change it, how will you implement your data access layer? Right now, the Contact Manager application uses the Microsoft Entity Framework to access the database. However, you might decide to migrate to a new or alternative data access technology such as ADO.NET Data Services or NHibernate. However, because the data access code is not isolated from the controller code, there is no way to modify the data access code in your application without modifying other code that is not directly related to data access. Have a look at the following example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MVC.Controllers
{
public class CompanyController : Controller
{
efDBEntities _db = null;
public CompanyController()
{
_db = new efDBEntities();
}
// Read All company Information
public string Index()
{
var company = _db.company.ToList();
return View(company); // Changed 'view' to 'View'
}
// Create new company
public ActionResult Create(company comp)
{
var insertedCompany = _db.company.Add(comp);
_db.SaveChanges();
return RedirectToAction("Index");
}
// Update Existing company
public ActionResult Update(company comp)
{
var company = _db.company.Where(f => f.cid == comp.cid).FirstOrDefault();
if (company != null)
{
company.company_name = comp.company_name;
_db.SaveChanges();
return RedirectToAction("Index");
}
throw new ArgumentException();
}
// Delete company
public ActionResult Delete(int? id) // Changed 'Int32 ?' to 'int?'
{
var company = _db.company.Where(f => f.cid == id).FirstOrDefault(); // Changed '1' to 'id'
if (company != null)
{
_db.Entry(company).State = System.Data.EntityState.Deleted;
_db.SaveChanges();
return RedirectToAction("Index");
}
throw new ArgumentException();
}
}
}
Ok, we understand the problem; how to solve it? The solution is the Repository Design Pattern, we will totally isolate the DB operation part into a different class and we will consume the service (yes, let's think of the class as a service class) into our actual controller class.

Manav PandyaPosted Oct 4, 2016, 10:10 AM
Can you please share source code with me
Manav PandyaPosted Oct 4, 2016, 10:10 AM
Nice share
hoakimPosted Apr 11, 2015, 6:09 PM
Nice article Sourav. Let me propose another approach described on this article http://wp.me/p3mRWu-FC
Manas MohapatraPosted Dec 23, 2014, 12:52 AM
Nice article on Repository design pattern. Thank you for sharing. I have one question here: I want to search record where CompanyId = 1 and CompanyName = "Test". Now I have two cases. First I want to search by CompanyId and secondly by both(CompanyId, CompanyName). How do I implement here. Because our Read() takes only one parameter which is CompanyId.