In this tutorial We will learn How to create generic Repository Pattern with Asp.Net MVC and Entity Framework.
Repository Pattern is used to create an abstraction layer between data access layer and business logic layer of an application. This layer communicate with data access layer and provide data to business logic layer as per requirement.
In general Our controller action methods directly access the data context and get data from database.
The main purpose of this Repository Pattern to Isolate the data access layer and business logic layer, so that Changes in any layer can not effect directly on other layer.
By using Repository Pattern, Our Controller Action Method won’t talk to Context Class Directly.
Now all the database action are done In our Repository.
Add Two Model class to your Model folder.
Create GenericRepository folder in your project.
First Add an Interface to this folder
I have created generic
Similarly We will Create Generic Class which will implement IRepository Interface.
So create a Empty Home Controller to perform CRUD Operation using Repository Pattern over Employee class.
I have declare IRepository as type of Employee.
Index View
Details View
Edit View
Create New record
Delete Record
DOWNLOAD Code to get the View Code
Repository Pattern is used to create an abstraction layer between data access layer and business logic layer of an application. This layer communicate with data access layer and provide data to business logic layer as per requirement.
In general Our controller action methods directly access the data context and get data from database.
The main purpose of this Repository Pattern to Isolate the data access layer and business logic layer, so that Changes in any layer can not effect directly on other layer.
By using Repository Pattern, Our Controller Action Method won’t talk to Context Class Directly.
Now all the database action are done In our Repository.
Now wee will see how to create Generic Repository Pattern In Asp.Net Mvc and Entity Framework.
Create a MVC application.Add Two Model class to your Model folder.
01
02
03
04
05
06
07
08
09
10
11
| namespace RepositoryDemo.Models { public class Employee { public int Id { get ; set ; } public string Name { get ; set ; } public string Email { get ; set ; } public string Age { get ; set ; } } } |
1
2
3
4
5
6
7
8
| using System.Data.Entity; namespace RepositoryDemo.Models { public class DemoContext:DbContext { public DbSet<Employee> employee { get ; set ; } } } |
First Add an Interface to this folder
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
| using System; using System.Collections.Generic; namespace RepositoryDemo.GenericRepository { interface IRepository<T> where T: class { IEnumerable<T> getAll(); T getById( object Id); void Insert(T obj); void Update(T obj); void Delete(Object Id); void Save(); } } |
IRepository
interface, which contain method for all the CRUD Operations.Similarly We will Create Generic Class which will implement IRepository Interface.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| using System.Collections.Generic; using System.Linq; using RepositoryDemo.Models; using System.Data.Entity; using System.Data; using System; namespace RepositoryDemo.GenericRepository { public class Repository<T>:IRepository<T> where T: class { private DemoContext demoContext; private DbSet<T> dbSet; public Repository() { this .demoContext = new DemoContext(); dbSet = demoContext.Set<T>(); } public IEnumerable<T> getAll() { return dbSet.ToList(); } public T getById( object Id) { return dbSet.Find(Id); } public void Insert(T obj) { dbSet.Add(obj); } public void Update(T obj) { demoContext.Entry(obj).State = EntityState.Modified; } public void Delete( object Id) { T getObjById = dbSet.Find(Id); dbSet.Remove(getObjById); } public void Save() { demoContext.SaveChanges(); } public void Dispose() { Dispose( true ); GC.SuppressFinalize( this ); } protected virtual void Dispose( bool disposing) { if (disposing) { if ( this .demoContext != null ) { this .demoContext.Dispose(); this .demoContext = null ; } } } } } |
Our Generic Repository Pattern is Created.
Now We can use it in our Controller.So create a Empty Home Controller to perform CRUD Operation using Repository Pattern over Employee class.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| using System.Web.Mvc; using RepositoryDemo.GenericRepository; using RepositoryDemo.Models; namespace RepositoryDemo.Controllers { public class HomeController : Controller { private IRepository<Employee> _repository = null ; public HomeController() { this ._repository = new Repository<Employee>(); } public ActionResult Index() { var employees = _repository.getAll(); return View(employees); } [HttpGet] public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Employee employee) { if (ModelState.IsValid) { _repository.Insert(employee); _repository.Save(); return RedirectToAction( "Index" ); } else { return View(employee); } } public ActionResult Edit( int Id) { var employee = _repository.getById(Id); return View(employee); } [HttpPost] public ActionResult Edit(Employee employee) { if (ModelState.IsValid) { _repository.Update(employee); _repository.Save(); return RedirectToAction( "Index" ); } else { return View(employee); } } public ActionResult Details( int Id) { var employee = _repository.getById(Id); return View(employee); } public ActionResult Delete( int Id) { var employee = _repository.getById(Id); return View(employee); } [HttpPost, ActionName( "Delete" )] public ActionResult DeleteConfirmed( int Id) { var employee = _repository.getById(Id); _repository.Delete(Id); _repository.Save(); return RedirectToAction( "Index" ); } } } |
Index View
Details View
Edit View
Create New record
Delete Record
DOWNLOAD Code to get the View Code
No comments:
Post a Comment