Generic Repository Pattern
According to my own knowledge that Every project needs a better architecture because it is the heart of any Projects. And wherefore I know that all programmers or developers need for a better architecture that reduces repetitive code and separates the data access layer and business logic layer. So, we will use our generic repository with MVC and Entity framework.
First Step
Create a Database and their tables.
Second Step
Open visual studio and add a new mvc (Empty) project.
Then Add Entity Framework on it.
Third Step
Create _IAllRepository.cs file and write this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MvcApplication11.Models.DAL
{
interface _IAllRepository<T> where T : class
{
IEnumerable<T> GetModel();
T GetModelByID(int modelId);
void InsertModel(T model);
void DeleteModel(int modelID);
void UpdateModel(T model);
void Save();
}
}
Step 4
Call _IAllRepository in AllRepository.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MvcApplication11.Models.DAL
{
public class AllRepository<T> : _IAllRepository<T> where T : class
{
private DataContext _context;
private IDbSet<T> dbEntity;
public AllRepository()
{
_context = new DataContext();
dbEntity = _context.Set<T>();
}
public IEnumerable<T> GetModel()
{
return dbEntity.ToList();
}
public T GetModelByID(int modelId)
{
return dbEntity.Find(modelId);
}
public void InsertModel(T model)
{
dbEntity.Add(model);
}
public void DeleteModel(int modelID)
{
T model = dbEntity.Find(modelID);
dbEntity.Remove(model);
}
public void UpdateModel(T model)
{
_context.Entry(model).State = EntityState.Modified;
}
public void Save()
{
_context.SaveChanges();
}
}
}
Step 5
Create Controller class for Contact table and controller name is "ContactsController"
using MvcApplication11.Models;
using MvcApplication11.Models.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication11.Controllers
{
public class ContactsController : Controller
{
//
// GET: /Contacts/
private _IAllRepository<Contact> interfaceobj;
public ContactsController()
{
this.interfaceobj = new AllRepository<Contact>();
}
public ActionResult Index()
{
return View(from m in interfaceobj.GetModel() select m);
}
//
// GET: /Contacts/Details/5
public ActionResult Details(int id)
{
Contact c = interfaceobj.GetModelByID(id);
return View(c);
}
//
// GET: /Contacts/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Contacts/Create
[HttpPost]
public ActionResult Create(Contact collection)
{
try
{
// TODO: Add insert logic here
interfaceobj.InsertModel(collection);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Contacts/Edit/5
public ActionResult Edit(int id)
{
Contact c = interfaceobj.GetModelByID(id);
return View(c);
}
//
// POST: /Contacts/Edit/5
[HttpPost]
public ActionResult Edit(int id, Contact collection)
{
try
{
// TODO: Add update logic here
interfaceobj.UpdateModel(collection);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /Contacts/Delete/5
public ActionResult Delete(int id)
{
Contact c = interfaceobj.GetModelByID(id);
return View(c);
}
//
// POST: /Contacts/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
interfaceobj.DeleteModel(id);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Step 6
Create Controller class for BookTbl and controller name is "MyBooksController"
using MvcApplication11.Models;
using MvcApplication11.Models.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication11.Controllers
{
public class MyBooksController : Controller
{
//
// GET: /MyBooks/
private _IAllRepository<BookTbl> interfaceobj;
public MyBooksController()
{
this.interfaceobj = new AllRepository<BookTbl>();
}
public ActionResult Index()
{
return View(from m in interfaceobj.GetModel() select m);
}
//
// GET: /MyBooks/Details/5
public ActionResult Details(int id)
{
BookTbl b = interfaceobj.GetModelByID(id);
return View(b);
}
//
// GET: /MyBooks/Create
public ActionResult Create()
{
return View();
}
//
// POST: /MyBooks/Create
[HttpPost]
public ActionResult Create(BookTbl collection)
{
try
{
// TODO: Add insert logic here
interfaceobj.InsertModel(collection);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /MyBooks/Edit/5
public ActionResult Edit(int id)
{
BookTbl b = interfaceobj.GetModelByID(id);
return View(b);
}
//
// POST: /MyBooks/Edit/5
[HttpPost]
public ActionResult Edit(int id, BookTbl collection)
{
try
{
// TODO: Add update logic here
interfaceobj.UpdateModel(collection);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
//
// GET: /MyBooks/Delete/5
public ActionResult Delete(int id)
{
BookTbl b = interfaceobj.GetModelByID(id);
return View(b);
}
//
// POST: /MyBooks/Delete/5
[HttpPost]
public ActionResult Delete(int id, BookTbl collection)
{
try
{
// TODO: Add delete logic here
interfaceobj.DeleteModel(id);
interfaceobj.Save();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Step 7
Here add views according to their action like Insert Update and delete
1 - Views for BookTbl
For display, all record so create first "Index View"
@model IEnumerable<MvcApplication11.Models.BookTbl>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head> <meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.BookName)
</th>
<th>
@Html.DisplayNameFor(model => model.BookAuthor)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BookAuthor)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
</body>
</html>
Create
@model MvcApplication11.Models.BookTbl
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Create</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>BookTbl</legend>
<div class="editor-label">
@Html.LabelFor(model => model.BookName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookName)
@Html.ValidationMessageFor(model => model.BookName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BookAuthor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookAuthor)
@Html.ValidationMessageFor(model => model.BookAuthor)
</div>
<p> <input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
Update
@model MvcApplication11.Models.BookTbl
@{ Layout = null;
}
<!DOCTYPE html>
<html> <head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>BookTbl</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
@Html.LabelFor(model => model.BookName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookName)
@Html.ValidationMessageFor(model => model.BookName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.BookAuthor)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.BookAuthor)
@Html.ValidationMessageFor(model => model.BookAuthor)
</div>
<p> <input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>
Delete
@model MvcApplication11.Models.BookTbl
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" /> <title>Delete</title>
</head>
<body>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>BookTbl</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.BookName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BookName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.BookAuthor)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BookAuthor)
</div>
</fieldset>
@using (Html.BeginForm()) {
<p>
<input type="submit" value="Delete" /> |
@Html.ActionLink("Back to List", "Index")
</p>
}
</body>
</html>
Details
@model MvcApplication11.Models.BookTbl
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Details</title>
</head>
<body>
<fieldset>
<legend>BookTbl</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.BookName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BookName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.BookAuthor)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.BookAuthor)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
@Html.ActionLink("Back to List", "Index")
</p>
</body>
</html>
And going on to create views for Contact table or contact controller.
Thank You.
Leave Comment