Recommendation on Entities

Jul 2, 2009 at 1:21 PM

Hello Michele,

Great series here, love your presentation style.

Do you decorate your models with the DataContract/DataMember attribs right in your libraries for business entities, or are you creating a new set of objects to move data across the wire?

I can see a case for both (decorate: common DLL with types across projects/and/new: only use what you need for the service, smaller message sizes), but I would think one should standardize across a project.  It would be quite the storm to have a large project with both scattered in it.

I guess there are other disadvantages to decorating the model...dependencies on WCF binaries, in some cases extremely large message sizes.

Your thoughts?


Jul 2, 2009 at 1:30 PM

I guess there is a third option: use POCOs from the business layer when you don't need descriminate serialization and specialize classes with contract attribs when necessary.  You seem to prefer the opt-in model, though.  Is that for service versioning support?  Sorry for the whack of questions...I've done the whole webservice->remoting->wcf progression and there's never been so many approaches (in other words, apparent ways to shoot one's self in the foot).



Jul 3, 2009 at 2:12 PM

Of course I don't discuss this in this particular series but do explain it quite a lot in Chapter 2 of my book (devoted to contracts), Learning WCF (get code here

In summary - POCO I don't recommend because it disallows any decorated elements in the hierarchy.

Event Entity Framework and LINQ to SQL types are now data contracts, this is an expectation in the service tier so no more issues with "dependencies on WCF". What can happen is if you don't own the library and need to serialize as part of hierarchy, and they aren't serializable or data contracts, you may need to map to a new object or use IXmlSerializable. The former for ease of implemetation, the latter for performance and control.

Agreed, you need a pattern for YOUR app. If you start from scratch, create an entities library. If you don't, design a pattern that makes sense for what you have.