Is there any way to hide/flatten base types in WCF service data contracts?

心已入冬 提交于 2020-01-04 19:06:34

问题


Consider the following simple example:

[DataContract("{0}Base")]
public class Base<T> where T : Entity<T>
{
    // Common methods & properties.  No WCF exposed properties
}

[DataContract]
public class Employee : Base<Employee>
{
    // WCF exposed properties
}

The base class Base has no properties of interest to the WCF service consumers, but WCF forces me to also annotate the Base class with a [DataContract] attribute. This essentially shows up on the service client as Employee : EmployeeBase with EmployeeBase being an empty class with no properties.

I do not want to expose the Base<T> class to the service in this way, so what are my options?

  1. DTO for the Employee class - I'd rather not add this complexity
  2. "Flatten" the DataContract hierarchy so that the data contract for Employee does not expose that it inherits from Base<T>. Is this possible? How?
  3. Other solution?

Thanks.


回答1:


Each class in hiearchy has to be serializable / DataContract. If you don't want to expose hiearchy you have to use DTO or you can try to implement IDataContractSuroggate for your Employee class.



来源:https://stackoverflow.com/questions/3499788/is-there-any-way-to-hide-flatten-base-types-in-wcf-service-data-contracts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!