Generic Repository Pattern with ASP.NET MVC and EF
-
Upload
mahedee-hasan -
Category
Software
-
view
233 -
download
8
Transcript of Generic Repository Pattern with ASP.NET MVC and EF
![Page 1: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/1.jpg)
Generic Repository Pattern with ASP.NET MVC and EF
Md. Mahedee Hasan
Microsoft MVP | Trainer | Speaker
Software Architect
LeadSoft Bangladesh LimitedLinkedin: http://www.linkedin.com/in/mahedee
Blog: http://mahedee.net/
![Page 2: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/2.jpg)
• MVC Stands for Model – View – Controller• It is Software Architectural pattern
MAHEDEE.NET 2
What is MVC?
![Page 3: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/3.jpg)
• Is an open source web application framework• It implements the model-view-controller pattern• MVC design pattern aims to “Separation Of concern”
MAHEDEE.NET 3
What is ASP.NET MVC?
![Page 4: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/4.jpg)
• Entity Framework is an ORM.• Work with relational data using domain-specific
objects• Eliminates the need for most of the data-access code
MAHEDEE.NET 4
What is ASP.NET EF?
![Page 5: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/5.jpg)
DRY?
Don’t repeat yourself
-Means don't write duplicate code
Use generic repository pattern to implement DRY
MAHEDEE.NET 5
![Page 6: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/6.jpg)
• What is Design Pattern?• Design pattern is a solution of known problems
• What is Software Design Pattern?• Is a solution of known problems in Software.
• Strategies of solving commonly occurring problems. • A design pattern is not a finish design. • It is like a template to solve a problem.
MAHEDEE.NET 6
Software Design Pattern
![Page 7: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/7.jpg)
Repository Pattern
• Mediator between BLL and DAL(Data Source)• It’s a separation layer between Data and Domain
Layer– Separates data and domain Layer
MAHEDEE.NET 7
![Page 8: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/8.jpg)
Benefits of Repository Pattern
• Centralizes data logic or service logic.
• Provides a substitution point for the unit tests – Both BLL and DAL
• Provides a flexible architecture– Can adopt new change easily
• Domain driven development is easier
MAHEDEE.NET 8
![Page 9: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/9.jpg)
What is Generic Repository Pattern?
• Generally - one repository for one model to access data.– Write similar code again and again
• What if Single Repository for all?
• Single repository for data access of all models.
MAHEDEE.NET 9
![Page 10: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/10.jpg)
Benefits of Generic Repository Pattern
• Reduce redundancy of code
• Faster development
• Force developer to work same pattern – Possibility of less error or no error
• Easy to maintain– Centralize data access logic
MAHEDEE.NET 10
![Page 11: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/11.jpg)
Implementation
• Tools and Technology used– Visual Studio 2013– Visual C#– ASP.NET MVC 5– Entity Framework 6– Razor view engine
MAHEDEE.NET 11
![Page 12: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/12.jpg)
Step 1: Create an ASP.NET MVC 5 application using Visual Studio 2013
MAHEDEE.NET 12
Implementation…
![Page 13: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/13.jpg)
Step 2: Configure connection string in web.config
MAHEDEE.NET 13
Implementation …
![Page 14: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/14.jpg)
Step 3: Create Models
MAHEDEE.NET 14
Implementation …
![Page 15: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/15.jpg)
Step 4: Create a DbContext in Repository folder.
MAHEDEE.NET 15
Implementation …
![Page 16: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/16.jpg)
Step 5: Create IGenericRepository and GenericRepository in Repository folder
MAHEDEE.NET 16
Implementation …
![Page 17: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/17.jpg)
• Step 6: – Create controllers of each models– Select template “MVC5 Controller with views, using Entity
Framework” – Modify Controllers and use Generic Repository in
Controller
MAHEDEE.NET 17
Implementation …
![Page 18: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/18.jpg)
• Step 7: Add links to _Layout
MAHEDEE.NET 18
Implementation …
![Page 19: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/19.jpg)
• Step 8: Write following command in package manager console– PM> Enable-Migrations -ContextTypeName
GenericRepoContext– PM> Add-Migration initialcreate– PM> Update-Database -Verbose -Force
MAHEDEE.NET 19
Implementation …
![Page 20: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/20.jpg)
Step 9: Run Project
MAHEDEE.NET 20
Implementation …
![Page 21: Generic Repository Pattern with ASP.NET MVC and EF](https://reader035.fdocuments.net/reader035/viewer/2022062300/55c4204fbb61eb68398b4648/html5/thumbnails/21.jpg)
21