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 AzureTypical dependency structure                          ...
What’s wrong?                               We want to move to AzureTypical dependency structure       Persistence change ...
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 ...
Code Break
The Visitors are here
Sample Query in EF context
Performanceleft join and where
Performance Code
Performanceselect and where id> i take(50)                Straight EF average: 103.811ms                Intercepted averag...
More PerformanceStraight EF Average=1190.3msIntercepted Average=1274.43 ms
Domain Layer - How?• Constructor Injection• Depends ONLY on the shared interfaces (and  sometimes DTOs)• No state, only be...
Domain Code
Domain Code – public sample
Domain Code – public sample
Domain – Data Layer check-mateSELECT[Extent2].[ID] AS [ID],[Extent2].[DealID] AS [DealID],[Extent2].[ScenarioNumber] AS [S...
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 sampleCreate + Edit
Edit scripts
Technologies utilized• Mvc3 – Now open source! -  http://aspnet.codeplex.com/• T4 -  http://en.wikipedia.org/wiki/Text_Tem...
References• Orm speed comparison -  http://code.google.com/p/dapper-dot-net/• Open Stackoverflow question -  http://stacko...
Available SideBar or presentations• Static Reflection• A maybe monad in .net   – http://maybe.codeplex.com• Unit Testing  ...
Upcoming SlideShare
Loading in …5
×

A clean repository pattern in ef

888 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
888
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
6
Comments
0
Likes
2
Embeds 0
No embeds

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

    1. 1. A Clean Repository Pattern In EF Via Interfaces By Brandon D’Imperio Imaginarydevelopment.blogspot.com
    2. 2. What’s wrong?Typical dependency structure
    3. 3. What’s wrong? We want to move to AzureTypical dependency structure We want to add a thick client against ADO for our existing web to Azure.
    4. 4. What’s wrong? We want to move to AzureTypical 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. 5. We need something more adaptable• We want to be able to swap out these layers with ease, or even on the fly.
    6. 6. What about this?Dependency-free foundation
    7. 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)
    8. 8. Code Break
    9. 9. The Visitors are here
    10. 10. Sample Query in EF context
    11. 11. Performanceleft join and where
    12. 12. Performance Code
    13. 13. Performanceselect and where id> i take(50) Straight EF average: 103.811ms Intercepted average: 220.3158ms
    14. 14. More PerformanceStraight EF Average=1190.3msIntercepted Average=1274.43 ms
    15. 15. Domain Layer - How?• Constructor Injection• Depends ONLY on the shared interfaces (and sometimes DTOs)• No state, only behavior
    16. 16. Domain Code
    17. 17. Domain Code – public sample
    18. 18. Domain Code – public sample
    19. 19. Domain – Data Layer check-mateSELECT[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. 20. UI layer - How?• Controller requires appropriate Model(s)• No dependencies on the data layer
    21. 21. UI layer – controller code
    22. 22. Create - post
    23. 23. Delete and Edit
    24. 24. Edit - post
    25. 25. View sampleCreate + Edit
    26. 26. Edit scripts
    27. 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. 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. 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

    ×