views:

7681

answers:

6

I'm using Entity Framework O/R mapper from Microsoft and using entity classes (generated classes that are mapped to DB objects) as a business objects. Is this OK? Please state your cons or pros. What to do in a case of WCF communication between business layer and presentation, how to send those objects as data members?

+7  A: 

The Entity framework was designed for the entity objects to be used as business objects, but you should keep in mind that the business objects will be tied to O/R technology as well as the EDM model. In EF 1.0, there wasn't any support yet for persistence-ignorance scenarios, but support was added in 4.0. You can implement interfaces, if you don't want to use any of their base classes.

As of .NET 3.5 SP1, they should also be usable as paramater and return types in WCF service methods without any additional code.

Mark Cidade
Update: There is support for persistence-ignorance in Entity Framework 4.0 / .NET 4.0
Mark Lindell
+9  A: 

I am using EF in this fashion and one nice feature is that generated entities are partial classes, allowing them to be extended in a way that is fairly protected from regeneration issues.

Also take a look at this link on MSDN which describes some common usage scenarios with EF in regards to Business Logic.

YeahStu
+1: I do this too, and not yet have found a case that got me stuck. I show my entity-class properties in diagrams, lists and on labels in a winform app.
Marcel
+2  A: 

In my experience, we've used EF objects within the business layer of our application, but when we make the transition into the presentation layer through our WCF service layer, we will create view objects from the EF objects.

In our case, only the view is passed to the presentation layer. We do this to control how the data is presented and apply defensive validation for data coming in from the presentation layer.

In the case of ueing EF objects in the WCF transaction, you'll lose the object context that the EF object was associated. There are some efforts in CodePlex that try to help with this but I havn't kept up with their efforts.

Rick
+2  A: 

Can't you just re-attach the objects if they lose their original object context? You'd need to handle concurrency-issues yourself though.

I wouldn't recommend using EF objects as DataContract objects for WCF, as you'd tie very strongly your implementation of entity objects to web service clients, change will be hard to do in the future, harder the more clients you plan on having.

AndreasKnudsen
+2  A: 

Two limitations to be aware of that I have run into are:

  1. Inherited Objects cannot have Navigation Properties - i.e. if you have a "person" class and then a "customer" and "supplier" those customer and suppliers cannont have Navigation properties.

  2. Methods and Calculated Fields (anything in the partial classes) are not transmitted over ADO.Net Data Services - if you are also using ADO.Net Data Services anything you expand the Entity Framework objects on in partial classes will not be transmitted over ADO.Net Data Services.

These are generally not show-stopper items (for navigation properties we just don't use inheritance on entity framework, for now), but might be something of interest to yourself. I'm holding out hope that a future release will enable both of these items.

ChrisHDog
+1  A: 

The BookLibrary sample application of the WPF Application Framework (WAF) shows how the Model-View-ViewModel (MVVM) pattern can be used in combination with the Entity Framework.

jbe