Data Contract of WCFWCF之数据协议

what is contract什么是数据协议
     In the abstract, all the WCF provides for is the ability to host and expose native CLR types as servies, and the ability to consume services as native CLR interfaces and classes.WCF service operations accept and return CLR types such as integers and string, and the WCFclients pass in and process returned CLR types. as a result, any client,regardless of its own technology, can ineract with the service. this obviously mean that WCF cannot  allow you to expose the CLR data types across the service boundary. what u need is a way of converting CLR types to and from a standard neutral represention. That reprepresentation is a simpel XML-based schema or an inset. In adddtion, the service needs a formal way for declarging how such conversion is to take place,this formal way is called a data contract
    从理论上讲,WCF能够将本地本地CLR类型作为服务进行启动和发布,也能够将服务作为本地CLR接口和类来进行调用。所有的WCF 服务操作所能接受和处理返回的都是CLR 定义类型,如整形,字符创等。因此,无论客户端采用的是何种技术,都能够和服务进行交互。但是,这样有一个限制就是,所操作的数据不能超越CLR 类型的服务边界(service boundary).这就需要我们能够有一种方法,能够有一种独立于平台的中性表达数据的方式,他能够转化为CLR types,或者相互转化。所以这种表达方式是基于XML模式或者信息集的。另外,服务需要一种正式的表示方法来表示如何进行这种conversion.这就是数据协议
DataContract Attribute数据协议属性
public sealed class DataContractAttribute:Attribute
{
    public string name{get;set;}
    public string NameSpace{get;set;}
}//DataContract主要包含2个性质name和Namesapce

public sealed class DataMemberAttribute:Attribute
{
    public bool IsRequired{get;set;}
    public string name{get;set;}
    public int Order{get;set;}
}//DataMember主要包含name和Order

we can apply the DataContract and DataMember attribute like this:
//我们采用如下语法定义一个数据协议
[DataContract]
struct mydata
{
[DataMember]    
public string FirstName;

[DataMember]
public string LastName;
}
        //comments:data contracts are case-sensitive.注意:数据协议对大小写敏感的。

Importing  a Data Contract on client 从客户端倒入一个数据协议
    when a data contract is used ina contract operation, it is published in the servce metadata. when the client imports the definiton of the data contract, the client will end up with an equivalent definition, but not an identical one. the imported definition will maintain the original type designation of a class or a structure. In addtion, unlike a service contract, by default the imported definition will maintain the original type namespace.
    当数据协议在协议操作中被使用到的时候,该数据协议利用服务元数据发布。 同时,当客户端导入我们的数据协议时,在客户端也会生成对等的数据定义,但是并不一定完全一样的定义格式。
如下例所示:
e.g1 
    service-side definition
    namespace MyNameSpace
    {
        [DataContract(Namespace="MyOtherNamespace")]
        struct Contract
        {...}
    }
    the imported definiton on client-side will be
    namespace MyOtherNamespace
    {
    [DataContract]
    struct Contract
    {...}
    }
The imported definiton will always have propertiyes decorated with the DataMember attribute,even if the original type on the service side did not define any properties. if the original service-side definition applied the DataMemer attribute on filelds directly, then the imported type definition will have properties accessing fields whose names will be the name of the DataMember suffixed with Field.
即使在服务端没有定义任何property的情况下,在客户端被导入的数据定义也会生成用[DataMember]修饰的property。如果服务器端的定义是直接通过field来进行定义的,那么在客户端也会生成property来访问其field,同时在其客户端生成的field将会加上Field字段。
   e.g 2
    service-side
    [DataContract]
    struct contract
    {    
    [DataMember]
    public string FirstName;
    [DataMember]
    public string LastName;
    }
    then the imported client-side definition will be:
    [DataContract]
    public partial struct contract
    {
    string FirstNameField;//suffixed with Field along original service-side FirstName;
    string LastNameField;//suffixed with Field along original service-side LastName;

    [DataMember]
    public string FirstName
    {
    get
        {
        return FirstNameField;
        }
    set
        {
        FirstNameField=value;
        }
    }

    [DataMember]
    public string LastName
    {
    get
        {
        return LastNameField;
        }
    set
        {
        LastNameField=value;
        }
    }
   }
//the client can of course manually rework any imported definition to be just like a service-side denifiton.
//当然,我们也可以在客户端手动修改其为与服务器端一样的定义。

Composite Data Contracts 组合数据协议
    wen we define a data contract, we can apply the DataMember attribute on members that are themselves data contracts.
as shown the following
    我们在定义一个数据协议的时候,其DataMember属性也可以应用于一个数据协议上面,即其DataMember也是一个数据协议。
[DataContract]
struct Address
{
    [DataMember]
    public string Street;
    [DataMember]
    public string City;
    [DataMember]
    public string State;
}

[DataContract]
struct Contact
{
    [DataMember]
    public string FirstName;
    [DataMember]
    public string LastName;
    [DataMember]
    public Address Address;
}

你可能感兴趣的:(Data)