熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

WCF中的Data Contract:WCF Data Contract對Collection和D

2013-11-13 10:33:30  來源: .NET編程 

  Data Contract for Collection

  我們照例用例子來說明問題在這裡我們創建一個批量處理Order的Service於是我們創建了一個OrderCollection Type

  

  namespace ArtechSpecialDataContractContract
{
[DataContract]
public class Order
{
[DataMember]
public Guid OrderID
{ get; set; }

  [DataMember]
public DateTime OrderDate
{ get; set; }
}

  public class OrderCollection : List
{

  }
}

  下面是Service Contract的定義

  

  namespace ArtechSpecialDataContractContract
{
[ServiceContract]
public interface IOrderManager
{
[OperationContract(Name = ProcessWithCollection)]
void Process(OrderCollection orders);
}


面是OrderCollection 在XSD中的呈現

  

  <?xml version= encoding=utf ?>
<xs:schema elementFormDefault=qualified

  targetNamespace=

  
xmlns:xs=

  xmlns:tns=
xmlns:ser=>
<xs:import

  schemaLocation=d
namespace= />
<xs:complexType name=ArrayOfOrder>
<xs:sequence>
<xs:element minOccurs= maxOccurs=unbounded name=Order

  nillable=true type=tns:Order />
</xs:sequence>
</xs:complexType>
<xs:element name=ArrayOfOrder nillable=true type=tns:ArrayOfOrder/>
<xs:complexType
name=Order>
<xs:sequence>
<xs:element minOccurs= name=OrderDate type=xs:dateTime/>
<xs:element
minOccurs= name=OrderID type=ser:guid/>
</xs:sequence>
</xs:complexType>
<xs:element
name=Order nillable=true type=tns:Order/>
</xs:schema>


  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="OrderManagerService.

  IOrderManager")]
public interface IOrderManager {
[System.ServiceModel.OperationContractAttribute(Action=

  "",

  ReplyAction="

  ProcessWithCollectionResponse")]
void ProcessWithCollection(Artech.SpecialDataContract.Client.OrderManagerService.

  Order[] orders);
}

  因為Array相對很Common的數據類型,基本上所有的廠商均提供了對Array的支持,這也是WCF在通過Add Service Reference生成Client端代碼的時候,會生成Array的原因。tw.wiNGwIT.COm不過並不是我們只有唯一的選擇,事實上VS為此提供了擴展,允許我們對於基於Collection 的Data Contract生成我們需要的各種類型,我們只需要在Add Service Reference的時候選擇“Configure Service Reference”進行相應的配置:

  通過上面的截圖,我們發現在Collection Type一項我們有若干選項,我們可以選擇我們希望生成的數據類型:Array,ArrayList,LinkedList,Generic List,Collection和BindingList。

  Data Contract for Dictionary

  前面的內容,我們分別討論了基於Generic和Collection的Data Contract,接下來,我們來討論最後一個特殊的數據類型的Data Contract:Dictionary。

  延續上面的Order Batch Processing的例子,不過我們現在處理的不是一個OrderCollection對象,而是一個Dictionary對象,線面是Service Contract和Order的定義:

  

  namespace Artech.SpecialDataContract.Contract
{
[ServiceContract]
public interface IOrderManager
{
[OperationContract(Name = "ProcessWithCollection")]
void Process(OrderCollection orders);

  [OperationContract(Name = "ProcessWithDictionary")]
void Process(IDictionary orders);
}

   [DataContract]
public class Order
{
[DataMember]
public Guid OrderID
{ get; set; }

  [DataMember]
public DateTime OrderDate
{ get; set; }
}

  閒話少說,我們來看XSD:

  

  <?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"

  targetNamespace=""

  xmlns:xs=""

  xmlns:tns=""

  xmlns:ser="">
<xs:import schemaLocation=

  "d1"

  namespace=""/>
<xs:import schemaLocation=

  "d2"

  namespace=""/>
<xs:complexType name="ArrayOfKeyValueOfguidOrder_SkVQi6O3">
<xs:annotation>
<xs:appinfo>
<IsDictionary xmlns=

  "true">true</IsDictionary>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="KeyValueOfguidOrder_SkVQi6O3">
<xs:complexType>
<xs:sequence>
<xs:element name="Key" type="ser:guid"/>
<xs:element name="Value" nillable="true" type="q1:Order"

  xmlns:q1=""/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="ArrayOfKeyValueOfguidOrder_SkVQi6O3" nillable="true"

  type="tns:ArrayOfKeyValueOfguidOrder_SkVQi6O3"/>
</xs:schema>


Data Contract的名稱為ArrayOfKeyValueOfguidOrder_SkVQi6O3=ArrayOfKeyValueOf+guid(Key的類型)+Order(Value)+_SkVQi6O3(Hash Value)。從該XSD的結構我們不難看出,只是一個數組,每個元素為Key-Value pair。

  

   

  我們照例看看通過Add Service Reference方式生成的Client端code中的對應的定義:

  

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName=

  "OrderManagerService.IOrderManager")]
public interface IOrderManager {              
[System.ServiceModel.OperationContractAttribute(Action=

  "",

  ReplyAction="")]
void ProcessWithDictionary(System.Collections.Generic.Dictionary<System.Guid,

  Artech.SpecialDataContract.Client.OrderManagerService.Order> orders);
}

  生成的是一個System.Collections.Generic.Dictionary類型。同Collection一樣,也依然可以有多種選擇:

  

  

圖1   



From:http://tw.wingwit.com/Article/program/net/201311/13994.html
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.