New to WCF and DataContracts RRS feed

  • Question

  • I am building my first major WCF project.  I don't think I fully understand defining DataContracts so I am hoping someone can help me out here.  I know I need to make a DataContract for complex objects (IE Custom Classes).  However This projects design has a LOT of custom classes that are used all over the application.  A great deal of those will be sent and received via the Web Service.

    My question is this, can I define the data contract in the base class assembly and still use it everywhere else without issue?  Or can I define the class in a way in which I don't have to define a DataContract for it.  All the WCF samples are small and put the DataContract right there with the WCF methods.  This would require me to declare and thus maintain them twice if that was the case.  I certainly hope its not.  

    Monday, July 30, 2012 12:49 PM


  • There is no problem with adding [DataContract] to existing classes. [DataContract] is basically the same as [Serializable] but use the DataContractSerializer which is more tuned to WCF than anything else.

    • Marked as answer by Otomii Lu Tuesday, August 7, 2012 5:38 AM
    Monday, July 30, 2012 2:09 PM

All replies

  • I have a couple assemblies that have a majority of my base classes in there.  Can I just add the DataContract tags to them where they are and use them that way?  Will that impact my existing code that uses them?
    Monday, July 30, 2012 1:17 PM
  • Ideally you shouldn't be using you data contract classes for your BLL. You need a transaformation layer to change from the datacontract to the inner type you use.

    The reason for that is that as soon as your datacontract is exposed and used by thirdparties you will be force to keep it intact or at least compatible to avoid disturbing the 3P clients. This means that you will be force to use maybe no longer ideal classes in your BLL as the application evolves.

    If you are short on budget you can have identical BLL objects so your transformation layers is minimal, at least in this way you will have the right foundations for the project.

    Juan Casanova http://jawsofdotnet.blogspot.com - My .Net babling blog

    Monday, July 30, 2012 1:52 PM