views:

237

answers:

2

Is it bad practice to use the code-gen'ed objects from Entity Framework as business objects? Or is it better to write a secondary wrapper around Entity Framework objects that I would then pass between layers?

For example, write my own POCO Person that accepts one of my code-gen'ed Entity Framework object EFPerson to initialize the POCO Person object?

+1  A: 

I don't see why it would be bad practice. It might be awkward according to how you intend to use your EF objects.

I have partial classes to implement BIZ logic in the EF objects, using an Interface to provide a level of abstraction.

eg.

public partial class Client : IClient
{
   void DoSomething();
}

// then an EF generated object ...
public partial class Client
{
 // ...
}

My only problem I have had is serializing the objects. In my case, serializing into JSON using WCF. It isn't possible without creating an intermediate DTO either as a discrete class/object or as an anonymous type.

If you're interested in serialisation, have a look at another question of mine here: http://stackoverflow.com/questions/657939/serialize-entity-framework-objects-into-json

Program.X
If a DTO is necessary, would it not be wise to simply use the DTO for everything and then have a few static methods on the DTO to convert to and from EF objects?
Nate Bross
I chose not to. 1/ Too much effort and code maintenance (even with Code generation tools) and 2/ When I use DTOs I am using them for a distinct purpose, mostly for WCF work. Simplicity wins, for me!
Program.X
Would your DTOs with anon types work on other "clients" besides JavaScript? My primary use is via WCF where I have no knowledge of the client.
Nate Bross
I also have no knowledge of the client, except I assume it supports the protocol I supply, which in this case is JSON - for reasons of conciseness. WCF comes with a WCF serializer that can deserialize as well as serialize and there are enough libraries out there to facilitate JSON parsing even for non-web clients.
Program.X
Could you provide a link to such an example?
Nate Bross
In a rare example of ego-tism, I actually blogged about it. http://bloggingabout.net/blogs/program.x/archive/2009/03/18/wcf-json-serialization-woes.aspx
Program.X
+2  A: 

While some people tend to wrap the Entity Framework classes into their business objects, I would usually suggest not to do that.

I understand that this improves the separation of business logic and data access, but I think that this is usually not worth the overhead of duplicating all entity types.

What is the purpose of an OR mapper? It is to persist business objects without the need of a complex data access layer mapping the objects to the database by hand. If you wrap the Entity Framework classes, you will only use half of the gained convenience.

And finally the coupling between data access and business logic is not that tight with partial classes. Not long ago I changed a project involving about 30 entities from Entity Framework to LINQ to SQL in only a few hours without major problems.

Daniel Brückner
Those mayors...always causing problems...
Bryan Watts
:D Going to fix that ... ;)
Daniel Brückner