WCF - Exclude DataMember from DataContract based on clients requirement

Mar 28 2011 9:41 AM
Consider I have a DataContract with 6 properties. one client needs first 3 properties and second client needs last 3 properties of the Data Contract. How to write the Data Contract in a service So that the Service sends the message with only Required properties?

Each client have different requirement.

Some clients may access property 2 & 3, Some clients may access property 4 & 5 and some other clients access all data member and so on.

I have created the simple test application, have a look on that.


The GetStudentInfo() method in service contract takes id and array of required properties from DataContract StudentInfo.

[ServiceContract]
    public interface IStudentMgmtService
    {
        [OperationContract]
        StudentInfo GetStudentInfo(string id,List<string> value);
    }

 

[DataContract]
    public class StudentInfo
    {
        private string _StudentName;
        private string _gender;
        private string _Adderss;
        private int _age;
        private int _studentID;

        [DataMember(EmitDefaultValue=false)]
        public int ID
        {
            get           {}
            set            {}
        }

        [DataMember(EmitDefaultValue = false)]
        public string Name
        {
            get   {}        
            set      {}     
        }

        [DataMember(EmitDefaultValue = false)]
        public string Address
        {
            get    {}
            set{}
        }

        [DataMember(EmitDefaultValue = false)]
        public string Gender
        {
            get            { }
            set            {}
        }

        [DataMember(EmitDefaultValue = false)]
        public int Age
        {
            get            {}
            set            {}
        }
    }

 

 

The Service Implemention Code:

public class StudentMgmtService:IStudentMgmtService
    {
        public StudentInfo GetStudentInfo(string id, List<string> value)
        {
            StudentInfo res = new StudentInfo();

            if (value.Count != 0)
            {
                StudentInfo obj = new StudentInfo();
                obj = BLL.BLL_DBLibrary.GetStudentInformation(int.Parse(id)); //Retrieves all the properties of StudentInfo class(i.e DataContract).
                foreach (string item in value)
                {
                    switch (item.ToLower())               // to get only Required properties of StudentInfo class.
                    {
                        case "id": res.ID = obj.ID;
                            break;
                        case "name": res.Name = obj.Name;
                            break;
                        case "address": res.Address = obj.Address;
                            break;
                        case "gender": res.Gender = obj.Gender;
                            break;
                        case "age": res.Age = obj.Age;
                            break;
                        default: break;
                    }
                }

            }
            return res;     // the result consist of only required properties of StudentInfo class.
        }
    }


Is this good approach?

Or

Is there any approach using Data Contract Surrogate class?




Answers (1)