您的位置:首页 > 其它

Chapter 2.1:WCF服务契约的重载与继承详解

2011-01-17 14:56 417 查看
本章主要介绍WCF服务契约的重载与继承,以及设计和分离服务契约的一般原则。

1. 源码下载

WCF服务契约重载:http://files.cnblogs.com/tianzhiliang/WCF.Chapter2.Overloading.rar

WCF服务契约继承:http://files.cnblogs.com/tianzhiliang/WCF.Chapter2.InheritanceReworked.rar

WCF服务契约代理链:http://files.cnblogs.com/tianzhiliang/WCF.Chapter2.InheritanceProxyChaining.rar

2. 服务契约重载

  基于 WSDL 的操作不支持操作重载,但可以使用 OperationContract 特性的 Name 属性,为操作指定别名,手动地启用操作重载。


  客户端导入契约并生成代理时,导入的操作就会包含定义的别名,也可以在客户端使用导入契约的 Name 属性,指定别名并重载方法,使它与导入的操作名保持一致。

using System;
using System.ServiceModel;
namespace WCF.Chapter2.Overloading.Client
{
[ServiceContract]
public interface IContract
{
[OperationContract(Name = "say1")]
string say();

[OperationContract(Name = "say2")]
string say(string str);
}
}

using System;
using System.ServiceModel;
namespace WCF.Chapter2.Overloading.Client
{
public class ClientProxy : ClientBase<IContract>, IContract
{
public ClientProxy()
{ }

public ClientProxy(string configurationName) :
base(configurationName)
{ }

public string say()
{
return base.Channel.say();
}

public string say(string str)
{
return base.Channel.say(str);
}
}
}


3. 服务契约的继承

  服务契约接口支持继承功能,我们定义一个契约层级,Human为第一级,Man和Woman分别继承Huamn,如下:

using System;
using System.ServiceModel;
namespace WCF.Chapter2.InheritanceReworked.Host
{
public class ManService : IMan
{
public string HumanSay()
{
return " 我是人,我会思考!";
}

public string ManSay()
{
return "我是男人,我力气比较大!";
}
}

public class WomanService : IWoman
{
public string HumanSay()
{
return " 我是人,我会思考!";
}

public string WomanSay()
{
return "我是女人,我爱漂亮!";
}
}
}


  客户端服务契约的定义,既可以用取消契约层级的方式实现,也可以用恢复契约层级的方式实现。取消契约层级的方式使用 OperationContract 特性中的 Action 与 ResponseAction 属性,使导入的接口定义保留原来定义每个操作的契约名。

public interface IScientificCalculator
{
[OperationContract(Action = ".../ISimpleCalculator/Add", ReplyAction = ".../ISimpleCalculator/AddResponse")]
int Add(int arg1,int arg2)]

  使用恢复契约层级的方式,如下所示:

using System;
using System.ServiceModel;
namespace WCF.Chapter2.InheritanceReworked.Client
{
[ServiceContract]
public interface IHuman
{
[OperationContract]
string HumanSay();
}

[ServiceContract]
public interface IMan : IHuman
{
[OperationContract]
string ManSay();
}

[ServiceContract]
public interface IWoman : IHuman
{
[OperationContract]
string WomanSay();
}
}

using System;
using System.ServiceModel;
namespace WCF.Chapter2.InheritanceReworked.Client
{
public class HumanProxy : ClientBase<IHuman>, IHuman
{
public HumanProxy()
{ }

public HumanProxy(string configurationName) :
base(configurationName)
{ }

public string HumanSay()
{
return base.Channel.HumanSay();
}

}
}

using System;
using System.ServiceModel;
namespace WCF.Chapter2.InheritanceReworked.Client
{
public class ManProxy : ClientBase<IMan>, IMan
{
public ManProxy()
{ }

public ManProxy(string configurationName) :
base(configurationName)
{ }

public string HumanSay()
{
return base.Channel.HumanSay();
}

public string ManSay()
{
return base.Channel.ManSay();
}
}
}

using System;
using System.ServiceModel;
namespace WCF.Chapter2.InheritanceReworked.Client
{
public class WomanProxy : ClientBase<IWoman>, IWoman
{
public WomanProxy()
{ }

public WomanProxy(string configurationName) :
base(configurationName)
{ }

public string HumanSay()
{
return base.Channel.HumanSay();
}

public string WomanSay()
{
return base.Channel.WomanSay();
}
}
}


4. 客户端服务契约代理链(Proxy Chaining)

  如果服务契约层级过多,在客户端实现服务契约层级就会显得冗余且复杂,可以使用服务契约代理链(Proxy Chaining)来解决该问题。服务契约代理链为代理建立了 IS-A 关系(继承关系),保证了代码的重用。实现方法:将最顶层的基契约代理直接继承于ClientBase<T>,T为最底层的子接口类型。所有的其他代理则直接继承于它们的上一级代理,同时实现各自的契约接口。


总结服务契约分解的规则和方法如下:

避免设计只具有一个操作的服务契约。一个服务契约体现了实体的特征,如果服务只有一个操作,则过于单调,没有实际的意义。此时,就应该检查它是否使用了太多的参数?它的粒度是否过粗,因此需要分解为多个操作?是否需要将该操作转移到已有的服务契约中?

服务契约成员的最佳数量应介于 3 到 5 之间。如果设计的服务契约包含了多个操作,例如 6 到 9 个,仍然可能工作良好。但是,我们需要判断这些操作是否会因为过度分解而需要合并。如果服务契约定义了 12 个甚至更多的操作,毫无疑问,我们需要将这些操作分解到单独的服务契约中,或者为它们建立契约层级。

避免定义准属性操作(Property -Like Operation)

[OperationContract]
long GetVetClinicNumber();


服务契约允许客户端在调用抽象操作时,不用关心具体的实现细节。准属性操作由于无法封装状态的管理,因此在封装性的表现上差强人意。在服务端,我们可以封装读写变量值的业务逻辑,但在理想状态下,我们却不应该干涉客户端对属性的使用。客户端应该只负责调用操作,而由服务去管理服务对象的状态。这种交互方式应该被表示为 DoSomething()样式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: