a clean repository pattern in ef

29
A Clean Repository Pattern In EF Via Interfaces By Brandon D’Imperio Imaginarydevelopment.blog spot.com

Upload: maslowb

Post on 06-May-2015

649 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A clean repository pattern in ef

A Clean Repository Pattern In EFVia Interfaces

By Brandon D’ImperioImaginarydevelopment.blogspot.com

Page 2: A clean repository pattern in ef

What’s wrong?

Typical dependency structure

Page 3: A clean repository pattern in ef

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

Page 4: A clean repository pattern in ef

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

Page 5: A clean repository pattern in ef

We need something more adaptable

• We want to be able to swap out these layers with ease, or even on the fly.

Page 6: A clean repository pattern in ef

What about this?

Dependency-free foundation

Page 7: A clean repository pattern in ef

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)

Page 8: A clean repository pattern in ef

Code Break

Page 9: A clean repository pattern in ef

The Visitors are here

Page 10: A clean repository pattern in ef

Sample Query in EF context

Page 11: A clean repository pattern in ef

Performance left join and where

Page 12: A clean repository pattern in ef

Performance Code

Page 13: A clean repository pattern in ef

Performance select and where id> i take(50)

Straight EF average: 103.811msIntercepted average: 220.3158ms

Page 14: A clean repository pattern in ef

More PerformanceStraight EF Average=1190.3msIntercepted Average=1274.43 ms

Page 15: A clean repository pattern in ef

Domain Layer - How?

• Constructor Injection• Depends ONLY on the shared interfaces (and

sometimes DTOs)• No state, only behavior

Page 16: A clean repository pattern in ef

Domain Code

Page 17: A clean repository pattern in ef

Domain Code – public sample

Page 18: A clean repository pattern in ef

Domain Code – public sample

Page 19: A clean repository pattern in ef

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)

Page 20: A clean repository pattern in ef

UI layer - How?

• Controller requires appropriate Model(s)• No dependencies on the data layer

Page 21: A clean repository pattern in ef

UI layer – controller code

Page 22: A clean repository pattern in ef

Create - post

Page 23: A clean repository pattern in ef

Delete and Edit

Page 24: A clean repository pattern in ef

Edit - post

Page 25: A clean repository pattern in ef

View sampleCreate + Edit

Page 26: A clean repository pattern in ef

Edit scripts

Page 27: A clean repository pattern in ef

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/

Page 28: A clean repository pattern in ef

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

Page 29: A clean repository pattern in ef

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