Friday, 24 August 2012

POCO vs EntityObject (EF) model

What is a poco object?

POCO is a short for Plain old clr object and refers to an object that does not depends on the framework. It will not inherits and it should support it's own functionnalities.

For example, when you get to chose a generation template for your edmx, you will have Self-Tracked entity objects. These will be generated over a POCO model and will contain their own methods for propertyChanged and will also contains it's own EntityState property as it cannot rely on it's base object.

What is the EF model?

The alternative to POCO is to generate your entities and inherits them from EntityObject. This is the other text template offered in VS2010. The base EntityObject class will support a common PropertyChanged method and the EntityState method but you will still have a partial member for each of your primitive properties.

What should i choose?

It depends on your needs. If you use a WCF service or if you need your data to be exposed to other frameworks or languages , you should go for the POCO option. These objects does not rely on .net and could be easily consumed by any other language like php or java.

If you are using RIA services and have no plan in the future to expose your data, you can go with the EntityObject approach. In a silverlight context for example, the client proxy for your domain context class will be generated using the EF model no matter if you are using POCO or not.

This is the main difference over the two models, you should go with POCO if you need to expose data to other technologies or need a better portability for your objects. If you want to avoid some redundant code and add some common functionality to your objects , the EF model is an option.

Remember that there is a few differences in the text template itself so you can easily do the switch later in your development.

No comments:

Post a Comment