Entity Framework in a SOA environment
I received a mission to explore entity framework as a DAL for a new project, this project is intended to be N-Tier SOA application.
Here are some of my findings:
- The partial classes which are created by entity framework already contain DataContract and DataMember attributes and are “ready to use” in WCF.
- Entity framework has a change tracking mechanism, the main problem with this mechanism is that once the object was serialized (before transferred in a service) it gets the state “detached” and from there on it doesn’t track changes until reattached to the graph (read more here).
The second issue is a major disadvantage in comparison to typed datasets, which has the ability to track changes in any tier (using DiffGrams).
There are few community projects which try to overcome this issue by developing their own type of DiffGram for entities (some of them can be found here and here), But after testing these project (and using SQL server profiler) we found out that there are 3 major issues that we couldn’t find a solution for all of them together in any of the community projects, the issues are:
- Performances (we saw that one solution made 17 select statements to the DB before updating a single entity).
- Updating entities which are connected by foreign keys to another entity (not supported by some solutions and problematic in others).
- We don’t want to create a 3 service functions for each Entity in example: UpdateCustomer(CustomerEntity c) , AddCustomer(CustomerEntity c) and DeleteCustomer(CustomerEntity c). We just want one single function:
CommitChanges(EntityObject e). We found that implementing such a function could be a very complex task.
But not everything is so bad, it appears that the ADO.NET team at Microsoft is aware of this issue, and in EF4 (VS2010) there will be a solution for these problems called “self tracking entities” (as you can read here and here).
To sum things up, my personal opinion is that until the “self tracking entities” mechanism will be implemented, this technology is still not mature for large scale SOA projects and requires many tweaks and infrastructure work.