0

I have WCF service which expose STE from EF.

[OperationContract, FaultContract(typeof(WarningFault)),  FaultContract(typeof(ErrorFault))]
    MyEntity GetMyEntityByID(int id);

    [OperationContract, FaultContract(typeof(WarningFault)), FaultContract(typeof(ErrorFault))]
    MyEntity SaveMyEntity(MyEntity myEntity);

Scenario look like this:

  1. client get entity from WCF(GetMyEntityByID)
  2. client make some changes to this entity
  3. client call SaveMyEntity
  4. in the WCF service is this particular entity attached to context and saved to db. (and there is also some custom validation work before saving itself.)

Because of STE, this is working quite fine..

But I have noticed, that this is not exactly good pattern. (in EF 5.0 are STE mark as Not Recommended)

What approach should I used instead of this? If I understand correctly WCF Data Services are not for this kind of work because they only expose entities. And saving, validation, etc. is managed on client side.

Chatumbabub
  • 1,357
  • 1
  • 15
  • 26

1 Answers1

2

I recommend that you work with detached entities. reload your object from the database by primary key and the copy only the fields that your client is able to modify. Dont expose the database context outside your service

tggm
  • 921
  • 11
  • 36
  • Is there any reason for it? Because this require quite lot of work. Entities are not usually simple, there is lot of navigation properties etc.. In my point of view, this "reload object" is expensive operation and with STE can be avoid. Is there something what I don't see? – Chatumbabub Sep 05 '12 at 22:14
  • 1
    @Chatumbabub: How about: Separation of concerns, proper layer abstractions, single responsibility principle, proper domain model – jgauffin Sep 06 '12 at 08:43
  • @Chatumbabub I can think of some reasons. First, you're required to share the assemblies between client and server (as far as I understand STEs over WCF. Check this answer http://stackoverflow.com/a/5463866/624680). Secondly, you'll be limiting your the service to WCF only and lose flexibility if your client's technology changes. And thirdly, you are in effect giving full control of your entity to the client. It will have full power to change any attribute of your entities and/or the graph of related entities below. Of course that, if your scenario is a simple one, then go ahead and try. – tggm Sep 06 '12 at 18:28