SlideShare a Scribd company logo
Repository and 
Unit of Work 
Design Patterns 
29th October 2014 
Presented By: Hatim Hakeel
10/29/2014 Page 2
Repository 
Object-Relational Metadata Mapping 
Patterns 
• Metadata Mapping 
• Query Object 
• Repository 
Repository – Mediates between the 
domain and data mapping layers using a 
collection-like interface for accessing 
domain objects 
10/29/2014 Page 3
What about DAOs ? 
• A Data Access Object doesn’t hide to the 
Data Access Layer that it is accessing a 
data table, unlike a Repository. 
• A DAO typically has a 1:1 map with a data 
store table or entity-set. 
• A Repository acts at a higher level of 
abstraction working with aggregations of 
business entities. 
10/29/2014 Page 4
Why use Repositories ? 
• Abstraction layer between Business Logic 
Layer and Data Access Layer. 
• Insulates application (Controller) from data 
store changes. 
• Facilitates automated Unit Testing, Test 
Driven Development. How ? 
• Easy to create mock repositories using 
in-memory domain object collections 
as a data store. But it’s difficult to mock 
entities similarly. 
10/29/2014 Page 5
UoW 
Object-Relational Behavior Patterns 
• Unit of Work 
• Identity Map 
• Lazy Load 
UoW – Maintains a list of objects affected 
by a business transaction and 
coordinates the writing out of changes 
and the resolution of concurrency 
problems 
10/29/2014 Page 6
Why use UoW ? 
• Keeps track of manipulated objects in order to 
synchronize in-memory data with the data store. 
• Provides a single transaction for multiple queries. 
• The UoW commits the transaction. 
• If the commit fails, rollback. 
• Single commit call on the database. 
• All object tracking information is centralized. 
• Provides a firm means for complex scenarios like 
handling business transactions that span several 
system transactions using Optimistic Offline 
Lock and Pessimistic Offline Lock. 
10/29/2014 Page 7
Entity Framework 5 and ASP.NET 
MVC 4 
and previous versions… 
• Repository classes with a UoW class. 
• Repository classes without a UoW class. 
• Single Repository. 
• A Repository class per Entity. 
10/29/2014 Page 8
The Overall Bigger Picture 
10/29/2014 Page 9
A possible project structure 
10/29/2014 Page 10
IUnitOfWork interface 
10/29/2014 Page 11
UnitofWork.cs i 
10/29/2014 Page 12
GenericRepository<TEntity> Getters ii 
10/29/2014 Page 13
Persist DbContext changes and clean 
up resources iii 
10/29/2014 Page 14
IGenericRepository<TEntity> 
10/29/2014 Page 15
GenericRepository<TEntity> 
constructor i 
10/29/2014 Page 16
IEnumerable<TEnitity> Get(…) ii 
10/29/2014 Page 17
Remaining CRUD method 
implementations iii 
10/29/2014 Page 18
EF DbContext implementer 
10/29/2014 Page 19
How does the architectural wiring come 
live in the controller ? 
10/29/2014 Page 20
Sample Edit, Get(…) calls 
10/29/2014 Page 21
Entity Framework 6 and ASP.NET 
MVC 5 
From http://goo.gl/eyDbrM 
Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12) | 
ASP.NET MVC Site 
10/29/2014 Page 22
Do we still need Repository and 
UoW ? 
10/29/2014 Page 23
Consider following scenarios 
without repositories 
10/29/2014 Page 24
In Summary 
• Business objects should interface with a 
business oriented storage service 
• Data objects should interface with a data 
oriented service 
• The repository should be the bridging 
mediator 
• BUT the real use case for the Repository 
pattern is in dealing with multiple 
persistence services 
10/29/2014 Page 25
Sample project hosted at GitHub 
https://github.com/hatimhakeel/UniversityVille 
10/29/2014 Page 26
Thank You 
Questions ? 
10/29/2014 Page 27

More Related Content

What's hot (20)

PPTX
Spring boot Introduction
Jeevesh Pandey
 
PDF
Angular
Lilia Sfaxi
 
PPTX
Android share preferences
Ajay Panchal
 
PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PPTX
React hooks
Ramy ElBasyouni
 
PPTX
C# in depth
Arnon Axelrod
 
PPTX
Angular modules in depth
Christoffer Noring
 
PPTX
Angular 14.pptx
MohaNedGhawar
 
PDF
Data Persistence in Android with Room Library
Reinvently
 
PPTX
Spring Boot and REST API
07.pallav
 
PPTX
jQuery
Dileep Mishra
 
PDF
Support NodeJS avec TypeScript Express MongoDB
ENSET, Université Hassan II Casablanca
 
PPT
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
JAX London
 
PPT
C# Exceptions Handling
sharqiyem
 
PPTX
Introduction to Koltin for Android Part I
Atif AbbAsi
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Repository Pattern with c# and Entity Framework
Imtiaj Ahammad
 
PPTX
Generic Repository Pattern with ASP.NET MVC and EF
Md. Mahedee Hasan
 
PPT
Java Servlets
BG Java EE Course
 
Spring boot Introduction
Jeevesh Pandey
 
Angular
Lilia Sfaxi
 
Android share preferences
Ajay Panchal
 
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
React hooks
Ramy ElBasyouni
 
C# in depth
Arnon Axelrod
 
Angular modules in depth
Christoffer Noring
 
Angular 14.pptx
MohaNedGhawar
 
Data Persistence in Android with Room Library
Reinvently
 
Spring Boot and REST API
07.pallav
 
Support NodeJS avec TypeScript Express MongoDB
ENSET, Université Hassan II Casablanca
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
JAX London
 
C# Exceptions Handling
sharqiyem
 
Introduction to Koltin for Android Part I
Atif AbbAsi
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Repository Pattern with c# and Entity Framework
Imtiaj Ahammad
 
Generic Repository Pattern with ASP.NET MVC and EF
Md. Mahedee Hasan
 
Java Servlets
BG Java EE Course
 

Viewers also liked (20)

PDF
Generic repository pattern with ASP.NET MVC and Entity Framework
Md. Mahedee Hasan
 
PPTX
Introduction the Repository Pattern
Bill Lin
 
PPTX
Dapper performance
Suresh Loganatha
 
PPTX
Repository design pattern in laravel - Samir Poudel
Sameer Poudel
 
PPTX
Dependency injection in asp.net core
Bill Lin
 
PPTX
Generic Unit Of Work Service & Repository
Abdulah Al Bahhar
 
PDF
Laravel 5 In Depth
Kirk Bushell
 
PPT
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Svetlin Nakov
 
PPTX
Layered Software Architecture
Lars-Erik Kindblad
 
PPTX
ASP.NET MVC and Entity Framework 4
James Johnson
 
DOCX
Generic Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
PDF
Real World Dependency Injection - phpday
Stephan Hochdörfer
 
PPTX
Entity Framework Database and Code First
James Johnson
 
PPTX
A Tour of EF Core's (1.1) Most Interesting & Important Features
Julie Lerman
 
PPTX
EF6 or EF Core? How Do I Choose?
Julie Lerman
 
PPTX
ASP.NET MVC 4 Request Pipeline Internals
Lukasz Lysik
 
PDF
Difference between wcf and asp.net web api
Umar Ali
 
PPTX
ASP.NET MVC 4 - Routing Internals
Lukasz Lysik
 
PPTX
C# 6.0 - DotNetNotts
citizenmatt
 
PDF
Day02 01 Advance Feature in C# DH TDT
Nguyen Patrick
 
Generic repository pattern with ASP.NET MVC and Entity Framework
Md. Mahedee Hasan
 
Introduction the Repository Pattern
Bill Lin
 
Dapper performance
Suresh Loganatha
 
Repository design pattern in laravel - Samir Poudel
Sameer Poudel
 
Dependency injection in asp.net core
Bill Lin
 
Generic Unit Of Work Service & Repository
Abdulah Al Bahhar
 
Laravel 5 In Depth
Kirk Bushell
 
Architectural Patterns and Software Architectures: Client-Server, Multi-Tier,...
Svetlin Nakov
 
Layered Software Architecture
Lars-Erik Kindblad
 
ASP.NET MVC and Entity Framework 4
James Johnson
 
Generic Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
Real World Dependency Injection - phpday
Stephan Hochdörfer
 
Entity Framework Database and Code First
James Johnson
 
A Tour of EF Core's (1.1) Most Interesting & Important Features
Julie Lerman
 
EF6 or EF Core? How Do I Choose?
Julie Lerman
 
ASP.NET MVC 4 Request Pipeline Internals
Lukasz Lysik
 
Difference between wcf and asp.net web api
Umar Ali
 
ASP.NET MVC 4 - Routing Internals
Lukasz Lysik
 
C# 6.0 - DotNetNotts
citizenmatt
 
Day02 01 Advance Feature in C# DH TDT
Nguyen Patrick
 
Ad

Similar to Repository and Unit Of Work Design Patterns (20)

PDF
An Introduction to Enterprise Design Patterns
Adam Stephensen
 
PPTX
Entity Framework: To the Unit of Work Design Pattern and Beyond
Steve Westgarth
 
PPTX
Entity Framework Today (May 2012)
Julie Lerman
 
PPTX
Inversion of Control and Dependency Injection
Dinesh Sharma
 
PPTX
Entity Framework 4
Stefano Paluello
 
PPTX
Design patterns fast track
Binu Bhasuran
 
PPTX
Trinug - repository pattern
Bhuvnesh Bhatt
 
PPTX
MVC and Entity Framework 4
James Johnson
 
DOCX
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
PDF
黑豹 ch4 ddd pattern practice (2)
Fong Liou
 
PPTX
Domain Driven Design in Rails
Hans Yu
 
PPT
A clean repository pattern in ef
jaxarcsig
 
PDF
Architecting Smarter Apps with Entity Framework
Saltmarch Media
 
PDF
.Net template solution architecture
Diogo Gonçalves da Cunha
 
PPT
A clean repository pattern in ef
MaslowB
 
PPTX
MVC and Entity Framework 4
James Johnson
 
PPTX
L09 The Behavioral Problem
Ólafur Andri Ragnarsson
 
PPTX
Most Useful Design Patterns
Steven Smith
 
PDF
Entity Framework 6 Recipes 2nd Edition Brian Driscoll
raffygobahc9
 
PDF
Doctrine Internals. UnitOfWork
Andrew Yatsenko
 
An Introduction to Enterprise Design Patterns
Adam Stephensen
 
Entity Framework: To the Unit of Work Design Pattern and Beyond
Steve Westgarth
 
Entity Framework Today (May 2012)
Julie Lerman
 
Inversion of Control and Dependency Injection
Dinesh Sharma
 
Entity Framework 4
Stefano Paluello
 
Design patterns fast track
Binu Bhasuran
 
Trinug - repository pattern
Bhuvnesh Bhatt
 
MVC and Entity Framework 4
James Johnson
 
Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
黑豹 ch4 ddd pattern practice (2)
Fong Liou
 
Domain Driven Design in Rails
Hans Yu
 
A clean repository pattern in ef
jaxarcsig
 
Architecting Smarter Apps with Entity Framework
Saltmarch Media
 
.Net template solution architecture
Diogo Gonçalves da Cunha
 
A clean repository pattern in ef
MaslowB
 
MVC and Entity Framework 4
James Johnson
 
L09 The Behavioral Problem
Ólafur Andri Ragnarsson
 
Most Useful Design Patterns
Steven Smith
 
Entity Framework 6 Recipes 2nd Edition Brian Driscoll
raffygobahc9
 
Doctrine Internals. UnitOfWork
Andrew Yatsenko
 
Ad

Recently uploaded (20)

PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Import Data Form Excel to Tally Services
Tally xperts
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Linux Certificate of Completion - LabEx Certificate
VICTOR MAESTRE RAMIREZ
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 

Repository and Unit Of Work Design Patterns

  • 1. Repository and Unit of Work Design Patterns 29th October 2014 Presented By: Hatim Hakeel
  • 3. Repository Object-Relational Metadata Mapping Patterns • Metadata Mapping • Query Object • Repository Repository – Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects 10/29/2014 Page 3
  • 4. What about DAOs ? • A Data Access Object doesn’t hide to the Data Access Layer that it is accessing a data table, unlike a Repository. • A DAO typically has a 1:1 map with a data store table or entity-set. • A Repository acts at a higher level of abstraction working with aggregations of business entities. 10/29/2014 Page 4
  • 5. Why use Repositories ? • Abstraction layer between Business Logic Layer and Data Access Layer. • Insulates application (Controller) from data store changes. • Facilitates automated Unit Testing, Test Driven Development. How ? • Easy to create mock repositories using in-memory domain object collections as a data store. But it’s difficult to mock entities similarly. 10/29/2014 Page 5
  • 6. UoW Object-Relational Behavior Patterns • Unit of Work • Identity Map • Lazy Load UoW – Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems 10/29/2014 Page 6
  • 7. Why use UoW ? • Keeps track of manipulated objects in order to synchronize in-memory data with the data store. • Provides a single transaction for multiple queries. • The UoW commits the transaction. • If the commit fails, rollback. • Single commit call on the database. • All object tracking information is centralized. • Provides a firm means for complex scenarios like handling business transactions that span several system transactions using Optimistic Offline Lock and Pessimistic Offline Lock. 10/29/2014 Page 7
  • 8. Entity Framework 5 and ASP.NET MVC 4 and previous versions… • Repository classes with a UoW class. • Repository classes without a UoW class. • Single Repository. • A Repository class per Entity. 10/29/2014 Page 8
  • 9. The Overall Bigger Picture 10/29/2014 Page 9
  • 10. A possible project structure 10/29/2014 Page 10
  • 14. Persist DbContext changes and clean up resources iii 10/29/2014 Page 14
  • 17. IEnumerable<TEnitity> Get(…) ii 10/29/2014 Page 17
  • 18. Remaining CRUD method implementations iii 10/29/2014 Page 18
  • 19. EF DbContext implementer 10/29/2014 Page 19
  • 20. How does the architectural wiring come live in the controller ? 10/29/2014 Page 20
  • 21. Sample Edit, Get(…) calls 10/29/2014 Page 21
  • 22. Entity Framework 6 and ASP.NET MVC 5 From http://goo.gl/eyDbrM Advanced Entity Framework 6 Scenarios for an MVC 5 Web Application (12 of 12) | ASP.NET MVC Site 10/29/2014 Page 22
  • 23. Do we still need Repository and UoW ? 10/29/2014 Page 23
  • 24. Consider following scenarios without repositories 10/29/2014 Page 24
  • 25. In Summary • Business objects should interface with a business oriented storage service • Data objects should interface with a data oriented service • The repository should be the bridging mediator • BUT the real use case for the Repository pattern is in dealing with multiple persistence services 10/29/2014 Page 25
  • 26. Sample project hosted at GitHub https://github.com/hatimhakeel/UniversityVille 10/29/2014 Page 26
  • 27. Thank You Questions ? 10/29/2014 Page 27