none
Two DataContracts for one Class RRS feed

  • Question

  • I am using Entity Framework to create POCO classes for me. In the t4 templates I have the classes being decorated with the [DataContract] attribute and the fields being decorated with the [DataMember] attribute. 

    I would like to have 2 different datacontracts. One that I can use internally which would expose all the fields and another external datacontract which would expose a limited set of fields per class. 

    For the service methods and ServiceContracts I was able to use different interfaces to handle this. But how would this work for the DataContracts?

    For instance:

    [DataContract]    
    public partial class CaseInfo
        {
          
            [DataMember]
            public string InternalOnlyProperty { get; set; }
          
            [DataMember]
            public string NormalProperty{ get; set; }
          
        }    

    Here I want the External Service to expose only the NormalProperty. But I want my internal service to expose both properties.

    Is there anyway to do this?


    Eric

    Tuesday, March 5, 2013 6:02 PM

Answers

  • versioning. 

    // Version 1 of a data contract for external clients
    [DataContract(Name = "Car")]
    public class CarV1
    {
        [DataMember]
        private string Model;
    }
    
    // Version 2 of the same data contract | Internal clients
    [DataContract(Name = "Car")]
    public class CarV2
    {
        [DataMember]
        private string Model;
    
        [DataMember]
        private int HorsePower;
    }

    Two end points for the service where internal end point exposes version 1 , external service exposes version 2. 

    Service security (windows, userName or digital certificates) should be in place to identify and expose resources to relevant client (Internal or external).


    Apriori algorithm [association rule]

    Tuesday, March 5, 2013 10:11 PM

All replies

  • if I understand correctly you want InternalOnlyProperty  not to be exposed to the service ...

    if so why not to define it without [DataMember] and with

    internal string InternalOnlyProperty { get; set; }

    in that case it will not need to be serialized

    Tuesday, March 5, 2013 8:17 PM
  • versioning. 

    // Version 1 of a data contract for external clients
    [DataContract(Name = "Car")]
    public class CarV1
    {
        [DataMember]
        private string Model;
    }
    
    // Version 2 of the same data contract | Internal clients
    [DataContract(Name = "Car")]
    public class CarV2
    {
        [DataMember]
        private string Model;
    
        [DataMember]
        private int HorsePower;
    }

    Two end points for the service where internal end point exposes version 1 , external service exposes version 2. 

    Service security (windows, userName or digital certificates) should be in place to identify and expose resources to relevant client (Internal or external).


    Apriori algorithm [association rule]

    Tuesday, March 5, 2013 10:11 PM