Adding methods to DataContract objects for WCF

后端 未结 4 1807
名媛妹妹
名媛妹妹 2020-12-06 08:20

Are DataContracts in WCF nothing more than DTOs? I was reading up about WCF and just had a couple of thoughts. It would be nice if some of the DataContract objects could hav

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 09:10

    DTOs that are decorated as DataContract classes are real objects. They can have methods in them, but the methods are not part of the serialization process.

    The main time this will cause you issues is when:

    • you are relying on the generated proxy version of the DataContract objects (like when you have a Silverlight client calling a WCF service, or you are calling a third party service that you have no access to the code or its libraries). The generated proxy versions will not have the methods in them, just the DataMember properties. The way to get round that is to use objects from a shared library (as already mentioned by @Insomniac).

    • your properties in the DataContract objects are more than just a simple get/set operation, i.e. you may have included some logic to do other operations when a property value is set. In this case even the proxy generated version will not have that logic included. The ways to get round this is to either have the shared library, or have a partial class on the client side that extends the proxy generated class.

提交回复
热议问题