SlideShare a Scribd company logo
Generic Repository Pattern
with ASP.NET MVC and EF
Md. Mahedee Hasan
Microsoft MVP | Trainer | Speaker
Software Architect
LeadSoft Bangladesh Limited
Linkedin: http://www.linkedin.com/in/mahedee
Blog: http://mahedee.net/
1
• MVC Stands for Model – View – Controller
• It is Software Architectural pattern
MAHEDEE.NET 2
What is MVC?
• 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?
• 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?
DRY?
Don’t repeat yourself
-Means don't write duplicate code
Use generic repository pattern to
implement DRY
MAHEDEE.NET 5
• 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
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
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
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
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
Implementation
• Tools and Technology used
– Visual Studio 2013
– Visual C#
– ASP.NET MVC 5
– Entity Framework 6
– Razor view engine
MAHEDEE.NET 11
Step 1: Create an ASP.NET MVC 5 application using
Visual Studio 2013
MAHEDEE.NET 12
Implementation…
Step 2: Configure connection string in web.config
MAHEDEE.NET 13
Implementation …
Step 3: Create Models
MAHEDEE.NET 14
Implementation …
Step 4: Create a DbContext in Repository folder.
MAHEDEE.NET 15
Implementation …
Step 5: Create IGenericRepository and
GenericRepository in Repository folder
MAHEDEE.NET 16
Implementation …
• 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 …
• Step 7: Add links to _Layout
MAHEDEE.NET 18
Implementation …
• 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 …
Step 9: Run Project
MAHEDEE.NET 20
Implementation …
21

More Related Content

PPTX
Generic Repository Pattern with ASP.NET MVC and EF
PPTX
Feature and Future of ASP.NET
PDF
Repository and Unit Of Work Design Patterns
PPSX
C#.net applied OOP - Batch 3
PPTX
Asp.net c# MVC-5 Training-Day-1 of Day-9
PPSX
Oop principles
PPTX
MVC - Introduction
PPTX
ASP.NET Brief History
Generic Repository Pattern with ASP.NET MVC and EF
Feature and Future of ASP.NET
Repository and Unit Of Work Design Patterns
C#.net applied OOP - Batch 3
Asp.net c# MVC-5 Training-Day-1 of Day-9
Oop principles
MVC - Introduction
ASP.NET Brief History

What's hot (20)

PPTX
ASP .Net MVC 5
PPT
ASP.NET MVC Presentation
PPTX
Asp.net c# mvc Training: Day-3 of Day-9
PPTX
MVC4 framework
PPTX
Architecting ASP.NET MVC Applications
PPTX
Asp.net mvc 5 course module 1 overview
PPTX
Getting started with MVC 5 and Visual Studio 2013
PPTX
ASP.NET MVC Best Practices malisa ncube
PPTX
ASP.NET MVC 5 - EF 6 - VS2015
PPT
Php Frameworks
PDF
Asp 1a-aspnetmvc
PPTX
Asp.net mvc presentation by Nitin Sawant
PDF
Php and-mvc
PPT
My cool new Slideshow!
PPTX
Asp.net MVC training session
PDF
Kentico and MVC
PPTX
PHP framework difference
PPTX
Using MVC with Kentico 8
PPT
Code igniter overview
PDF
Principles of MVC for PHP Developers
ASP .Net MVC 5
ASP.NET MVC Presentation
Asp.net c# mvc Training: Day-3 of Day-9
MVC4 framework
Architecting ASP.NET MVC Applications
Asp.net mvc 5 course module 1 overview
Getting started with MVC 5 and Visual Studio 2013
ASP.NET MVC Best Practices malisa ncube
ASP.NET MVC 5 - EF 6 - VS2015
Php Frameworks
Asp 1a-aspnetmvc
Asp.net mvc presentation by Nitin Sawant
Php and-mvc
My cool new Slideshow!
Asp.net MVC training session
Kentico and MVC
PHP framework difference
Using MVC with Kentico 8
Code igniter overview
Principles of MVC for PHP Developers
Ad

Viewers also liked (20)

PPTX
Introduction the Repository Pattern
PPTX
Dependency injection in asp.net core
PPT
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
PPTX
Layered Software Architecture
PPTX
ASP.NET MVC and Entity Framework 4
DOCX
Generic Repository Pattern in MVC3 Application with Entity Framework
PPTX
Generic Unit Of Work Service & Repository
PDF
Object Oriented Programming
PPTX
Domain Driven Design in Rails
PPSX
C# - Part 1
PPTX
Entity Framework Database and Code First
PPTX
Dapper performance
PDF
The world of enterprise solution development with asp.net and C#
PPTX
EF6 or EF Core? How Do I Choose?
PPTX
A Tour of EF Core's (1.1) Most Interesting & Important Features
PPSX
MS SQL Server
PDF
Introduction to OMNeT++
PPTX
ASP.NET MVC 4 Request Pipeline Internals
PDF
Introduction to TFS 2013
PPTX
Repository design pattern in laravel - Samir Poudel
Introduction the Repository Pattern
Dependency injection in asp.net core
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Layered Software Architecture
ASP.NET MVC and Entity Framework 4
Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Unit Of Work Service & Repository
Object Oriented Programming
Domain Driven Design in Rails
C# - Part 1
Entity Framework Database and Code First
Dapper performance
The world of enterprise solution development with asp.net and C#
EF6 or EF Core? How Do I Choose?
A Tour of EF Core's (1.1) Most Interesting & Important Features
MS SQL Server
Introduction to OMNeT++
ASP.NET MVC 4 Request Pipeline Internals
Introduction to TFS 2013
Repository design pattern in laravel - Samir Poudel
Ad

Similar to Generic repository pattern with ASP.NET MVC and Entity Framework (20)

PPTX
Ps02 cint24 mvc in php
PDF
MVC 3.0 KU Day 1 v 1.1
PDF
Aspnetmvc 1
PPTX
Php Framework
PDF
Php framework
PPTX
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
PPTX
Asp.net Overview and Controllers
PPTX
Prism library and MVVM
PPT
PDF
Clean architecture with asp.net core
PPTX
Introduction to MVC Web Framework with CodeIgniter
PPTX
An Introduction To Model  View  Controller In XPages
PDF
Introduction to ASP.NET MVC
PPTX
ASP.NET MVC Zero to Hero
PPTX
MWLUG 2015 - An Introduction to MVC
PDF
DotVVM Fundamentals
PDF
Asp.NETZERO - A Workshop Presentation by Citytech Software
PPTX
Walther Mvc
PDF
ASP.NET MVC Introduction
PDF
Session dotNed Saturday 28 januari 2017
Ps02 cint24 mvc in php
MVC 3.0 KU Day 1 v 1.1
Aspnetmvc 1
Php Framework
Php framework
Introduction Asp.Net Core, MVC, Docker (Linux), Postman and Swagger
Asp.net Overview and Controllers
Prism library and MVVM
Clean architecture with asp.net core
Introduction to MVC Web Framework with CodeIgniter
An Introduction To Model  View  Controller In XPages
Introduction to ASP.NET MVC
ASP.NET MVC Zero to Hero
MWLUG 2015 - An Introduction to MVC
DotVVM Fundamentals
Asp.NETZERO - A Workshop Presentation by Citytech Software
Walther Mvc
ASP.NET MVC Introduction
Session dotNed Saturday 28 januari 2017

More from Md. Mahedee Hasan (8)

PPTX
Azure Machine Learning
PPTX
Chatbot development with Microsoft Bot Framework and LUIS
PPTX
Chatbot development with Microsoft Bot Framework
PPTX
Introduction to Windows 10 IoT Core
PPTX
Whats new in visual studio 2017
PPTX
Increasing productivity using visual studio 2017
PPSX
Exciting features in visual studio 2017
PPSX
ASP.NET Web form
Azure Machine Learning
Chatbot development with Microsoft Bot Framework and LUIS
Chatbot development with Microsoft Bot Framework
Introduction to Windows 10 IoT Core
Whats new in visual studio 2017
Increasing productivity using visual studio 2017
Exciting features in visual studio 2017
ASP.NET Web form

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
Benefits of DCCM for Genesys Contact Center
PPTX
Introduction to Artificial Intelligence
PPTX
AIRLINE PRICE API | FLIGHT API COST |
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PPTX
Online Work Permit System for Fast Permit Processing
PDF
top salesforce developer skills in 2025.pdf
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
PPTX
Presentation of Computer CLASS 2 .pptx
PDF
Become an Agentblazer Champion Challenge Kickoff
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
How to Confidently Manage Project Budgets
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
PDF
Jenkins: An open-source automation server powering CI/CD Automation
2025 Textile ERP Trends: SAP, Odoo & Oracle
ManageIQ - Sprint 268 Review - Slide Deck
The Five Best AI Cover Tools in 2025.docx
Benefits of DCCM for Genesys Contact Center
Introduction to Artificial Intelligence
AIRLINE PRICE API | FLIGHT API COST |
Materi_Pemrograman_Komputer-Looping.pptx
Online Work Permit System for Fast Permit Processing
top salesforce developer skills in 2025.pdf
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Presentation of Computer CLASS 2 .pptx
Become an Agentblazer Champion Challenge Kickoff
PTS Company Brochure 2025 (1).pdf.......
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
Materi-Enum-and-Record-Data-Type (1).pptx
How to Confidently Manage Project Budgets
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
Jenkins: An open-source automation server powering CI/CD Automation

Generic repository pattern with ASP.NET MVC and Entity Framework

  • 1. Generic Repository Pattern with ASP.NET MVC and EF Md. Mahedee Hasan Microsoft MVP | Trainer | Speaker Software Architect LeadSoft Bangladesh Limited Linkedin: http://www.linkedin.com/in/mahedee Blog: http://mahedee.net/ 1
  • 2. • MVC Stands for Model – View – Controller • It is Software Architectural pattern MAHEDEE.NET 2 What is MVC?
  • 3. • 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?
  • 4. • 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?
  • 5. DRY? Don’t repeat yourself -Means don't write duplicate code Use generic repository pattern to implement DRY MAHEDEE.NET 5
  • 6. • 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. Implementation • Tools and Technology used – Visual Studio 2013 – Visual C# – ASP.NET MVC 5 – Entity Framework 6 – Razor view engine MAHEDEE.NET 11
  • 12. Step 1: Create an ASP.NET MVC 5 application using Visual Studio 2013 MAHEDEE.NET 12 Implementation…
  • 13. Step 2: Configure connection string in web.config MAHEDEE.NET 13 Implementation …
  • 14. Step 3: Create Models MAHEDEE.NET 14 Implementation …
  • 15. Step 4: Create a DbContext in Repository folder. MAHEDEE.NET 15 Implementation …
  • 16. Step 5: Create IGenericRepository and GenericRepository in Repository folder MAHEDEE.NET 16 Implementation …
  • 17. • 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 …
  • 18. • Step 7: Add links to _Layout MAHEDEE.NET 18 Implementation …
  • 19. • 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 …
  • 20. Step 9: Run Project MAHEDEE.NET 20 Implementation …
  • 21. 21