Wcf Service Basic Part Twelve Known Type.

If we want to use inheritance inside the service then known type will be great choice for us . For example we have one class named Employee . This class have several properties like name , id , gender etc . As we know from real experience employee could be permanent type or contract type . We can use Employee class as base type and inherited permanent and contract class from Employee class . We must use known type attributes at top of the base class , here is the example ..


 PermanentEmployee.cs
namespace CompanyService
{
   
public class  PermanentEmployee:Employee
    {
       
public int Salary { get; set; }
    }
}


ContractEmployee.cs
namespace EmployeeService
{
   
public class ContractEmployee : Employee
    {
       
public double HourlyPay { get; set; }
       
public string HoursWorked { get; set; }
    }
}




Employee.cs


namespace CompanyService
{
    [
KnownType(typeof(ParmanentEmployee))]
    [
KnownType(typeof(ContractEmployee))]

    [
DataContract]
   
public class Employee
    {
       
private int _id;
        
private string _name;
        
private string _gender;
        


        [
DataMember(Order = 1)]
       
public int Id
        {
           
get { return _id; }
           
set { _id = value; }
        }

        [
DataMember(Order = 2)]
       
public string Name
        {
           
get { return _name; }
           
set { _name = value; }
        }

        [
DataMember(Order = 3)]
       
public string Gender
        {
           
get { return _gender; }
           
set { _gender = value; }
        }

        


        [
DataMember(Order = 5)]
       
public Type Type { get; set; }
    }

   
public enum Type
    {
        ParmanentEmployee = 1,
        ContractEmployee = 2
    }
}


There are 4 different ways to associate KnownTypes:

1.Use KnownType attribute on the base type. like this 

[KnownType(typeof(ParmanentEmployee))]
    [
KnownType(typeof(ContractEmployee))]

    [
DataContract]
    
public class Employee{
............

}

2. Apply ServiceKnownType attribute on the service contract.



[ServiceKnownType(typeof(ParmanentEmployee))]
[
ServiceKnownType(typeof(ContractEmployee))]
[
ServiceContract]
public interface ICompanyService
{
     


    [
OperationContract]
   
string SaveEmployee(Employee Employee);
}

3.  Apply ServiceKnownType attribute on specific operation contracts. With this option, only the operation contracts that are decorated with ServiceKnownType attribute respect known types.

[ServiceContract]
public interface ICompanyService

{
 [ServiceKnownType(typeof(ParmanentEmployee))]
[
ServiceKnownType(typeof(ContractEmployee))]
   
  

    [
OperationContract]
   
string SaveEmployee(Employee Employee);
}


4. You can also specify known types in the configuration file like this ...


<add type="CompanyService.Employee, CompanyService, Version=1.0.1.0, 
            Culture=Neutral, PublicKeyToken=null">
        
<knownType type="CompanyService.ParmanentEmployee, CompanyService, 
                    Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
        
<knownType type="CompanyService.ContractEmployee, CompanyService,
                    Version=1.0.0.0, Culture=Neutral, PublicKeyToken=null"/>
     
</add>


No comments:

Post a Comment