代码改变世界

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

  DanielWise  阅读(1177)  评论(0编辑  收藏  举报
复杂类型一般在代码中以类的形式实现。复杂类更进一步通过增加特殊结构的继承关系来定义。这种方式,一个通用类型比如”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>
复制代码
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示