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_Tran
  – 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
• Dependency Injection in .net by Mark
  Seemann - http://manning.com/seemann
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 3/4
   – Inline templates
   – Custom templates
• XNA
   – http://PitofDespair.codeplex.com

A clean repository pattern in ef

  • 1.
    A Clean RepositoryPattern In EF Via Interfaces By Brandon D’Imperio Imaginarydevelopment.blogspot.com
  • 2.
  • 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 somethingmore adaptable • We want to be able to swap out these layers with ease, or even on the fly.
  • 6.
  • 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.
  • 9.
  • 10.
    Sample Query inEF context
  • 11.
  • 12.
  • 13.
    Performance select and whereid> i take(50) Straight EF average: 103.811ms Intercepted average: 220.3158ms
  • 14.
    More Performance Straight EFAverage=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
  • 16.
  • 17.
    Domain Code –public sample
  • 18.
    Domain Code –public sample
  • 19.
    Domain – DataLayer 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
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
    Technologies utilized • Mvc3– Now open source! - http://aspnet.codeplex.com/ • T4 - http://en.wikipedia.org/wiki/Text_Template_Tran – T4Mvc - https://t4mvc.codeplex.com/ • Ninject – http://ninject.org • jQuery - http://jquery.com/ • IQToolkit - http://iqtoolkit.codeplex.com/
  • 28.
    References • Orm speedcomparison - http://code.google.com/p/dapper-dot-net/ • Open Stackoverflow question - http://stackoverflow.com/questions/9422789/can • Dependency Injection in .net by Mark Seemann - http://manning.com/seemann
  • 29.
    Available Sidebar orpresentations • 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 3/4 – Inline templates – Custom templates • XNA – http://PitofDespair.codeplex.com

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