Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0
The information in this post is out of date.
Visit msdn.com/data/ef for the latest information on current and past releases of EF.
In Entity Framework 3.5 (.NET 3.5 SP1), there are more than a few restrictions that were imposed on entity classes. Entity classes in EF needed to either be sub classes of EntityObject, or had to implement a set of interfaces we collectively refer to as IPOCO – i.e. IEntityWithKey, IEntityWithChangeTracker and IEntityWithRelationships. These restrictions made it difficult if not downright impossible to build EF friendly domain classes that were truly independent of persistence concerns. It also meant that the testability of the domain classes was severely compromised.
All of this changes dramatically with the next release of Entity Framework: 4.0 (.NET Framework 4.0). Entity Framework 4.0 introduces support for Plain Old CLR Objects, or POCO types that do not need to comply with any of the following restrictions:
- Inheriting from a base class that is required for persistence concerns
- Implementing an interface that is required for persistence concerns
- The need for metadata or mapping attributes on type members
For instance, in Entity Framework 4.0, you can have entities that are coded as shown:
public class
Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public
List
<
Order
> Orders { get; set; }
}
public class
Order
{
public int OrderID { get; set; }
public
Customer
Customer { get; set; }
public
DateTime
OrderDate { get; set; }
}
You can then use the Entity Framework to query and materialize instances of these types out of the database, get all the other services offered by Entity Framework for change tracking, updating, etc. No more IPOCO, no more EntityObject - just pure POCO.
Keep in mind that this is an extremely simplistic example, and intentionally so. There is much more here than meets the eye – I am sure that it brings up at least a few questions about what is possible and what isn’t possible with POCO entities – for instance:
- Do I need to have public getters/setters for scalar and navigation properties?
- Will I get Lazy Loading? How does explicit loading work?
- How does relationship fix-up work with POCO?
- What does this mean for code generation done within Visual Studio?
- How does this fit in with the repository pattern?
What about Complex Types? Serialization? Change Tracking? Add/Attach…. The list goes on….
These and many other questions and concerns will be answered in our in-depth series on POCO that we are working on publishing in the coming weeks.
And by the way – did I just mention Lazy Loading? Watch for a sneak preview on that tomorrow!
- Faisal Mohamood
Program Manager, Entity Framework
Comments
Anonymous
May 11, 2009
PingBack from http://microsoft-sharepoint.simplynetdev.com/sneak-preview-persistence-ignorance-and-poco-in-entity-framework-40/Anonymous
May 11, 2009
I’m off at TechEd this week talking to customers about the EF—especially about ways to be successfulAnonymous
May 11, 2009
Can I have entity looking like this? public class Customer { private readonly List<Order> _orders = new List<Order>(); public string CustomerID { get; set; } public string ContactName { get; set; } public string City { get; set; } public List<Order> Orders { get{return _orders}; } public bool HasOrders {get {return Orders.Count>0;}} }Anonymous
May 11, 2009
Thank you for submitting this cool story - Trackback from DotNetShoutoutAnonymous
May 11, 2009
Wonderful!!! Is it possible to get a planned date for EF 4? Will be available CTP?Anonymous
May 12, 2009
Great. We were also improvements in the visual editor?Anonymous
May 12, 2009
Thank you for submitting this cool story - Trackback from progg.ruAnonymous
May 12, 2009
Entity Framework 4.0 Sneak Peak - POCO Goodness!Anonymous
May 12, 2009
@ Krzysztof Yes your entity can look like that. We will support collection properties with getters only, or with both a getter and a setter. Anything that is an ICollection<T> will do. JeffAnonymous
May 12, 2009
Eagerly awaiting this release. I have just gotten comfortable with EFPocoAdapter so the migration isn't too tedious. And definitely looking forward to MSDN articles and better support (though Jarek has been quite responsive over email, I really was starting to miss full support) My biggest pain point has to be serialization. I ran into a bunch of issues with circular references and JSON serialization before finally getting it working. Since the POCO classes are auto-generated I would like to see support to dynamically ignore properties during serialization (attributes only work at compile time and get overridden each time those classes are regenerated). This way Would it be a bad idea to make those classes partial? I know that extending them would mean they're not purely POCO anymore but I do find that if they are auto-generated it's hard to modify them without getting your changes overridden. Extension methods work but only halfway through. Extension properties if that comes in .NET 4 might offer more flexibility in extending POCO objects.Anonymous
May 12, 2009
Excellent!!! Now we are talking.Anonymous
May 12, 2009
That's great news. Although I feel you will still find cry babies, but EF finally starting to look good.Anonymous
May 12, 2009
You've been kicked (a good thing) - Trackback from DotNetKicks.comAnonymous
May 12, 2009
Completely excited!!! Unit Testing sound now different ....Anonymous
May 13, 2009
Is the designer going to overwrite POCOs in case of database changes?Anonymous
May 13, 2009
I have started talking to developers about what they can expect in Entity Framework V2 such as in myAnonymous
May 13, 2009
If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylordAnonymous
May 13, 2009
If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord.com/JasonNGaylordAnonymous
May 13, 2009
What to be Expecting of Entity Framework in .NET 4 The ADO.NET team started to release a series of postsAnonymous
May 14, 2009
Windows Azure/Azure Service Framework (including .NET Services)/BizTalk David Pallmann has released 2.1 of Azure Storage Explorer with the new feature of modifying what is in storage including create or delete blob containers, blob items, queues, queueAnonymous
May 16, 2009
This week on Channel 9 at TechEd 2009, Brian is joined by Jeff Hadfield and Greg Duncan to discuss thisAnonymous
May 18, 2009
Writing unit tests is a core practice in the vast majority of modern software development approaches.Anonymous
May 18, 2009
The comment has been removedAnonymous
May 19, 2009
In the first version of the Entity Framework code generation was implemented internally using CodeDomAnonymous
May 21, 2009
One important feature missing in this example is the ability to customize the generation of your entity classes. Using T4 you will be able to have POCO or whatever flavor of entity you desire by simply creating additional templates.Anonymous
May 21, 2009
Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilitiesAnonymous
May 21, 2009
#.think.in infoDose #29 (11th May - 15th May)Anonymous
May 25, 2009
In the first version of the Entity Framework code generation was implemented internally using CodeDomAnonymous
May 30, 2009
#.think.in infoDose #29 (11th May - 15th May)Anonymous
May 30, 2009
Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilitiesAnonymous
May 30, 2009
【译者按】 Entity Framework 1.0 发布也有一段时间了,但感觉用的人很少。其中一个很大的原因,也许就是不支持POCO。要知道,Entity Framework 1.0的做法是让你的实体从EF的基类继承而来Anonymous
June 03, 2009
Introduction: From the moment I put my hands on Visual Studio.Net 2010 Beta 1 and I’m targeting EF4Anonymous
June 03, 2009
The comment has been removedAnonymous
June 30, 2009
Hi, I am trying to use ADO.Net Entity Data Framework in Prism. Almost all the examples including http://msdn.microsoft.com/en-us/library/cc838191(VS.95).aspx talk about Silverlight page accessing. However in our applications, we use modules which are Class Libraries or Silverlight Class Libraries, which bind the data to the UI. Are there any code samples/snippets to use the same? Have uploaded the code sample at http://cid-8f83691dd01b3d44.skydrive.live.com/self.aspx/Prism/prismapp.v3.zipAnonymous
July 19, 2009
That's great news. Although I feel you will still find cry babies, but EF finally starting to look good.Anonymous
August 14, 2009
Faisal, Beta 1 already llok great but Any aproximate date for Beta 2 ?Anonymous
June 02, 2013
Great :)