Entity Framework 4POCO & Persistence Ignorance<br />Julie Lerman<br />thedatafarm.com<br />jlerman@thedatafarm.com<br />Tw...
Julie Lerman<br />website<br />theDataFarm.com<br />blog & twitter<br />theDataFarm.com/blog@julielerman<br />book web sit...
    Sponsored by INETA	  The Face of .NET<br />World-wide association of .NET User Groups<br />860+ groups world-wide<br /...
Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />Persistence Dependent<br />var context = new PublishingEntities();<b...
EF1 & Default EF4 Classes<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
POCO: Shifting the Responsibility<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />?<br />ObjectContext<br />Con...
ObjectContext Manages Entities<br />POCO<br />“Snapshot”4.0<br />POCO<br />+ Dynamic Runtime Proxy <br />(4.0)<br />Entity...
Basic POCO Support<br />Turn off code generation from EDMX<br />“Convention” will auto-map classes/entities<br />Julie Ler...
POCO and Other EF Features<br />Complex Types: Supported<br />Use a class (no structs) to represent the type<br />Explicit...
Rules for Proxy Notification<br />All properties  of the model entity must be virtual<br />Use ICollection<T> for child co...
POCO Classes from Model<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />T4<br />Text Template Transformation To...
P.I.?<br />Huh?<br />EF1<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
EF4<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
EF4 POCO Support<br />All entity properties must be represented<br />Hand Code<br />Code Gen with T4<br />Snapshot<br />Ob...
IObjectSet for Separation<br />Set with Collection-like behavior<br />Add, Attach, Delete, etc.<br />ObjectSet<br />Concre...
Classes<br />PI/Repositories<br />System.Data.Entity<br />Fake<br />ObjectSet<br />Fake<br />Context & Data A<br />EDM &<b...
Entity Framework 4<br />Supports POCO<br />Separation of concerns<br />Unit Testing<br />Coming soon:<br />Code First<br /...
Resources<br />msdn.com/data<br />Learnentityframework.com<br />Testability and Entity Framework 4.0,Scott Allen, http://m...
Julie Lerman<br />website<br />theDataFarm.com<br />blog & twitter<br />theDataFarm.com/blog@julielerman<br />book web sit...
Upcoming SlideShare
Loading in …5
×

Persistence Ignorance in Entity Framework 4, Julie Lerman

4,805 views

Published on

Slides from NDC 2010 (June 2010) and Cleveland .NET SIG (Aug. 2010).
POCO, Persistence Ignorance, Repositories, Unit Testing and overall better architecture using Entity Framework 4

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,805
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Persistence Ignorance in Entity Framework 4, Julie Lerman

  1. 1. Entity Framework 4POCO & Persistence Ignorance<br />Julie Lerman<br />thedatafarm.com<br />jlerman@thedatafarm.com<br />Twitter @julielerman<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  2. 2. Julie Lerman<br />website<br />theDataFarm.com<br />blog & twitter<br />theDataFarm.com/blog@julielerman<br />book web site<br />LearnEntityFramework.com<br />consultant/mentor<br />Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader<br />2nd edition<br />in August<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  3. 3. Sponsored by INETA The Face of .NET<br />World-wide association of .NET User Groups<br />860+ groups world-wide<br />Over 810,000 developers<br />Started and run by .NET User Group leaders<br />Sponsors and supports both .NET and other professional user groups all around the world<br />Sponsors<br />Microsoft<br />New sponsors welcome!<br />North America (NorAm) INETA Speaker Bureau consists of 86 world-renowned speakers<br />www.ineta.org<br />Julie Lerman, May 11 2010, for HUNTUG<br />
  4. 4. Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />Persistence Dependent<br />var context = new PublishingEntities();<br />var book= (from b in context select books where bookid=3).First();<br />b.Status = “Sent to Printer”;<br />Context.SaveChanges();<br />Persistence Ignorance<br />varbookMgr = new BookManager()<br />var book=bookMgr.GetBook(3);<br />book.Status=“Sent to Printer”;<br />bookMgr.Update(book);<br />
  5. 5. EF1 & Default EF4 Classes<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  6. 6. POCO: Shifting the Responsibility<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />?<br />ObjectContext<br />Context.DetectChanges<br />POCO<br />Classes<br />“Snapshot”<br />OR<br />EntityObject Proxy<br />“Proxy Notification”<br />
  7. 7. ObjectContext Manages Entities<br />POCO<br />“Snapshot”4.0<br />POCO<br />+ Dynamic Runtime Proxy <br />(4.0)<br />EntityObject<br />(3.5 & 4.0)<br />ObjectContext<br />Person Object<br />State info<br />State info<br />Order Object<br />State info<br />Detail Object<br />State info<br />Detail Object<br />State info<br />Order Object<br />State info<br />Detail Object<br />State info<br />Detail Object<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  8. 8. Basic POCO Support<br />Turn off code generation from EDMX<br />“Convention” will auto-map classes/entities<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  9. 9. POCO and Other EF Features<br />Complex Types: Supported<br />Use a class (no structs) to represent the type<br />Explicit Loading<br />ObjectContext.LoadProperty(myObj,”PropertyName”)<br />ObjectCOntext.LoadProperty(myObj, o=>o.property)<br />Lazy Loading (new to EF4!)<br />Declare property as virtual<br />EF uses a proxy instance at runtime to load<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  10. 10. Rules for Proxy Notification<br />All properties of the model entity must be virtual<br />Use ICollection<T> for child collections<br />ObjectContext.CreateObject<T> for new<br />Class cannot be sealed<br />Navigation props cannot be sealed<br />Class cannot be abstract <br />Constructor with no parameters<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  11. 11. POCO Classes from Model<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />T4<br />Text Template Transformation Toolkit<br />
  12. 12. P.I.?<br />Huh?<br />EF1<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  13. 13. EF4<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  14. 14. EF4 POCO Support<br />All entity properties must be represented<br />Hand Code<br />Code Gen with T4<br />Snapshot<br />ObjectContext does all of the work<br />Lazy Loading<br />Virtual navigation properties<br />Dynamic Runtime Proxies<br />Every single property is virtual<br />Proxy emulates EntityObject behavior<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  15. 15. IObjectSet for Separation<br />Set with Collection-like behavior<br />Add, Attach, Delete, etc.<br />ObjectSet<br />Concrete class<br />Implements IObjectSet<br />Inherits ObjectQuery<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  16. 16. Classes<br />PI/Repositories<br />System.Data.Entity<br />Fake<br />ObjectSet<br />Fake<br />Context & Data A<br />EDM &<br />ObjectContext<br />Fake<br />Context & Data B<br />Unit of Work<br />Class<br />Repositories<br />Class<br />Repositories<br />Class<br />Repositories<br />Class<br />Repositories<br />UI<br />Tests<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  17. 17. Entity Framework 4<br />Supports POCO<br />Separation of concerns<br />Unit Testing<br />Coming soon:<br />Code First<br />Currently part of CTP4, not part of .NET 4<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  18. 18. Resources<br />msdn.com/data<br />Learnentityframework.com<br />Testability and Entity Framework 4.0,Scott Allen, http://msdn.microsoft.com/en-us/ff714955.aspx<br />Agile Entity Framework 4 Repository: blog series<br />http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-1-model-and-poco-classes/<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />
  19. 19. Julie Lerman<br />website<br />theDataFarm.com<br />blog & twitter<br />theDataFarm.com/blog@julielerman<br />book web site<br />LearnEntityFramework.com<br />consultant/mentor<br />Microsoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader<br />2nd edition<br />in August<br />Julie Lerman, August 10, 2010 Cleveland .NET SIg<br />

×