5

I have been using EF since it first came out. Used to hand build POCOs in 3.5 and was glad to see Self Tracked Entities(STE) in EF4.0.

I have use STEs in a couple of very large projects(500+ entities, some with multiple models). In these projects I use a generic Repository and a generic Unit of Work to persist the entities i.e. 2 small generic classes no mapping. By electing a core entity as the "aggregrate root", other entities are added and updated on the client side and the core entity graph containing these changes is sent to the WCF service and used in the Logic Layer which creates the Repository<[core entity]> and uses the UnitOfWork<[core entity]>.Save(Repository<[core entity]>) to persist the STEs and their children to the database.

Now Microsoft is recommending that we not use STEs. See this article

So my question is, What is(are) the patterns that are now recommended by Microsoft for applications that are persisting client changes to WCF Services that use EF?

I created a EF5 Model and examined the generated code. The there are no attributes for a WCF Service i.e. DataContract, DataMember etc

EF4 had a "ADO.NET DbContext Generator with WCF Support" template, but there isn't a EF5 equivalent.

One site suggested I should use a partial class file and decorate the same properties in that file with these attributes. But unless .net 4.5 has introduced partial properties, I cannot see how that can be done.

Another blog suggested using DTO and Automapper, which means more mapping which is error prone; especially when entity fields change type.

So now that DBContext generated code classes are not Service enabled, does this mean that we need to write another set of classes (POCOs) that:

  • needs to be mapped FROM the DBContext generated code classes after querying the database.
  • holds the data state for the WCF Service client(s)
  • is updatable by that client(s)
  • is mapped by the client(s)
  • has the ability to hold changed state so this can be sent back to the WCF Service
  • needs to be mapped TO the DBContext generated code classes for persistence

It seems we just took a great leap backwards to EF3.

If you code both client and service that runs on your hardware, you don't need to be concerned about data structures at the client as they belong to you.

If you also need to expose some of your service methods to non.NET clients you should do the 5 points above for those services anyway and use DTOs and Automapper in those occasions.These should be in a different WCF Service but implemented against the same Logic Layer, after mapping.

But how many of these type of non.NET client services are be created in the day to day building of web applications in most software teams?

This latest recommendation is confusing as it has not been explained as to WHY STEs are ALWAYS ill-conceived and what now, are the recommended patterns to be used for persisting client changes to WCF Services that use EF.

Can anybody inform me where I can find a good resource that solves this architectural design issue?

P.S.

Please don't recommend WCF Data Services or WCF RIA as we need a lot of control over how your data is retrieved and saved by clients.

Please don't recommend Code First as we use Database First as we want to have and need to control the structure of that database and not have to generated for us.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
GregJF
  • 456
  • 4
  • 14

1 Answers1

0

Ok so i thought the same thing when I first read this article, it seems a bit weird to deprecate a whole branch of EF like this and the intention wasn't terribly well communicated (IMO). I think a couple of things are important here:

  • STEs as referred to in this article refer to object context based self tracking entities (which act a little like autonomous contexts)
  • ObjectContext is generally being moved away from in favor of the cleaner DbContext structure (this is for both DB first and Code First)
  • STEs != DB first generation, you can still use an EDMX model in EF and this isn't likely to change.
  • When i originally saw this article I mistook STEs for POCO Proxy entities which are still available and AFAIK there are no plans to deprecate. (these achieve a similar technical solution to the problem of change detection but with a nicer interface. Check out this article for the differences EF4: Difference between POCO , Self Tracking Entities , POCO Proxies

So what does this all mean

Basically STEs in terms of the old implementation of a change tracker are being deprecated in favor of the newer forms of change tracking (Snapshot or POCO Proxies). This means that if snapshot tracking doesn't suit you you should look into POCO Proxies which are similar to the old STEs.

You can still use all previous techniques for context generation (DB First, Model First, Code First, and DB-> Code)

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • I used to work with the OP. Snapshot and Proxy POCOS actually predate STEs. They do not track when detached from the context and are not usable at all when you want to detached from a context and resync with another context instance later which is typical in WCF per-call designs. STEs are not perfect due to exposing and running tracking code in the client but there is no replacement for this DataSet-like usage scenario. MS needs to implement a server side graph sync for POCOs that can work across context instances. Until that time, DbContext remains crippled. – MrLane Dec 23 '14 at 04:15