A clean repository pattern in ef

Post on 06-May-2015

1,431 views 1 download

transcript

A Clean Repository Pattern In EFVia Interfaces

By Brandon D’ImperioImaginarydevelopment.blogspot.com

What’s wrong?

Typical dependency structure

What’s wrong?

We want to add a thick client against ADO for our existing web to Azure.

We want to move to Azure

Typical dependency structure

What’s wrong?

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 want to move to AzurePersistence change = somewhere between massive rework to lots of code adjustments

Typical dependency structure

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.811msIntercepted average: 220.3158ms

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 behavior

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 [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 sampleCreate + 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

• 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