Search code examples
wcfdatacontracts

WCF Business Objects or DataContracts


I have three projects:

  • WCF Service project (Interface and Implementation)
  • aspx web project (client) that consumes the WCF Service
  • class library project that holds my business objects (shared by both WCF project and client)

I have a method in the WCF Service implementation class file that retrieves a generic list of data from SQL (referencing the project that holds the business objects), serialize the data using System.Web.Script.Serialization.JavaScriptSerializer and returns the result as a string.

The web client takes this string and deserializes it back to the appropriate business object (referencing the project that holds the business objects)

This is an intranet app and I want to make sure I am doing this correctly.

My questions are:

  • Should I be using DataContracts instead of business objects? Not sure when to use DataContracts and when to use the business objects.
  • If I am using DataContracts, should I not use System.Web.Script.Serialization.JavaScriptSerializer?

Any clarification would be appreciated.


Solution

  • Of course there is no one answer. I think the question is whether you want to use business objects in the first place, otherwise my fourth point pretty much covers it.

    Do use the business objects if they look like the data contracts would, i.e. they are a bunch of public properties and do not contain collections of children/ grandchildren etc.

    Don't use the business objects if they contain a bunch of data you don't need. For example populating a grid with hundreds of entities begs for a data contract specific to that grid.

    Do use the business objects if they contain validation logic etc that you would otherwise have to duplicate in your web service.

    Do use the business objects if you are just going to use the data contracts to fully inflate business objects anyway.

    Don't use the business objects if you ever want to consume that service interface from non .net code.

    Don't use the business objects if you have to massively configure their serialization.

    Don't use the business objects if they need to "know" where they are (web server or app server)

    Not your case but: Do use the business objects if you are building a rich client for data entry.

    Thats all for now, I'll see if anything more occurs to me. :)