1

Is it bad practice to use the generated objects from Entity Framework as business objects? 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 generated Entity Framework object EFPerson to initialize the POCO Person object?

Guilherme Fidelis
  • 1,022
  • 11
  • 20
Nate
  • 30,286
  • 23
  • 113
  • 184
  • If EF objects have issues being serialized, wouldn't it be best to wrap them in POCO? Because you may at some point in the future want to expose your Biz logic through a WCF. – Nate May 06 '09 at 16:08

2 Answers2

4

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
  • 59,031
  • 16
  • 99
  • 143
2

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: Serialize Entity Framework objects into JSON

Community
  • 1
  • 1
Program.X
  • 7,250
  • 12
  • 49
  • 83
  • 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 May 06 '09 at 16:12
  • 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 May 06 '09 at 17:13
  • 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 May 07 '09 at 03:34
  • 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 May 07 '09 at 07:22
  • Could you provide a link to such an example? – Nate May 07 '09 at 15:32
  • 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 May 07 '09 at 15:42