SlideShare a Scribd company logo
Steve Westgarth
Entity Framework - to the Unit of Work Design Pattern and Beyond
Software Developer
Lets start at the beginning
Classic ASP
Lets start at the beginning
ASP.NET Web Forms
Major Issues
• Undefined Application Architecture with lack of Separation of
Concerns (SoC)
• Complex Pages with Performance issues.
• Lack of abstraction with least control over HTML.
• Limited support for testing and SEO.
• Lack of Re-usability and minimal parallel Development.
13th March 2009 – A great day for developers
Model
View
Controller
Release of .NET MVC 1.0
Entity Framework: To the Unit of Work Design Pattern and Beyond
var MVC = new MassiveViewController();
Demo
Major Issues
• Limited Programming / Coding Standards / Agreed Best Practice
• Difficult to Unit Test
• View Controller Bloat
• Difficult to debug
• Difficult to maintain
• Working together is challenging
• Code slow and not responsive
• ….
Entity Framework: To the Unit of Work Design Pattern and Beyond
We Need a Design Pattern!
Separation of Concerns
DAL
Presentation Layer
Model
View
Controller
Improvements We Made
• Strongly Typed Models
• Static Helper Methods for Transforming Model Objects
• DAL Extension Methods
• Model Annotations
• Introduction of a Base Controller
• Agreed Folder Structure
• Dependency Injection
Demo
DAL
(With Repository Pattern?)
Presentation Layer
Model
View
Controller
What’s the Purpose of a Repository Pattern?
• Good for Testing
• Loose Coupling (Easy to Upgrade to New Framework Versions)
• Easily Extend Database Functionality
• Clean Architecture – Independent of Frameworks
• Ability to remove some abilities / features of EF
We Still Had Problems
• Customer Ordering Application
Each DB Context has its own in memory set of changes
If Save() on one Context Succeeds and the Other Fails their can be Database Inconsistency
DAL
With Unit of Work Design Pattern
Presentation Layer
Model
View
Controller
Entity Framework: To the Unit of Work Design Pattern and Beyond
Unit of Work Design Pattern
• Single Transaction – Minimizing Database Calls
• Creates an abstraction layer between the database and the
application
• Facilitates Automated Unit Testing
Demo
Entity Framework: To the Unit of Work Design Pattern and Beyond
SOLID Principles
The first 5 principles of Object Orientated Design
S ingle Responsibility
Each class has one job to do
Open / Closed
Open for Extension but Closed for Modification
L iskov substitution
If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the
desirable properties of T
I nterface Segregation
No client should be forced to depend on methods it does not use
Dependency Inversion
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
Unit of Work Violates a Number of SOLID
Principles
• Nominal Abstraction – Its still closely coupled with Entity Framework
• The DBContext Entity makes it all the way into the application layer
• Violates the Single Responsibility Principle
• Leads to Opaque Dependancies, all dependancies are hard coded into
the UnitOfWork Class making it difficult to extend.
• Violates the Open / Closed Principle
DAL
Presentation Layer
Model
View
Controller
Business Logic Layer
Our Solution
Key Principles
• Data is transformed in each layer into a layer specific model
• A Generic Repositories is Implemented which is then extended for
individual Models – this minimizes code duplication
• Business Logic is totally divorced from the Application Layer – no
database logic operations are performed in the controller or
application classes
• Minimizes View Controller Bloat making code readable
• Encourages adherence to agreed coding standards
Advantages
• Totally Decouples Entity Framework from Presentation Layer
• Makes it possible to adhere easily to the Single Responsibility
Principle
• Adheres to Liskovs Substitution Principle
• Total Interface Segregation
• Adheres to Dependency Inversion
• Entity Framework could be entirely placed with no modification to the
Application Code.
Issues?
• Open / Closed Principle
• The repositories are still hard coded into the Unit of Work Class
• It is possible to extend Unit Of Work as a series of partial classes but it isn’t a
true implementation of the principle.
To Summarise
• Do you have an agreed Design Pattern In Your Company? When did
you last challenge the accepted norm?
• Adhere to the Solid Principles, it makes for cleaner code
• If you use Unit of Work – consider how you can make it better
• If you don’t use Unit of Work explore its merits …. And then consider
how you can make it better.
Steve Westgarth
Entity Framework - to the Unit of Work Design Pattern and Beyond
Code @ github.com/stephenwestgarth/DesignPattern.git
@stevewestgarth on Twitter
Software Developer
Android and iOS Conference
www.codemobile.co.uk
Use Discount Code DOTNETNOTTS to get 25% off

More Related Content

PPTX
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
PPTX
Episode 21 - Design Pattern 1
PPT
Hybrid Automation Framework Developement
PPTX
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
PPTX
Introduction To AOP
PPTX
05 managing transactions
PPTX
5 Considerations When Adopting Automated Testing
PPT
Microservices
Episode 3 – Classes, Inheritance, Abstract Class, and Interfaces
Episode 21 - Design Pattern 1
Hybrid Automation Framework Developement
Model-Based Systems Engineering Tool How To Use Innoslate Pt. 2
Introduction To AOP
05 managing transactions
5 Considerations When Adopting Automated Testing
Microservices

What's hot (20)

PPTX
Improving the Quality of Existing Software
PDF
Introduction to Microsoft Prism
PPT
Test Automation Framework Designs
PPTX
QAorHighway2016
PPTX
Tdd for php
PPTX
Presentation delex
PPTX
Mihai mahulea the zen of test driven development
PPTX
Power-Up Your Test Suite with OLE Automation by Joshua Russell
PPTX
Visual Studio 2010 Testing for Developers
PPTX
Design patterns in test automation
PPTX
Avoiding test hell
PPTX
Lap around ALM with Visual Studio and TFS 2013
PDF
Promoting Agility with Running Tested Features - Lightening Talk
PPTX
Benefits from AATs
PPTX
Visual Studio LightSwitch (Beta 1) Overview
PPT
Odd E验收测试驱动开发实战
PPTX
.NET Frameworks Benchmarking
PDF
Validate Documents with Automated Visual Testing
PDF
Introduction to Test Automation
PPTX
Automation test scripting techniques
Improving the Quality of Existing Software
Introduction to Microsoft Prism
Test Automation Framework Designs
QAorHighway2016
Tdd for php
Presentation delex
Mihai mahulea the zen of test driven development
Power-Up Your Test Suite with OLE Automation by Joshua Russell
Visual Studio 2010 Testing for Developers
Design patterns in test automation
Avoiding test hell
Lap around ALM with Visual Studio and TFS 2013
Promoting Agility with Running Tested Features - Lightening Talk
Benefits from AATs
Visual Studio LightSwitch (Beta 1) Overview
Odd E验收测试驱动开发实战
.NET Frameworks Benchmarking
Validate Documents with Automated Visual Testing
Introduction to Test Automation
Automation test scripting techniques
Ad

Similar to Entity Framework: To the Unit of Work Design Pattern and Beyond (20)

PPTX
Mobile App Architectures & Coding guidelines
PDF
Applying Roles and Profiles method to compliance code
PPTX
Чурюканов Вячеслав, “Code simple, but not simpler”
PDF
Microservices Architecture
PDF
Clean architecture with asp.net core
PPTX
Refactoring Legacy Web Forms for Test Automation
PPTX
Improving the Design of Existing Software
PPTX
Improving the Quality of Existing Software
PPTX
Clean architecture
PPTX
Improving The Quality of Existing Software
PDF
SOLID Design Principles for Test Automaion
PPTX
android principle.pptx
PPTX
Architecture Principles CodeStock
PDF
Solid principles
PPTX
1012892161-Module-4-Agile-Software-Design-and-Development.pptx
PDF
Android architecture
PDF
MVP Clean Architecture
PPT
DesignPrinciples-and-DesignPatterns
PDF
Is your ABAP Code Ready for the Cloud?
PPTX
Segue to design patterns
Mobile App Architectures & Coding guidelines
Applying Roles and Profiles method to compliance code
Чурюканов Вячеслав, “Code simple, but not simpler”
Microservices Architecture
Clean architecture with asp.net core
Refactoring Legacy Web Forms for Test Automation
Improving the Design of Existing Software
Improving the Quality of Existing Software
Clean architecture
Improving The Quality of Existing Software
SOLID Design Principles for Test Automaion
android principle.pptx
Architecture Principles CodeStock
Solid principles
1012892161-Module-4-Agile-Software-Design-and-Development.pptx
Android architecture
MVP Clean Architecture
DesignPrinciples-and-DesignPatterns
Is your ABAP Code Ready for the Cloud?
Segue to design patterns
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Sports Quiz easy sports quiz sports quiz
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPH.pptx obstetrics and gynecology in nursing
Microbial disease of the cardiovascular and lymphatic systems
Sports Quiz easy sports quiz sports quiz
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

Entity Framework: To the Unit of Work Design Pattern and Beyond

  • 1. Steve Westgarth Entity Framework - to the Unit of Work Design Pattern and Beyond Software Developer
  • 2. Lets start at the beginning Classic ASP
  • 3. Lets start at the beginning ASP.NET Web Forms
  • 4. Major Issues • Undefined Application Architecture with lack of Separation of Concerns (SoC) • Complex Pages with Performance issues. • Lack of abstraction with least control over HTML. • Limited support for testing and SEO. • Lack of Re-usability and minimal parallel Development.
  • 5. 13th March 2009 – A great day for developers
  • 8. var MVC = new MassiveViewController();
  • 10. Major Issues • Limited Programming / Coding Standards / Agreed Best Practice • Difficult to Unit Test • View Controller Bloat • Difficult to debug • Difficult to maintain • Working together is challenging • Code slow and not responsive • ….
  • 12. We Need a Design Pattern!
  • 15. Improvements We Made • Strongly Typed Models • Static Helper Methods for Transforming Model Objects • DAL Extension Methods • Model Annotations • Introduction of a Base Controller • Agreed Folder Structure • Dependency Injection
  • 16. Demo
  • 17. DAL (With Repository Pattern?) Presentation Layer Model View Controller
  • 18. What’s the Purpose of a Repository Pattern? • Good for Testing • Loose Coupling (Easy to Upgrade to New Framework Versions) • Easily Extend Database Functionality • Clean Architecture – Independent of Frameworks • Ability to remove some abilities / features of EF
  • 19. We Still Had Problems • Customer Ordering Application Each DB Context has its own in memory set of changes If Save() on one Context Succeeds and the Other Fails their can be Database Inconsistency
  • 20. DAL With Unit of Work Design Pattern Presentation Layer Model View Controller
  • 22. Unit of Work Design Pattern • Single Transaction – Minimizing Database Calls • Creates an abstraction layer between the database and the application • Facilitates Automated Unit Testing
  • 23. Demo
  • 25. SOLID Principles The first 5 principles of Object Orientated Design
  • 26. S ingle Responsibility Each class has one job to do Open / Closed Open for Extension but Closed for Modification L iskov substitution If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of T I nterface Segregation No client should be forced to depend on methods it does not use Dependency Inversion High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
  • 27. Unit of Work Violates a Number of SOLID Principles • Nominal Abstraction – Its still closely coupled with Entity Framework • The DBContext Entity makes it all the way into the application layer • Violates the Single Responsibility Principle • Leads to Opaque Dependancies, all dependancies are hard coded into the UnitOfWork Class making it difficult to extend. • Violates the Open / Closed Principle
  • 29. Key Principles • Data is transformed in each layer into a layer specific model • A Generic Repositories is Implemented which is then extended for individual Models – this minimizes code duplication • Business Logic is totally divorced from the Application Layer – no database logic operations are performed in the controller or application classes • Minimizes View Controller Bloat making code readable • Encourages adherence to agreed coding standards
  • 30. Advantages • Totally Decouples Entity Framework from Presentation Layer • Makes it possible to adhere easily to the Single Responsibility Principle • Adheres to Liskovs Substitution Principle • Total Interface Segregation • Adheres to Dependency Inversion • Entity Framework could be entirely placed with no modification to the Application Code.
  • 31. Issues? • Open / Closed Principle • The repositories are still hard coded into the Unit of Work Class • It is possible to extend Unit Of Work as a series of partial classes but it isn’t a true implementation of the principle.
  • 32. To Summarise • Do you have an agreed Design Pattern In Your Company? When did you last challenge the accepted norm? • Adhere to the Solid Principles, it makes for cleaner code • If you use Unit of Work – consider how you can make it better • If you don’t use Unit of Work explore its merits …. And then consider how you can make it better.
  • 33. Steve Westgarth Entity Framework - to the Unit of Work Design Pattern and Beyond Code @ github.com/stephenwestgarth/DesignPattern.git @stevewestgarth on Twitter Software Developer
  • 34. Android and iOS Conference www.codemobile.co.uk Use Discount Code DOTNETNOTTS to get 25% off