SlideShare a Scribd company logo
1 of 29
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

Similar to A clean repository pattern in ef

Similar to A clean repository pattern in ef (20)

Devoxx London 2017 - Rethinking Services With Stateful Streams
Devoxx London 2017 - Rethinking Services With Stateful StreamsDevoxx London 2017 - Rethinking Services With Stateful Streams
Devoxx London 2017 - Rethinking Services With Stateful Streams
 
Django è pronto per l'Enterprise
Django è pronto per l'EnterpriseDjango è pronto per l'Enterprise
Django è pronto per l'Enterprise
 
Accelerate your Application Delivery with DevOps and Microservices
Accelerate your Application Delivery with DevOps and MicroservicesAccelerate your Application Delivery with DevOps and Microservices
Accelerate your Application Delivery with DevOps and Microservices
 
There is NO CLOUD: For Non-Geeks
There is NO CLOUD: For Non-GeeksThere is NO CLOUD: For Non-Geeks
There is NO CLOUD: For Non-Geeks
 
Lets focus on business value
Lets focus on business valueLets focus on business value
Lets focus on business value
 
Lets focus on business value
Lets focus on business valueLets focus on business value
Lets focus on business value
 
Mini-Track: Lessons from Public Cloud
Mini-Track: Lessons from Public CloudMini-Track: Lessons from Public Cloud
Mini-Track: Lessons from Public Cloud
 
JAX London Slides
JAX London SlidesJAX London Slides
JAX London Slides
 
AWS Core Services Overview, Immersion Day Huntsville 2019
AWS Core Services Overview, Immersion Day Huntsville 2019AWS Core Services Overview, Immersion Day Huntsville 2019
AWS Core Services Overview, Immersion Day Huntsville 2019
 
04 managing the database
04   managing the database04   managing the database
04 managing the database
 
Stephane Lapointe & Alexandre Brisebois: Développer des microservices avec Se...
Stephane Lapointe & Alexandre Brisebois: Développer des microservices avec Se...Stephane Lapointe & Alexandre Brisebois: Développer des microservices avec Se...
Stephane Lapointe & Alexandre Brisebois: Développer des microservices avec Se...
 
There is NO CLOUD: Geeky Version
There is NO CLOUD: Geeky VersionThere is NO CLOUD: Geeky Version
There is NO CLOUD: Geeky Version
 
Impact 2013 2971 - Fundamental integration and service patterns
Impact 2013 2971 - Fundamental integration and service patternsImpact 2013 2971 - Fundamental integration and service patterns
Impact 2013 2971 - Fundamental integration and service patterns
 
AWS vs. Azure
AWS vs. AzureAWS vs. Azure
AWS vs. Azure
 
Build on AWS: Migrating and Platforming
Build on AWS: Migrating and PlatformingBuild on AWS: Migrating and Platforming
Build on AWS: Migrating and Platforming
 
Why NBC Universal Migrated to MongoDB Atlas
Why NBC Universal Migrated to MongoDB AtlasWhy NBC Universal Migrated to MongoDB Atlas
Why NBC Universal Migrated to MongoDB Atlas
 
Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy
 
The Data Dichotomy- Rethinking the Way We Treat Data and Services
The Data Dichotomy- Rethinking the Way We Treat Data and ServicesThe Data Dichotomy- Rethinking the Way We Treat Data and Services
The Data Dichotomy- Rethinking the Way We Treat Data and Services
 
An introduction to AWS Direct Connect
An introduction to AWS Direct ConnectAn introduction to AWS Direct Connect
An introduction to AWS Direct Connect
 
Strata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data DichotomyStrata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data Dichotomy
 

More from MaslowB (9)

F# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperioF# for BLOBA, by brandon d'imperio
F# for BLOBA, by brandon d'imperio
 
Knockout vs. angular
Knockout vs. angularKnockout vs. angular
Knockout vs. angular
 
Js testing
Js testingJs testing
Js testing
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
What’s new mvc 4
What’s new mvc 4What’s new mvc 4
What’s new mvc 4
 
Metrics
MetricsMetrics
Metrics
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Mvc presentation
Mvc presentationMvc presentation
Mvc presentation
 
Metaprogramming by brandon
Metaprogramming by brandonMetaprogramming by brandon
Metaprogramming by brandon
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.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

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