SlideShare a Scribd company logo
A Clean Repository Pattern In EF
         Via Interfaces
         By Brandon D’Imperio
  Imaginarydevelopment.blogspot.com
What’s wrong?
Typical dependency structure
What’s wrong?
                               We want to move to Azure
Typical dependency structure




                               We want to add a thick client against ADO for
                               our existing web to Azure.
What’s wrong?
                               We want to move to Azure
Typical dependency structure       Persistence change = somewhere
                                   between massive rework to lots of code
                                   adjustments


                               We want to add a thick client against ADO for
                               our existing web to Azure.
                                    UI against multiple data layers=
                                    somewhere between massive rework and
                                    lots of searching for dependencies
We need something more adaptable
• We want to be able to swap out these layers
  with ease, or even on the fly.
What about this?




Dependency-free foundation
How?
• EF layer
  – Entities implement shared interfaces
  – Context implements IRepository<T>
  – T4 for automation
  – Query Interception
     • EF does not allow nor handle IQueryable<T> where T:
       (parent to an Entity)
Code Break
The Visitors are here
Sample Query in EF context
Performance
left join and where
Performance Code
Performance
select and where id> i take(50)



                Straight EF average: 103.811ms
                Intercepted average: 220.3158ms
More Performance
Straight EF Average=1190.3ms
Intercepted Average=1274.43 ms
Domain Layer - How?
• Constructor Injection
• Depends ONLY on the shared interfaces (and
  sometimes DTOs)
• No state, only behavior
Domain Code
Domain Code – public sample
Domain Code – public sample
Domain – Data Layer check-mate
SELECT
[Extent2].[ID] AS [ID],
[Extent2].[DealID] AS [DealID],
[Extent2].[ScenarioNumber] AS [ScenarioNumber],
[Extent2].[ServiceCodeID] AS [ServiceCodeID],
[Extent2].[CountryCode] AS [CountryCode],
[Extent2].[CurrencyCode] AS [CurrencyCode],
[Extent2].[PriceListID] AS [PriceListID],
[Extent2].[BranchID] AS [BranchID],
[Extent2].[ExchangeRate] AS [ExchangeRate],
[Extent2].[Volume] AS [Volume],
[Extent2].[ActualPrice] AS [ActualPrice],
[Extent2].[CurrentPrice] AS [CurrentPrice],
[Extent2].[StandardPrice] AS [StandardPrice],
[Extent2].[MinVol] AS [MinVol],
[Extent2].[MaxVol] AS [MaxVol],
FROM [Treasury].[Deal] AS [Extent1]
INNER JOIN [Treasury].[DealActivity] AS [Extent2] ON [Extent1].[DealID] = [Extent2].[DealID]
WHERE [Extent1].[RegionID] IN (5,1,6,4,2)
UI layer - How?
• Controller requires appropriate Model(s)
• No dependencies on the data layer
UI layer – controller code
Create - post
Delete and Edit
Edit - post
View sample
Create + Edit
Edit scripts
Technologies utilized
• Mvc3 – Now open source! -
  http://aspnet.codeplex.com/
• T4 -
  http://en.wikipedia.org/wiki/Text_Template_
  Transformation_Toolkit
  – T4Mvc - https://t4mvc.codeplex.com/
• Ninject – http://ninject.org
• jQuery - http://jquery.com/
• IQToolkit - http://iqtoolkit.codeplex.com/
References
• Orm speed comparison -
  http://code.google.com/p/dapper-dot-net/
• Open Stackoverflow question -
  http://stackoverflow.com/questions/9422789
  /can-you-program-against-interfaces-in-ef-if-
  you-do-not-make-use-of-navigation-pr
Available SideBar or presentations
• Static Reflection
• A maybe monad in .net
   – http://maybe.codeplex.com
• Unit Testing
   – Asp.net Routing
• T4
   – DTO generation
• Hybrid Winforms/Mvc application
   – Hybrid winforms+mvc pages
• Mvc
   – Inline templates
   – Custom templates

More Related Content

PPT
A clean repository pattern in ef
PDF
Pragmatic Event Driven Microservices
PPTX
Whats New In Mvc 2
PDF
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
PPTX
Entity Framework 4
PPTX
Common ASP.NET Design Patterns - Telerik India DevCon 2013
PPT
Entity Framework 4 In Microsoft Visual Studio 2010
PPTX
Real World API Design Using The Entity Framework Services
A clean repository pattern in ef
Pragmatic Event Driven Microservices
Whats New In Mvc 2
[FREE PDF sample] Programming Entity Framework DbContext 1st Edition Julia Le...
Entity Framework 4
Common ASP.NET Design Patterns - Telerik India DevCon 2013
Entity Framework 4 In Microsoft Visual Studio 2010
Real World API Design Using The Entity Framework Services

Similar to A clean repository pattern in ef (20)

PDF
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
DOCX
Learning MVC Part 3 Creating MVC Application with EntityFramework
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
PPTX
Building nTier Applications with Entity Framework Services (Part 1)
PPTX
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
PPTX
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
PDF
Entity Framework Core Cookbook 2nd Edition Ricardo Peres
PPTX
Entity Framework: Code First and Magic Unicorns
PPTX
Building Enterprise Apps with ASP.NET Core 2.1
PDF
14147503 Intentions Interfaces Making Patterns Concrete
PPTX
Hard Coding as a design approach
PPTX
Entity Framework Today (May 2012)
DOC
10265 developing data access solutions with microsoft visual studio 2010
PPTX
Building nTier Applications with Entity Framework Services (Part 2)
PDF
Clean architecture with asp.net core
PDF
70487.pdf
PPT
Ado.Net Data Services (Astoria)
PDF
.Net template solution architecture
PDF
Entity Framework 6 Recipes 2nd Edition Brian Driscoll
PPTX
Application Architecture
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Learning MVC Part 3 Creating MVC Application with EntityFramework
Building nTier Applications with Entity Framework Services (Part 1)
Building nTier Applications with Entity Framework Services (Part 1)
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
Entity Framework Core Cookbook 2nd Edition Ricardo Peres
Entity Framework: Code First and Magic Unicorns
Building Enterprise Apps with ASP.NET Core 2.1
14147503 Intentions Interfaces Making Patterns Concrete
Hard Coding as a design approach
Entity Framework Today (May 2012)
10265 developing data access solutions with microsoft visual studio 2010
Building nTier Applications with Entity Framework Services (Part 2)
Clean architecture with asp.net core
70487.pdf
Ado.Net Data Services (Astoria)
.Net template solution architecture
Entity Framework 6 Recipes 2nd Edition Brian Driscoll
Application Architecture
Ad

More from MaslowB (9)

PPTX
F# for BLOBA, by brandon d'imperio
PDF
Knockout vs. angular
PDF
Js testing
PPTX
Type mock isolator
PPTX
What’s new mvc 4
PPTX
Metrics
PPTX
Type mock isolator
PPTX
Mvc presentation
PPT
Metaprogramming by brandon
F# for BLOBA, by brandon d'imperio
Knockout vs. angular
Js testing
Type mock isolator
What’s new mvc 4
Metrics
Type mock isolator
Mvc presentation
Metaprogramming by brandon
Ad

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
cuic standard and advanced reporting.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Weekly Chronicles - August'25 Week I
“AI and Expert System Decision Support & Business Intelligence Systems”
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
cuic standard and advanced reporting.pdf

A clean repository pattern in ef

  • 1. A Clean Repository Pattern In EF Via Interfaces By Brandon D’Imperio Imaginarydevelopment.blogspot.com
  • 3. What’s wrong? We want to move to Azure Typical dependency structure We want to add a thick client against ADO for our existing web to Azure.
  • 4. What’s wrong? We want to move to Azure Typical dependency structure Persistence change = somewhere between massive rework to lots of code adjustments We want to add a thick client against ADO for our existing web to Azure. UI against multiple data layers= somewhere between massive rework and lots of searching for dependencies
  • 5. We need something more adaptable • We want to be able to swap out these layers with ease, or even on the fly.
  • 7. How? • EF layer – Entities implement shared interfaces – Context implements IRepository<T> – T4 for automation – Query Interception • EF does not allow nor handle IQueryable<T> where T: (parent to an Entity)
  • 10. Sample Query in EF context
  • 13. Performance select and where id> i take(50) Straight EF average: 103.811ms Intercepted average: 220.3158ms
  • 14. More Performance Straight EF Average=1190.3ms Intercepted Average=1274.43 ms
  • 15. Domain Layer - How? • Constructor Injection • Depends ONLY on the shared interfaces (and sometimes DTOs) • No state, only behavior
  • 17. Domain Code – public sample
  • 18. Domain Code – public sample
  • 19. Domain – Data Layer check-mate SELECT [Extent2].[ID] AS [ID], [Extent2].[DealID] AS [DealID], [Extent2].[ScenarioNumber] AS [ScenarioNumber], [Extent2].[ServiceCodeID] AS [ServiceCodeID], [Extent2].[CountryCode] AS [CountryCode], [Extent2].[CurrencyCode] AS [CurrencyCode], [Extent2].[PriceListID] AS [PriceListID], [Extent2].[BranchID] AS [BranchID], [Extent2].[ExchangeRate] AS [ExchangeRate], [Extent2].[Volume] AS [Volume], [Extent2].[ActualPrice] AS [ActualPrice], [Extent2].[CurrentPrice] AS [CurrentPrice], [Extent2].[StandardPrice] AS [StandardPrice], [Extent2].[MinVol] AS [MinVol], [Extent2].[MaxVol] AS [MaxVol], FROM [Treasury].[Deal] AS [Extent1] INNER JOIN [Treasury].[DealActivity] AS [Extent2] ON [Extent1].[DealID] = [Extent2].[DealID] WHERE [Extent1].[RegionID] IN (5,1,6,4,2)
  • 20. UI layer - How? • Controller requires appropriate Model(s) • No dependencies on the data layer
  • 21. UI layer – controller code
  • 27. Technologies utilized • Mvc3 – Now open source! - http://aspnet.codeplex.com/ • T4 - http://en.wikipedia.org/wiki/Text_Template_ Transformation_Toolkit – T4Mvc - https://t4mvc.codeplex.com/ • Ninject – http://ninject.org • jQuery - http://jquery.com/ • IQToolkit - http://iqtoolkit.codeplex.com/
  • 28. References • Orm speed comparison - http://code.google.com/p/dapper-dot-net/ • Open Stackoverflow question - http://stackoverflow.com/questions/9422789 /can-you-program-against-interfaces-in-ef-if- you-do-not-make-use-of-navigation-pr
  • 29. Available SideBar or presentations • Static Reflection • A maybe monad in .net – http://maybe.codeplex.com • Unit Testing – Asp.net Routing • T4 – DTO generation • Hybrid Winforms/Mvc application – Hybrid winforms+mvc pages • Mvc – Inline templates – Custom templates

Editor's Notes

  • #2: IRepository, Entity Framework, T4, Front-To-Back
  • #3: Ask for input, what difficulties could this structure cause?
  • #4: Linq to Entities, vs. Linq to Objects, vs. Linq to Azure?
  • #5: Linq to Entities, vs. Linq to Objects, vs. Linq to Azure?
  • #7: Mark Seemann - http://blog.ploeh.dk/
  • #9: When I serialized out an entity, properties that were non-poco came with. Translate anonymous types in this layer
  • #22: Does not rely on model directly – sample for ideas, not completely clean