Entity Framework 4POCO & Persistence IgnoranceJulie Lermanthedatafarm.comjlerman@thedatafarm.comTwitter @julielermanJulie Lerman, August 10, 2010 Cleveland .NET SIg
Julie LermanwebsitetheDataFarm.comblog & twittertheDataFarm.com/blog@julielermanbook web siteLearnEntityFramework.comconsultant/mentorMicrosoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader2nd editionin AugustJulie Lerman, August 10, 2010 Cleveland .NET SIg
    Sponsored by INETA	  The Face of .NETWorld-wide association of .NET User Groups860+ groups world-wideOver 810,000 developersStarted and run by .NET User Group leadersSponsors and supports both .NET and other professional user groups all around the worldSponsorsMicrosoftNew sponsors welcome!North America (NorAm) INETA Speaker Bureau consists of  86 world-renowned speakerswww.ineta.orgJulie Lerman, May 11 2010, for HUNTUG
Julie Lerman, August 10, 2010 Cleveland .NET SIgPersistence Dependentvar context = new PublishingEntities();var book= (from b in context select books where bookid=3).First();b.Status = “Sent to Printer”;Context.SaveChanges();Persistence IgnorancevarbookMgr = new BookManager()var book=bookMgr.GetBook(3);book.Status=“Sent to Printer”;bookMgr.Update(book);
EF1 & Default EF4 ClassesJulie Lerman, August 10, 2010 Cleveland .NET SIg
POCO: Shifting the ResponsibilityJulie Lerman, August 10, 2010 Cleveland .NET SIg?ObjectContextContext.DetectChangesPOCOClasses“Snapshot”OREntityObject Proxy“Proxy Notification”
ObjectContext Manages EntitiesPOCO“Snapshot”4.0POCO+ Dynamic Runtime Proxy (4.0)EntityObject(3.5 & 4.0)ObjectContextPerson ObjectState infoState infoOrder ObjectState infoDetail ObjectState infoDetail ObjectState infoOrder ObjectState infoDetail ObjectState infoDetail ObjectJulie Lerman, August 10, 2010 Cleveland .NET SIg
Basic POCO SupportTurn off code generation from EDMX“Convention” will auto-map classes/entitiesJulie Lerman, August 10, 2010 Cleveland .NET SIg
POCO and Other EF FeaturesComplex Types: SupportedUse a class (no structs) to represent the typeExplicit LoadingObjectContext.LoadProperty(myObj,”PropertyName”)ObjectCOntext.LoadProperty(myObj, o=>o.property)Lazy Loading (new to EF4!)Declare property as virtualEF uses a proxy instance at runtime to loadJulie Lerman, August 10, 2010 Cleveland .NET SIg
Rules for Proxy NotificationAll properties  of the model entity must be virtualUse ICollection<T> for child collectionsObjectContext.CreateObject<T> for newClass cannot be sealedNavigation props cannot be sealedClass cannot be abstract          Constructor with no parametersJulie Lerman, August 10, 2010 Cleveland .NET SIg
POCO Classes from ModelJulie Lerman, August 10, 2010 Cleveland .NET SIgT4Text Template Transformation Toolkit
P.I.?Huh?EF1Julie Lerman, August 10, 2010 Cleveland .NET SIg
EF4Julie Lerman, August 10, 2010 Cleveland .NET SIg
EF4 POCO SupportAll entity properties must be representedHand CodeCode Gen with T4SnapshotObjectContext does all of the workLazy LoadingVirtual navigation propertiesDynamic Runtime ProxiesEvery single property is virtualProxy emulates EntityObject behaviorJulie Lerman, August 10, 2010 Cleveland .NET SIg
IObjectSet for SeparationSet with Collection-like behaviorAdd, Attach, Delete, etc.ObjectSetConcrete classImplements IObjectSetInherits ObjectQueryJulie Lerman, August 10, 2010 Cleveland .NET SIg
ClassesPI/RepositoriesSystem.Data.EntityFakeObjectSetFakeContext & Data AEDM &ObjectContextFakeContext & Data BUnit of WorkClassRepositoriesClassRepositoriesClassRepositoriesClassRepositoriesUITestsJulie Lerman, August 10, 2010 Cleveland .NET SIg
Entity Framework 4Supports POCOSeparation of concernsUnit TestingComing soon:Code FirstCurrently part of CTP4, not part of .NET 4Julie Lerman, August 10, 2010 Cleveland .NET SIg
Resourcesmsdn.com/dataLearnentityframework.comTestability and Entity Framework 4.0,Scott Allen, http://msdn.microsoft.com/en-us/ff714955.aspxAgile Entity Framework 4 Repository: blog serieshttp://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-1-model-and-poco-classes/Julie Lerman, August 10, 2010 Cleveland .NET SIg
Julie LermanwebsitetheDataFarm.comblog & twittertheDataFarm.com/blog@julielermanbook web siteLearnEntityFramework.comconsultant/mentorMicrosoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader2nd editionin AugustJulie Lerman, August 10, 2010 Cleveland .NET SIg

Persistence Ignorance in Entity Framework 4, Julie Lerman

  • 1.
    Entity Framework 4POCO& Persistence IgnoranceJulie Lermanthedatafarm.comjlerman@thedatafarm.comTwitter @julielermanJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 2.
    Julie LermanwebsitetheDataFarm.comblog &twittertheDataFarm.com/blog@julielermanbook web siteLearnEntityFramework.comconsultant/mentorMicrosoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader2nd editionin AugustJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 3.
    Sponsored by INETA The Face of .NETWorld-wide association of .NET User Groups860+ groups world-wideOver 810,000 developersStarted and run by .NET User Group leadersSponsors and supports both .NET and other professional user groups all around the worldSponsorsMicrosoftNew sponsors welcome!North America (NorAm) INETA Speaker Bureau consists of 86 world-renowned speakerswww.ineta.orgJulie Lerman, May 11 2010, for HUNTUG
  • 4.
    Julie Lerman, August10, 2010 Cleveland .NET SIgPersistence Dependentvar context = new PublishingEntities();var book= (from b in context select books where bookid=3).First();b.Status = “Sent to Printer”;Context.SaveChanges();Persistence IgnorancevarbookMgr = new BookManager()var book=bookMgr.GetBook(3);book.Status=“Sent to Printer”;bookMgr.Update(book);
  • 5.
    EF1 & DefaultEF4 ClassesJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 6.
    POCO: Shifting theResponsibilityJulie Lerman, August 10, 2010 Cleveland .NET SIg?ObjectContextContext.DetectChangesPOCOClasses“Snapshot”OREntityObject Proxy“Proxy Notification”
  • 7.
    ObjectContext Manages EntitiesPOCO“Snapshot”4.0POCO+Dynamic Runtime Proxy (4.0)EntityObject(3.5 & 4.0)ObjectContextPerson ObjectState infoState infoOrder ObjectState infoDetail ObjectState infoDetail ObjectState infoOrder ObjectState infoDetail ObjectState infoDetail ObjectJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 8.
    Basic POCO SupportTurnoff code generation from EDMX“Convention” will auto-map classes/entitiesJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 9.
    POCO and OtherEF FeaturesComplex Types: SupportedUse a class (no structs) to represent the typeExplicit LoadingObjectContext.LoadProperty(myObj,”PropertyName”)ObjectCOntext.LoadProperty(myObj, o=>o.property)Lazy Loading (new to EF4!)Declare property as virtualEF uses a proxy instance at runtime to loadJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 10.
    Rules for ProxyNotificationAll properties of the model entity must be virtualUse ICollection<T> for child collectionsObjectContext.CreateObject<T> for newClass cannot be sealedNavigation props cannot be sealedClass cannot be abstract Constructor with no parametersJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 11.
    POCO Classes fromModelJulie Lerman, August 10, 2010 Cleveland .NET SIgT4Text Template Transformation Toolkit
  • 12.
    P.I.?Huh?EF1Julie Lerman, August10, 2010 Cleveland .NET SIg
  • 13.
    EF4Julie Lerman, August10, 2010 Cleveland .NET SIg
  • 14.
    EF4 POCO SupportAllentity properties must be representedHand CodeCode Gen with T4SnapshotObjectContext does all of the workLazy LoadingVirtual navigation propertiesDynamic Runtime ProxiesEvery single property is virtualProxy emulates EntityObject behaviorJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 15.
    IObjectSet for SeparationSetwith Collection-like behaviorAdd, Attach, Delete, etc.ObjectSetConcrete classImplements IObjectSetInherits ObjectQueryJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 16.
    ClassesPI/RepositoriesSystem.Data.EntityFakeObjectSetFakeContext & DataAEDM &ObjectContextFakeContext & Data BUnit of WorkClassRepositoriesClassRepositoriesClassRepositoriesClassRepositoriesUITestsJulie Lerman, August 10, 2010 Cleveland .NET SIg
  • 17.
    Entity Framework 4SupportsPOCOSeparation of concernsUnit TestingComing soon:Code FirstCurrently part of CTP4, not part of .NET 4Julie Lerman, August 10, 2010 Cleveland .NET SIg
  • 18.
    Resourcesmsdn.com/dataLearnentityframework.comTestability and EntityFramework 4.0,Scott Allen, http://msdn.microsoft.com/en-us/ff714955.aspxAgile Entity Framework 4 Repository: blog serieshttp://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-1-model-and-poco-classes/Julie Lerman, August 10, 2010 Cleveland .NET SIg
  • 19.
    Julie LermanwebsitetheDataFarm.comblog &twittertheDataFarm.com/blog@julielermanbook web siteLearnEntityFramework.comconsultant/mentorMicrosoft MVP, INETA Speaker,ASPInsider, MCP, VTdotNET Leader2nd editionin AugustJulie Lerman, August 10, 2010 Cleveland .NET SIg