WCF 第二章 契约 定义类的层次结构

复杂类型一般在代码中以类的形式实现。复杂类更进一步通过增加特殊结构的继承关系来 定义。这种方式,一个通用类型比如”price” 可以派生出为一个更加特殊的类型如”stock price” 或者 “house price”.WCF支持通过在WSDL中合适的表示的类的继承关系,在类结构和XML之间序列化和反序列化它们同时从每个类中取出属性并加入到一个集合 中。
  在列表2.17中,类Price由三个元素和一个子类组成。StockPrice,继承自Price.命名空间应用到两个类上所以它们在XML中由完全合法的名字。每个元素保留自己的命名空间。
 
列表2.17 使用数据契约定义类的层次结构
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Stock
{
[DataContract(Namespace
= "http://EssentialWCF/Price/")]
public class Price
{
[DataMember]
public double CurrentPrice;
[DataMember]
public DateTime CurrentTime;
[DataMember]
public string Currency;
}

[DataContract(Namespace
= "http://EssentialWCF/StockPrice")]
public class StockPrice : Price
{
[DataMember]
public string Ticker;
[DataMember]
public long DailyVolume;
[DataMember]
public double DailyChange;
}
}
生成这两段XML元数据是用来支持列表2.18中显示的基础关系。首先显示Price XML元数据,其次显示StockPrice XML元数据。注意StockPrice导入Price元数据。注意在XSD中,所有的元素都是用minOccurs=0来定义,因为在代码中它们都没有 使用[isRequired=true]来定义属性。
列表2.18 XML元数据中定义的类的层次结构
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns=http://EssentialWCF/Price/
elementFormDefault="qualified"
targetNamespace=http://EssentialWCF/Price/
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="Price">
<xs:sequence>
<xs:element minOccurs="0" name="Currency"
nillable="true" type="xs:string" />
<xs:element minOccurs="0"
name="CurrentPrice" type="xs:double" />
<xs:element minOccurs="0"
name="CurrentTime" type="xs:dateTime" />
</xs:sequence>
</xs:complexType>
<xs:element name="Price"
nillable="true" type="tns:Price" />
</xs:schema>
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://EssentialWCF/StockPrice" el
ementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import namespace="http://EssentialWCF/Price/" />
<xs:complexType name="StockPrice">
<xs:complexContent mixed="false">
<xs:extension xmlns:q1="http://EssentialWCF/Price/" base="q1:Price">
<xs:sequence>
<xs:element minOccurs="0"
name="DailyChange" type="xs:double" />
<xs:element minOccurs="0"
name="DailyVolume" type="xs:long" />
<xs:element minOccurs="0"
name="Ticker" nillable="true" type="xs:string" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="StockPrice" nillable="true" type="tns:StockPrice" />
</xs:schema>
  一个序列化的StockPrice类型的SOAP消息体在列表2.19中显示。注意Price和StockPrice的命名空间完全通过SOAP消息体被从列表2.17的代码搬到列表2.18中的XML元数据中。
列表2.19 SOAP消息体中序列化的类层次结构
<s:Body>
<GetPriceResponse xmlns="http://EssentialWCF/FinanceService/">
<GetPriceResult xmlns:a="http://EssentialWCF/StockPrice"
xmlns:i
="http://www.w3.org/2001/XMLSchema-instance">
<Currency xmlns="http://EssentialWCF/Price">Dollars</Currency>
<CurrentPrice xmlns="http://EssentialWCF/Price">100</CurrentPrice>
<CurrentTime xmlns="http://EssentialWCF/Price">2006-12-13T21:18:51.313-05:00</CurrentTime>
<a:DailyChange>0.012345</a:DailyChange>
<a:DailyVolume>450000</a:DailyVolume>
<a:Ticker>msft</a:Ticker>
</GetPriceResult>
</GetPriceResponse>
</s:Body>

==========

转载自

 

posted @ 2011-06-27 15:12  Gavin Liu  阅读(330)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!