views:

173

answers:

1

What does one loose by creating POCO using T4 templates in entity framework 4.0? Why is the default behavior when using entity framework 4.0 not to create POCO?

+3  A: 

You lose a number of things. A "pure" POCO is of limited use in an ORM, because it will not do change tracking. In other words, when you mutate the object and then save changes to the context, you would like the changed properties saved to the database. With a "pure" POCO you can do this with snapshot based change tracking, which is fairly inefficient. You can also do it with runtime proxies, which force you to make your track properties public virtual, so you arguably don't have a "POCO" anymore. Also, using proxies means that you don't know the true runtime type of the instance.

You also lose some of the convenience properties like EntityState.

"Pure" POCOs cannot do lazy loading. Again, you can work around this with proxy types, but, again, if you're using proxies, you don't really have a "pure" POCO.

On top of all of this, there is less need to use POCO entities in the Entity Framework than in some other ORMs. This is because you can always project your entity types onto POCO instances using LINQ, without having to materialize the entity instances first. So "pure" POCOs are always available in an Entity Framework application, even if you don't happen to map your entities that way.

Craig Stuntz
Could you comment on unit testing with non POCO?
rkrauter
Unit testing with non-POCOs works fine. No problems at all for me.
Craig Stuntz
Thanks for the response!
rkrauter
I thought the POCO T4 Templates create proxies for you.And doesn't IPoco removed the need for EntityState?I think the OP's question is really interesting.. I'd love to see some more discussion on this topic.
itchi
IPOCO is fine, but it's different than POCO. IMHO proxies aren't really true POCOs, either, because my "POCOs" don't have everything delcared `public virtual`.
Craig Stuntz
Yeah I understand. But I think the OP was talking about http://visualstudiogallery.msdn.microsoft.com/en-us/23df0450-5677-4926-96cc-173d02752313 and not your "POCOs".
itchi
@itchi, how the POCOs are created doesn't change anything about my answer. After all, if you care how it was made, then it isn't really a "POCO," is it?
Craig Stuntz