技术开发 频道

浅析Web Service适配器

示例 

    为了统一各适配器的统一实现,先定义了一个统一适配器接口IServiceAdapter:


UML 适配器接口的根对象

//C#
namespace VisionTask.Training.ServicePattern.ServiceAdapter
{
/// <summary>
/// 所有Web Service 适配器的根接口
/// </summary>
public interface IServiceAdapter
{
string Name { get;}
}
}

单纯数据XSD适配

1、准备


图5:生产者服务接口返回的报价信息Schema定义


图6:消费者服务接口返回的报价信息定义


图7:两个XSD之间的映射关系

//XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:n1=http://www.visionlogic.com/trade
xmlns:xs=http://www.w3.org/2001/XMLSchema
xmlns=http://www.visionlogic.com/trade
exclude-result-prefixes="n1 xs">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/n1:Quote">
<Quote>
<xsl:attribute name="xsi:schemaLocation">
http://www.visionlogic.com/trade QuoteTarget.xsd</xsl:attribute>
<xsl:for-each select="@Id">
<xsl:attribute name="SequenceNo">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:for-each select="@Company">
<xsl:attribute name="CompanyName">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
</Quote>
</xsl:template>
</xsl:stylesheet>

2、定义抽象的Service Adapter

    为了进一步扩展的考虑,这里定义一个IXsdServiceAdapter的抽象数据适配接口,由于这个接口操作的内容一般而言都是Web Service调用过程中的XML内存文件,因此该接口仅有一个统一的Transform方法。


UML 抽象Adapter的定义

//C#
using System.Xml;
using VisionTask.Training.ServicePattern.Common.Xml;
namespace VisionTask.Training.ServicePattern.ServiceAdapter
{
/// <summary>
/// 所有单纯XSD 数据适配器的根接口
/// </summary>
public interface IXsdServiceAdapter : IServiceAdapter
{
/// <summary>
/// 该适配器需要使用的XSLT 定义文件。
/// </summary>
string Xslt { get; set;}
/// <summary>
/// 依据XSLT 对XML 数据进行转换。
/// </summary>
XmlDocument Transform(XmlDocument source);
}

/// <summary>
/// 抽象IXsdServiceAdapter 实现。
/// </summary>
public abstract class BaseXsdServiceAdapter : IXsdServiceAdapter
{
private string xslt;
public BaseXsdServiceAdapter(string xslt) { this.xslt = xslt; }

public virtual string Xslt
{
get { return xslt; }
set { xslt = value; }
}
public virtual XmlDocument Transform(XmlDocument source)
{
return XmlHelper.Transform(xslt, source);
}
public abstract string Name { get;}
}
}

3、定义作为Adaptee的生产者Web Service

//C#
using System;
using System.Text;
using System.Xml;
using System.Web.Services;
namespace VisionTask.Training.ServicePattern.UtilityService.XsdAdapter
{
[WebService(Namespace = "http://www.visionlogic.com/trade")]
public class ProducerService : System.Web.Services.WebService
{
/// <summary>
/// 这里的XmlDocument 是遵循Quote.xsd 的数据。
/// </summary>
/// <returns></returns>
[WebMethod]
public XmlDocument GetQuote(string company, string id)
{
string quoteContent = string.Format(Resource.Quote, company, id);
XmlDocument doc = new XmlDocument();
doc.LoadXml(quoteContent);
return doc;
}
}
}
//Unit Test
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UtilityService.UnitTest.XsdProducer;
using System.Xml;
namespace UtilityService.UnitTest.XsdAdapter
{
[TestClass()]
public class ProducerServiceTest
{
[TestMethod]
public void Test()
{
ProducerService service = new ProducerService();
string company = "VisoinTask";
string id = "2007-07-21";
XmlNode node = service.GetQuote(company, id);
Assert.IsNotNull(node);
// 这里的 attribute 名称与 Quote.xsd一致。
Assert.AreEqual<string>(id, node.Attributes["Id"].Value);
Assert.AreEqual<string>(company, node.Attributes["Company"].Value);
}
}
}

 

0
相关文章