Entity Framework NYC Firestarter

4,256 views

Published on

On Sept 27, 2010, Julie Lerman, Rachel Appel and Steve Bohlen presented a full day of Entity Framework presentations to help developers get started with EF. The presentations begin with "why entity framework" and end with creating better architectures when using EF.

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
  • plz open it for download
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
4,256
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Entity Framework NYC Firestarter

  1. 1. NYC<br />Entity Framework<br />Firestarter<br />
  2. 2. Speakers<br />
  3. 3. Entity Framework Firestarter<br />Schedule<br />
  4. 4. Thanks Microsoft<br />For Hosting and Lunch<br />
  5. 5. EF NYC Firestarter<br />Part 1: It’s Time to Look at Entity Framework<br />Julie Lerman<br />thedatafarm.com/blog<br />twitter @julielerman<br />
  6. 6. 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 />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  7. 7. Why Entity Framework?<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />Object Relational Mapper<br />ADO.NET<br />Data<br />Set<br />ADO<br />Record<br />Set<br />Entity<br />Framework<br />Object<br />Spaces<br />
  8. 8. Design Time: EDMX<br />EF Metadata vs Other ORMs<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />DB<br />DB<br />EF Runtime Metadata (XML files)<br />Conceptual Model<br />Schema<br />Database<br />Structure<br />Schema<br />Designer Layout & Positioning<br />Typical ORM<br />Objects<br />
  9. 9. ADO.NET Today & Tomorrow<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />DataSet<br />Entity Framework<br />LINQ to SQL<br />
  10. 10. Model in Entity Framework<br />Model<br />First<br />Database<br />First<br />New<br />Database<br />Legacy<br />Database<br />Code<br />First<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />“M”<br />
  11. 11. Entity Framework<br />Conceptual Model in your Apps<br />UI<br />DB<br />Conceptual<br />Model<br />of your Domain<br />App Logic<br />Provider<br />Query<br />Track Changes<br />Repository<br />Updates to DB<br />Business<br />Objects<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  12. 12. Time to Code<br />A little modeling…<br />A little querying…<br />A little editing…<br />A little saving…<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  13. 13. FAQS<br />Stored Procedure Support<br />Use Views & Stored Procs – no table access<br />Update the model when DB changes<br />Mappings are highly customizable<br />Inheritance<br />Many to Many<br />Merge tables<br />Split tables<br />etc<br />Pass through queries & commands<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  14. 14. Entity Framework Everywhere<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  15. 15. Why Entity Framework Summary<br />Microsoft’s in-the-box ORM<br />Focus of Microsoft/.NET data access<br />Use any back end db (with a provider)<br />Use in any type of .NET application<br />Backbone of many Microsoft technologies<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  16. 16.  Part 2:What’s new in Entity Framework 4<br />EF NYC Firestarter<br />Steve Bohlen<br />unhandled-exceptions.com/blog<br />twitter @sbohlen<br />
  17. 17. Long Form: Comprehensive<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />Model-first development<br />Automatic pluralization<br />Foreign keys in models<br />POCO class support<br />Lazy loading<br />T4 Code Generation<br />Template customization<br />IObjectSet<br />Virtual SaveChanges<br />ObjectStateManagercontrol<br />Self-tracking entities<br />SQL generation improvements<br />More LINQ operator support<br />LINQ extensibility<br />ExecuteStoreQuery<br />ExecuteStoreCommand<br />SPROC import improvements<br />Model defined functions<br />WPF designer integration<br />Code-First development (Feature CTP)<br />
  18. 18. Short Form: Highlights<br />Designer Improvements<br />Foreign Keys<br />New Code Generation<br />Persistence Ignorance<br />Change Tracking for WCF<br />Better n-tier Support<br />Lazy Loading<br />Code-Only Development<br />
  19. 19. Designer Improvements<br />Pluralization Support<br />Generate Database from Model<br />vs. only Reverse-Engineering in v1<br />Improved SPROC Support<br />SPROCs can return only entities in v1<br />SPROCs can return strongly-typed projections in v4<br />
  20. 20. Foreign Key Support<br />ERM vs exposed FKs (it’s a CHOICE via the dialog box)<br />Does NOT replace the Nav properties<br />DataContext is able to leverage this for some efficiencies<br />Changing either the FK or the Nav property changes the other value (due to code-gen support)<br />
  21. 21. Code Generation<br />Model  templates  Classes<br />Code Generation Engine now T4 instead of proprietary engine in v1<br />Tools support/syntax support resources <br />review a quick look at the T4 syntax<br />T4 Editor add-in<br />
  22. 22. (better) Persistence Ignorance<br />EntityObject inheritance NOT required<br />Snapshot-based<br />No lazy loading support in this approach<br />No Change Tracking<br />must call ObjectContext.DetectChanges( )<br />ObjectContext.Save( ) implicitly calls .DetectChanges( )<br />Proxy-based<br />virtual keyword is the ‘flag’ for this behavior<br />virtual on all  change-tracking and lazy-load<br />virtual on just navigation props  only lazy-loading<br />Several Canned Templates<br />
  23. 23. WCF and Self-Tracking Entities<br />T4 template for self-tracking entities<br />Passing objects across serialization boundaries<br />
  24. 24. Lazy Loading<br />v1 supported only explicit load of lazy collections<br />ObjectContext.ContextOptions.LazyLoadingEnabled = true<br />by default in NEW models, but NOT in imported v1 models<br />Behavior scoped at the ObjectContext<br />NOT at property level for entities<br />Toggle-able before and after operations for query-specific granular lazy-loading behavior<br />
  25. 25. Explicit State Management<br />ObjectContext.ApplyCurrentValues( )<br />Formerly ApplyPropertyChanges( ) in v1<br />ObjectContext.ApplyOriginalValues( )<br />ObjectStateEntry.ChangeState( )<br />Unchanged/Added/Modified/Deleted<br />ObjectStateManager.ChangeRelationshipState( )<br />
  26. 26. Code-Only Support<br />‘Feature CTP’<br />Classes  Entities<br />Entities  Database (optionally)<br />No Model Necessary<br />Heavily Convention over Configuration<br />
  27. 27. Miscellaneous<br />Model-Defined Functions<br />ObjectSet (collections of entities)<br />Complex Type Support<br />Query optimization (E-SQL, LINQ2Entities)<br />ExecuteCommand/Defined Queries/Model Functions<br />Designer support for more features without direct editing of .csdl, .ssdl, .edml<br />
  28. 28.  Part 4: Getting Started with POCOs in EF4<br />EF NYC Firestarter<br />Julie Lerman<br />thedatafarm.com/blog<br />twitter @julielerman<br />
  29. 29. Agenda<br />Simple POCO Classes<br />Leveraging Proxies<br />Customizable T4 Code Generation<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  30. 30. POCO<br />Plain Old CLR Objects<br />
  31. 31. Benefits<br />Use existing classes<br />Loose Coupling<br />Classes have no dependency on EF<br />Persistence Ignorance<br />Unit Testing<br />Repositories & Unit Of Work Patterns<br />
  32. 32. ObjectContext Manages Entities<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />POCO<br />“Snapshot”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 />
  33. 33. POCO Support Basics<br />Turn off code generation from EDMX<br />“Convention” will auto-map classes/entities<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  34. 34. POCO and Other EF Features<br />Complex Types<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 />Two-Way Navigation<br />Must be coded into classes<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  35. 35. Lazy Loading POCOs<br />virtual (Overridable) navigation properties <br />IRelatedEnd wrapper at run-time<br />LazyLoadingEnabled=true required<br />Selectively allow lazy loaded properties<br />publicList<CreditCard>CreditCards {get; set; }<br />virtualpublicPersonPerson {get;set; }<br />publicList<Order> Orders { get;set; }<br />
  36. 36. ObjectContext Manages Entities<br />NYC Entity Framework Firestarter, Sept 27th 2010<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 />
  37. 37. Dynamic Runtime Proxy Classes<br /><ul><li> Track
  38. 38. Lazy Load
  39. 39. Relationships</li></ul>Person<br />class<br />F5<br />Mark all Entity (model) Properties virtual<br />Provides features similar to EntityObject<br />Change notification<br />Relationship management<br />Lazy Loading <br />
  40. 40. Rules for Proxy Notification<br />All entity properties shall be virtual<br />Though shalt use ICollection<T><br />Creation shall be done withObjectContext.CreateObject<T><br />Classes shall not be sealed<br />Navigation properties shall not be sealed<br />Classes shall not be abstract <br />Classes shall have a parameter-less constructor<br />
  41. 41. Code Gen POCOs from Model<br />T4<br />T<br />4<br />Text Template Transformation Toolkit<br />
  42. 42. EF POCO Summary<br />POCO support is new to EF4<br />Alternative to EntityObjects<br />Simple snapshot POCOs or use proxies<br />Use or even customize T4 template for code generation<br />Enable many coding/architectural possibilities<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  43. 43.  Part 5: Disconnected Strategies for ASP.NET & Services<br />EF NYC Firestarter<br />Julie Lerman<br />thedatafarm.com/blog<br />twitter @julielerman<br />
  44. 44. Agenda<br />EF Challenges in Server Aps<br />Review Options with EF<br />EF4 Tools to the Rescue<br />POCOs and Repositories<br />Leveraging Foreign Keys<br />Putting it all together<br />
  45. 45. Change Tracking Across Tiers<br />ObjectContext<br />ObjectStateEntry<br />SO1 EntityKey Original ValuesCurrent Values Other ∆ Info <br />ObjectStateEntry<br />SO2 EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info <br />detach<br />entities<br />ObjectStateEntry<br />LIA EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info <br />Original Property Values<br />EntityState Values<br />ObjectStateEntry<br />LIB EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info <br />
  46. 46. SaveChanges Fails Across Post Backs<br />Page Request<br />Post Back #1<br />Post Back #2<br />New Page Class<br />New Page Class<br />New Page Class<br />Create New ObjectContext<br />Create NewObjectContext<br />Create NewObjectContext<br />Get Entities<br />Process Request<br />Get Entities<br />Build HTML using Data & ASP.NET Markup<br />Build HTML using Data & ASP.NET Markup<br />Build HTML using Data & ASP.NET Markup<br />Destroy Page and Dependents<br />Destroy Page and Dependents<br />Destroy Page and Dependents<br />
  47. 47. The Server Side Spectrum with EF<br />Dynamic Data forEntities<br />ObjectDataSource<br />WCF &<br />Self-Tracking Ents<br />EntityDataSource<br />MVC<br />WCF<br />Data<br />Services<br />Dev<br />WCF<br />ASP.NET<br />n-Tier<br />Architect<br />
  48. 48. EF4 Changes to the Rescue<br />Foreign Keys<br />Big help for selected list values on postback<br />State Methods<br />ApplyCurrentValues, ApplyOriginalValues<br />ChangeState<br />ChangeRelationshipState (when no FKs)<br />POCO Classes & T4 Generation<br />Self-Tracking Entities for use with WCF<br />
  49. 49. What about?<br />Graphs<br />Lazy Loading<br />Returning IQueryables<br />Change<br />Tracking<br />
  50. 50. NYC Entity Framework Firestarter, Sept 27th 2010<br />Oh, goody….<br />time for code…<br />
  51. 51. Disconnected Strategies Summary<br />EF has inherent challenges across tiers<br />New tools in EF4 to enable disconnected<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  52. 52.  Part 6:Writing Testable/Maintainable Apps with EF<br />EF NYC Firestarter<br />Steve Bohlen<br />unhandled-exceptions.com/blog<br />twitter @sbohlen<br />Julie Lerman<br />thedatafarm.com/blog<br />twitter @julielerman<br />
  53. 53. Agenda<br />Setting the stage<br />Refactor a demo-worthy MVC app<br />Explore an existing solution with more complexity<br />
  54. 54. “-ilities”<br />Benefits:<br />Extensibility<br />Adaptability<br />Testability<br />Maintainability<br />Quality <br />Why should You Care?<br />Persistence Ignorance (PI)<br />Future-Proofing your investment<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />
  55. 55. IObjectSet Abstraction<br />IObjectSet<br />Collection-like behavior<br />Add, Attach, Delete, etc.<br />ObjectSet<br />Concrete class<br />Implements IObjectSet<br />Inherits ObjectQuery<br />public class ObjectSet:ObjectQuery,IObjectSet<br />
  56. 56. EF4 Goodness Makes This Work<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />POCOs<br />+<br />T4 Code Gen<br />Disconnected Methods<br />IObjectSet<br />
  57. 57. 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 />Repositories<br />UI<br />Tests<br />
  58. 58. Resources<br />LearnEntityFramework.com/resources<br />thedatafarm.com/blog/tags/ef4<br />blogs.msdn.com/adonet, efdesign, dsimmons, alexj, more...<br />msdn.microsoft.com/data<br />Gateway to whitepapers, forums, documentation<br />Lots of bloggers on EF now! Google! <br />-Bing!<br />NYC Entity Framework Firestarter, Sept 27th 2010<br />

×