SlideShare a Scribd company logo
1 of 10
Repository Pattern
Separation of Concern using Entity Framework
(TRINUG – May 2, 2018)
Agenda
 Quick Intro
 Role of a MVC/Web-API Controller
 Controllers – the old fashioned way
 Demo
 Controllers – the old fashioned way
 Controllers – the new way
 Introducing Repository
 Demos
 Basic Repository
 Controllers – the new way
 Generic Repository Demo
 Unit Of Work Demo (if time permits)
 Question/Comments
Quick Intro
Bhuvnesh J Bhatt (BJ)
bjbhatt@hotmail.com
Consultant @ Attain, LLC
Role of a MVC/Web-API Controller
 A Controller (MVC or Web-API) typically:
 Receives requests from a user. The request might contain data that user want to
read/add/update/delete
 Process the request using the data received by performing CRUD operations against a
database
 Returns data, if applicable, to the user
 Controller might depend upon Helper Classes and/or BLL/DAL for validating the
data and updating data in the database
Controllers – the old fashioned way
 Controller initiates a DB-Context
 Methods within a controller uses the DB-Context to perform CRUD operations,
resulting in:
 Controller being tightly coupled with the ORM technology (e.g. Entity Framework) being
used
 Controller might depend upon BLL and DAL for validating the business logic and
updating data in the database instead of directly using DB-Context
 Now, DAL is tightly coupled with the ORM technology being used
Demos
 Controller – the old fashion way
Controllers – the new way
 Controllers (or underlying DAL or Helper Classes) should not depend on ORM
Technology and/or instantiate a DB-Context, instead should uses Repository
 Controllers should only have to deal with Primitive values, POCOs or Collection of
POCOs
 If ORM Technology is changed Controller or DAL should not be required to be
refactored
 Advantages
 DB technology change are unaffected in the Core application logic
 Unit testability (by creating Mock objects)
 Team member with different expertise can work together and as long as the Interface
“contract” is not broken. The “back-end” team member can completely change how data is
stored/retrieved e.g. From SQL DB to NoSQL DB to WebService to WebAPI without affecting
work done by “front-end” developers
Introducing Repository
 Build an Interface and related concrete Implementation class that contain various methods that
performs CRUD operations
 These methods expects and returns Primitive values (e.g. integer for Keys), POCOs or collections of
POCOs as parameters and values respectively
 Method should take primitive types and/or Model Object as parameters and return primitive values,
Model Object and/or void (for action operation)
 Recommendation: a separate Repository for each entity (DB object)
 ** If you need to call a Stored Procedure create a method in the repository **
 In controller use Interface to perform CRUD operations
 Using dependency injection a Concrete Implementation of class can be passed at runtime
 Mock Implementation class of Repository can be passed by a Unit Test
 Concrete implementation of the Repository will use DB-Context to perform CRUD operation as
dictated by the method – is only object that would be tightly coupled
Demos
 Basic Repository
 Generic Repository Demo
 Unit Of Work Demo (if time permits)
Questions/Comments

More Related Content

Similar to Trinug - repository pattern

JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol BuffersMatt O'Keefe
 
Struts 2 Overview
Struts 2 OverviewStruts 2 Overview
Struts 2 Overviewskill-guru
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2divzi1913
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCJohn Lewis
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC SeminarJohn Lewis
 
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
ZZ BC#7.5 asp.net mvc practice  and guideline refresh! ZZ BC#7.5 asp.net mvc practice  and guideline refresh!
ZZ BC#7.5 asp.net mvc practice and guideline refresh! Chalermpon Areepong
 
The semantic web an inside look at the creation of control loop foundation
The semantic web   an inside look at the creation of control loop foundationThe semantic web   an inside look at the creation of control loop foundation
The semantic web an inside look at the creation of control loop foundationEmerson Exchange
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To MvcVolkan Uzun
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use itnspyre_net
 
IRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET Journal
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2Long Nguyen
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Entity Framework Code First Migrations
Entity Framework Code First MigrationsEntity Framework Code First Migrations
Entity Framework Code First MigrationsDiluka99999
 
MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCAnton Krasnoshchok
 

Similar to Trinug - repository pattern (20)

J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
 
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful  Protocol BuffersJavaOne 2009 - TS-5276 - RESTful  Protocol Buffers
JavaOne 2009 - TS-5276 - RESTful Protocol Buffers
 
Struts Ppt 1
Struts Ppt 1Struts Ppt 1
Struts Ppt 1
 
MVC
MVCMVC
MVC
 
Struts 2 Overview
Struts 2 OverviewStruts 2 Overview
Struts 2 Overview
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Annotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVCAnnotation-Based Spring Portlet MVC
Annotation-Based Spring Portlet MVC
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
Sprint Portlet MVC Seminar
Sprint Portlet MVC SeminarSprint Portlet MVC Seminar
Sprint Portlet MVC Seminar
 
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
ZZ BC#7.5 asp.net mvc practice  and guideline refresh! ZZ BC#7.5 asp.net mvc practice  and guideline refresh!
ZZ BC#7.5 asp.net mvc practice and guideline refresh!
 
The semantic web an inside look at the creation of control loop foundation
The semantic web   an inside look at the creation of control loop foundationThe semantic web   an inside look at the creation of control loop foundation
The semantic web an inside look at the creation of control loop foundation
 
Introduction To Mvc
Introduction To MvcIntroduction To Mvc
Introduction To Mvc
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use it
 
IRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHPIRJET- Lightweight MVC Framework in PHP
IRJET- Lightweight MVC Framework in PHP
 
Virtual Classroom
Virtual ClassroomVirtual Classroom
Virtual Classroom
 
Data access
Data accessData access
Data access
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
Repository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity FrameworkRepository Pattern in MVC3 Application with Entity Framework
Repository Pattern in MVC3 Application with Entity Framework
 
Entity Framework Code First Migrations
Entity Framework Code First MigrationsEntity Framework Code First Migrations
Entity Framework Code First Migrations
 
MVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVCMVC Pattern. Flex implementation of MVC
MVC Pattern. Flex implementation of MVC
 

Recently uploaded

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 

Recently uploaded (20)

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 

Trinug - repository pattern

  • 1. Repository Pattern Separation of Concern using Entity Framework (TRINUG – May 2, 2018)
  • 2. Agenda  Quick Intro  Role of a MVC/Web-API Controller  Controllers – the old fashioned way  Demo  Controllers – the old fashioned way  Controllers – the new way  Introducing Repository  Demos  Basic Repository  Controllers – the new way  Generic Repository Demo  Unit Of Work Demo (if time permits)  Question/Comments
  • 3. Quick Intro Bhuvnesh J Bhatt (BJ) bjbhatt@hotmail.com Consultant @ Attain, LLC
  • 4. Role of a MVC/Web-API Controller  A Controller (MVC or Web-API) typically:  Receives requests from a user. The request might contain data that user want to read/add/update/delete  Process the request using the data received by performing CRUD operations against a database  Returns data, if applicable, to the user  Controller might depend upon Helper Classes and/or BLL/DAL for validating the data and updating data in the database
  • 5. Controllers – the old fashioned way  Controller initiates a DB-Context  Methods within a controller uses the DB-Context to perform CRUD operations, resulting in:  Controller being tightly coupled with the ORM technology (e.g. Entity Framework) being used  Controller might depend upon BLL and DAL for validating the business logic and updating data in the database instead of directly using DB-Context  Now, DAL is tightly coupled with the ORM technology being used
  • 6. Demos  Controller – the old fashion way
  • 7. Controllers – the new way  Controllers (or underlying DAL or Helper Classes) should not depend on ORM Technology and/or instantiate a DB-Context, instead should uses Repository  Controllers should only have to deal with Primitive values, POCOs or Collection of POCOs  If ORM Technology is changed Controller or DAL should not be required to be refactored  Advantages  DB technology change are unaffected in the Core application logic  Unit testability (by creating Mock objects)  Team member with different expertise can work together and as long as the Interface “contract” is not broken. The “back-end” team member can completely change how data is stored/retrieved e.g. From SQL DB to NoSQL DB to WebService to WebAPI without affecting work done by “front-end” developers
  • 8. Introducing Repository  Build an Interface and related concrete Implementation class that contain various methods that performs CRUD operations  These methods expects and returns Primitive values (e.g. integer for Keys), POCOs or collections of POCOs as parameters and values respectively  Method should take primitive types and/or Model Object as parameters and return primitive values, Model Object and/or void (for action operation)  Recommendation: a separate Repository for each entity (DB object)  ** If you need to call a Stored Procedure create a method in the repository **  In controller use Interface to perform CRUD operations  Using dependency injection a Concrete Implementation of class can be passed at runtime  Mock Implementation class of Repository can be passed by a Unit Test  Concrete implementation of the Repository will use DB-Context to perform CRUD operation as dictated by the method – is only object that would be tightly coupled
  • 9. Demos  Basic Repository  Generic Repository Demo  Unit Of Work Demo (if time permits)