Data Contract for Collection
我們照例用例子來說明問題
namespace Artech
{
[DataContract]
public class Order
{
[DataMember]
public Guid OrderID
{ get; set; }
[DataMember]
public DateTime OrderDate
{ get; set; }
}
public class OrderCollection : List
{
}
}
下面是Service Contract的定義
namespace Artech
{
[ServiceContract]
public interface IOrderManager
{
[OperationContract(Name =
void Process(OrderCollection orders);
}
面是OrderCollection 在XSD中的呈現
<?xml version=
<xs:schema elementFormDefault=
targetNamespace=
xmlns:xs=
xmlns:tns=
xmlns:ser=
<xs:import
schemaLocation=
namespace=
<xs:complexType name=
<xs:sequence>
<xs:element minOccurs=
nillable=
</xs:sequence>
</xs:complexType>
<xs:element name=
<xs:complexType
name=
<xs:sequence>
<xs:element minOccurs=
<xs:element
minOccurs=
</xs:sequence>
</xs:complexType>
<xs:element
name=
</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
}
}
[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>
我們照例看看通過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