Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0

Published 11 May 09 03:59 PM | dpblogs 

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

Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Sneak Preview: Persistence Ignorance and POCO in Entity Framework 4.0 | Microsoft Share Point said on May 11, 2009 7:48 PM:

PingBack from http://microsoft-sharepoint.simplynetdev.com/sneak-preview-persistence-ignorance-and-poco-in-entity-framework-40/

# system.data.objects dev guy said on May 11, 2009 9:36 PM:

I’m off at TechEd this week talking to customers about the EF—especially about ways to be successful

# Krzysztof Kozmic said on May 12, 2009 2:30 AM:

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;}}

   }

# DotNetShoutout said on May 12, 2009 5:05 AM:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# emanuele said on May 12, 2009 5:14 AM:

Wonderful!!!

Is it possible to get a planned date for EF 4?

Will be available CTP?

# ramonduraes said on May 12, 2009 8:28 AM:

Great. We were also improvements in the visual editor?

# progg.ru said on May 12, 2009 9:19 AM:

Thank you for submitting this cool story - Trackback from progg.ru

# Steve Moseley said on May 12, 2009 10:51 AM:

Entity Framework 4.0 Sneak Peak - POCO Goodness!

# Jeff said on May 12, 2009 12:30 PM:

@ 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.

Jeff

# Aleem Bawany said on May 12, 2009 2:52 PM:

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.

# Murad Kayani said on May 12, 2009 8:58 PM:

Excellent!!! Now we are talking.

# Parag Mehta said on May 13, 2009 3:51 AM:

That's great news. Although I feel you will still find cry babies, but EF finally starting to look good.

# DotNetKicks.com said on May 13, 2009 4:52 AM:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# csegura said on May 13, 2009 5:06 AM:

Completely excited!!!

Unit Testing sound now different ....

# Dmitry said on May 13, 2009 11:47 AM:

Is the designer going to overwrite POCOs in case of database changes?

# IUpdateable from Eric Nelson said on May 13, 2009 1:01 PM:

I have started talking to developers about what they can expect in Entity Framework V2 such as in my

# Jason N. Gaylord's Blog said on May 13, 2009 5:07 PM:

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord

# ASPInsiders said on May 13, 2009 6:07 PM:

If you are looking to follow this series, be sure to subscribe to my RSS feed at http://feeds.jasongaylord.com/JasonNGaylord

# Gil Fink on .Net said on May 14, 2009 3:32 AM:

What to be Expecting of Entity Framework in .NET 4 The ADO.NET team started to release a series of posts

# Sam Gentile's Blog (if (DeveloperTask == Communication && OS == Windows) said on May 14, 2009 1:47 PM:

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, queue

# ComponentGear.com Feed said on May 16, 2009 2:00 PM:

This week on Channel 9 at TechEd 2009, Brian is joined by Jeff Hadfield and Greg Duncan to discuss this

# ADO.NET team blog said on May 18, 2009 7:56 PM:

Writing unit tests is a core practice in the vast majority of modern software development approaches.

# Code Monkey Labs said on May 18, 2009 11:49 PM:

Pick of the week: The Web Browser Address Bar is the New Command Line General Open Source or Die – The *Real* Future of Graffiti : Lee Dumond addresses the lack of any forward development of Graffiti CMS and implores Telligent to hand it over to the capable

# ADO.NET team blog said on May 19, 2009 4:42 PM:

In the first version of the Entity Framework code generation was implemented internally using CodeDom

# dotnetrich said on May 21, 2009 3:15 PM:

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.

# ADO.NET team blog said on May 21, 2009 8:46 PM:

Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

# #.think.in said on May 22, 2009 7:46 AM:

#.think.in infoDose #29 (11th May - 15th May)

# Web开发技术 said on May 26, 2009 7:08 AM:

In the first version of the Entity Framework code generation was implemented internally using CodeDom

# #.think.in said on May 30, 2009 6:16 PM:

#.think.in infoDose #29 (11th May - 15th May)

# Web开发技术 said on May 30, 2009 10:12 PM:

Last week I mentioned in the sneak preview on POCO that support for POCO entities is one of the new capabilities

# Web开发技术 said on May 30, 2009 11:50 PM:

【译者按】 Entity Framework 1.0 发布也有一段时间了,但感觉用的人很少。其中一个很大的原因,也许就是不支持POCO。要知道,Entity Framework 1.0的做法是让你的实体从EF的基类继承而来

# VS2010学习 said on June 3, 2009 11:44 AM:

Introduction: From the moment I put my hands on Visual Studio.Net 2010 Beta 1 and I’m targeting EF4

# Mostly Technical Content said on June 4, 2009 6:06 AM:

In verschiedenen Einträgen in diesem Blog habe ich über die Verwendung von POCO’s mit dem Entityframework

# Pooran said on July 1, 2009 5:12 AM:

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.zip

# radyo dinle said on July 19, 2009 10:42 AM:

That's great news. Although I feel you will still find cry babies, but EF finally starting to look good.

# Laith Yousif said on August 14, 2009 9:11 AM:

Faisal,

Beta 1 already llok great but Any aproximate date for Beta 2 ?

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker