locked
WCF and Entity Framework Design question RRS feed

  • Question

  • Should we be using Entity Framework entities as data contracts across the WCF Layer? If yes when why have the POCO support and code only been added to the Entity Framework? I was assuming it was with the purpose to remove the dependancy that the client will have on the database if the client depends on the Entities.

    If No, then does that mean we need to create Business Objects for each entity that exists in the entity framework model?

    Please advise.

    Thanks.


    Nilotpal
    Wednesday, September 8, 2010 10:36 AM

Answers

  • Hi Nilotpal,

    It depends on the version of EF. If you are using .NET 3.5 SP1, then its definitely not recomended to use EF over WCF. Primary reason being SOA separation of concerns. If you look the response of a wcf service xposing EF entitities in 3.5sp1, then you'll see a lot of unwanted stuff like entity key etc which to a certain extent does not concern the client consuming it and also exposes internal implementation details.

    In .NET 4.0 with POCO support, its possible to simplify the final representation of the entities on the wire by removing the unwanted EF specific stuff. Also you can chose to decorate which ever entities you want to expose as data/contract/members rathern than exposing the  entire entity.

    But invariably of the framework, its a bad idea to expose your data model as contracts as any changes in your data model might result in changing the service. But if your only objective of using a WCF service is for simple data exchange and u dont mind on losing out interoperabiliyt then its fine to use it.


    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Proposed as answer by Patriek van Dorp Wednesday, September 8, 2010 2:31 PM
    • Marked as answer by Nilotpal Das Sunday, September 12, 2010 12:37 PM
    Wednesday, September 8, 2010 1:02 PM

All replies

  • Hi Nilotpal,

    It depends on the version of EF. If you are using .NET 3.5 SP1, then its definitely not recomended to use EF over WCF. Primary reason being SOA separation of concerns. If you look the response of a wcf service xposing EF entitities in 3.5sp1, then you'll see a lot of unwanted stuff like entity key etc which to a certain extent does not concern the client consuming it and also exposes internal implementation details.

    In .NET 4.0 with POCO support, its possible to simplify the final representation of the entities on the wire by removing the unwanted EF specific stuff. Also you can chose to decorate which ever entities you want to expose as data/contract/members rathern than exposing the  entire entity.

    But invariably of the framework, its a bad idea to expose your data model as contracts as any changes in your data model might result in changing the service. But if your only objective of using a WCF service is for simple data exchange and u dont mind on losing out interoperabiliyt then its fine to use it.


    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Proposed as answer by Patriek van Dorp Wednesday, September 8, 2010 2:31 PM
    • Marked as answer by Nilotpal Das Sunday, September 12, 2010 12:37 PM
    Wednesday, September 8, 2010 1:02 PM
  • That worked...! I am now using self tracking entities in 4.0. as data transfer objects. But now I am stuck with another one. Not being able to AddObject(). The error I am getting is: The property 'Id' is part of the object's key and cannot be changed. Changes to key properties can only be made when the object is not being tracked or is in the Added state.

    Have you encountered that before?

    p.s. I have posted it as a separate question. You will get full details there.


    Nilotpal
    • Edited by Nilotpal Das Sunday, September 12, 2010 12:39 PM typo
    Sunday, September 12, 2010 12:39 PM