Attributes of DataContract and DataMember

Introduction 

 
We are explaining various concepts of WCF applications in this series. We have learned how to create a simple service, host it in a hosting environment, consume it, and much more about WCF applications. Here are the complete URL set for your reference:
 
In this chapter, we will see how to add attributes to a Data Contract and Data Member. In our previous chapter, we saw that a Data Contract is nothing but a simple C# class and the Data Member is the member of the class, it might be any data type.
 

Properties of Data Contract

 
According to MSDN, there are the following 4 properties of a Data Contract:
  • IsReference: Gets or sets a value that indicates to preserve object reference data
  • Name: Gets or sets the name of the data contract for the type
  • Namespace: Gets or sets the namespace for the data contract for the type
  • TypeId: When implemented in a derived class, gets a unique identifier for this Attribute.

Properties of Data Member

  • EmitDefaultValue: If you want to add default values in the serialization then set the EmitDefaultValue to true, else set EmitDefaultValue to false. By default it is true.
  • IsRequired: It controls the minOccurs attribute for the schema element. The default value is false.
  • Name: It creates the schema element name generated for the member.
  • Order: It maintains the order of each element in the schema. By default, it appears alphabetically.
But the most used property for Data Contract is the "name" property. We will see one example of it in a WCF application.
 
Create one WCF application and modify code for the Data Contract as in the following.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.    
  9. namespace DataContract  
  10. {  
  11.     [ServiceContract]  
  12.     public interface IService1  
  13.     {  
  14.         [OperationContract(Name="AddTowPerson")]  
  15.         string AddName(Person obj);  
  16.     }   
  17.     //Create a data contract and set the property into it.  
  18.     [DataContract(Name="PersonClass")]  
  19.     public class Person  
  20.     {  
  21.         [DataMember(Name="FirstName",IsRequired=true,Order=0)]  
  22.         public string name;  
  23.         [DataMember(Name="LastName",IsRequired=true,Order=1)]  
  24.         public string surname;  
  25.     }  
  26. }  
You can see that we have attached the name property to the Data Contract, DataMember attributes, and OperationContract attributes. This is to hide its actual name and the value of the name property will show in the client section. So, it's one kind of security implementation in the service code. Outside the application, the concrete class name and method name are unknown. This is one mechanism of information abstraction.
 
We will now implement the interface in the concrete class. Try to understand the following code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.    
  9. namespace DataContract  
  10. {  
  11.     public class Service1 : IService1  
  12.     {  
  13.         public string AddName(Person obj)  
  14.         {  
  15.             return obj.name + obj.surname;  
  16.         }  
  17.     }  
  18. }  
We have given a simple implementation of the "AddName()" method within the Service1 class. So, we have set up our service and now we will develop the client code to consume the service.
 
We will use a console application to develop our client code. Here is sample code.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Diagnostics;  
  6. using System.Collections;  
  7. using Client;  
  8. using System.ServiceModel;  
  9. using System.ServiceModel.Description;  
  10. using Client.ServiceReference1;   
  11.   
  12. namespace Client  
  13. {  
  14.     class Program  
  15.     {  
  16.         static void Main(string[] args)  
  17.         {  
  18.             PersonClass p = new PersonClass();  
  19.             p.FirstName = "Sourav";  
  20.             p.LastName = "Kayal";  
  21.             Client.ServiceReference1.Service1Client obj = new Service1Client();  
  22.             String fullName =  obj.AddTowPerson(p);  
  23.             Console.WriteLine(fullName);  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }  
Now, when we develop the client code we will observe that there is no "person" class available. It has changed into "PersonClass". The reason is we have specified the "PersonClass" property in the name parameter of the DataContract attribute of the "person" class.
 
 
Again, Visual Studio IntelliSense is showing that there are two data members of the "PersonClass" class; they are "FirstName" and "LastName", but actually, we have given the identifier name like "name" and "surname", so here the name attribute is effective and suppressing the actual identifier name.
 
The following is sample output of the application:
 
 

Conclusion

 
In this chapter, we saw the name property of the DataContract and DataMember attributes. Basically, this property suppresses the actual identifier name from the outside world. Hope you have enjoyed the chapter. 
 
And this is the end of the learn series. Hope you learned all the necessary concepts to start working with WCF.