使用WCF的层次设计--LitwareHR

来源:互联网 发布:linux系统编程手册chm 编辑:程序博客网 时间:2024/06/15 13:23

http://www.codeplex.com/litwarehr

LitwareHR是微软的一个开源代码,主要目的是为了解释如何使用WCF等技术开发一个实现微软的SaaS,如下:

To be the embodiment of the architectural guidance described in our whitepapers

To demonstrate how the Microsoft platform is used in the creation of SaaS solutions

关于SaaS这里不再描述,LitwareHR在架构上也有很多可以参考的地方,具体可以去看他的文档和代码

这里关注一下LitwareHR是如何使用WCF的,尤其是在使用WCF的时候是如何划分层次结构的.

其实,如果我们关注微软的软件工厂,就会发现在微软的Service Factory里面,也是采用了类似的层次结构

总的来说,在LitwareHR里面,对于某一个功能来说,从门户端(下面称之为客户端)到中间层(下面称之为服务端)一共会涉及这么几个模块:

Gateway,Host,Service,Contract,BusinessLogic,这里,从其中的一个具体业务,GetMainMenuList作为例子.

其中:

Gateway,是一个典型的Service Gateway的Enterprise Pattern的应用,包装了对服务的应用,这个是跑在客户端的,包装在一个gateways的DLL中

比如:

PresentationGateway.cs中

static public MainMenuItem[] GetMainMenuList()

{

using (SecureChannel channel = new SecureChannel())

{

return channel.GetMainMenuList();

}

}

 private class SecureChannel : ClientBase<IPresentationSC>, IPresentationSC

{

public MainMenuItem[] GetMainMenuList()

{

return base.Channel.GetMainMenuList();

}

}

上面的IPresentationSC以及MainMenuItem都是Contract里面描述的接口以及对象

Contract,是接口,包装在独立的DLL中,客户端和服务端都会用到

比如:

MainMenuItem.cs中描述了传输的数据对象

[DataContract, Serializable]

public class MainMenuItem

{

private object _id;

[DataMember]

public object Id

{

get { return _id; }

set { _id = value; }

}

….

}

IPresentationSC.cs中描述了服务接口,

[ServiceContract]

public interface IPresentationSC

{

[OperationContract]

MainMenuItem[] GetMainMenuList();

}

 Host:很简单,就是宿主程序,这里是宿主的IIS里面的,所以就是一个web site,自然是服务端的

比如

在其中的PresentationService.svc中(就这么一句话,这也是适用IIS作为WCF宿主的好处之一)

<% @ServiceHost Language=C# Debug="true" Service="Shp.Runtime.Services.PresentationService" %>

 Service:这是host文件里面指定的service类,自然是服务端的

比如:

在PresentationService.cs中

public class PresentationService : IPresentationUC, IPresentationSC

{

...

public MainMenuItem[] GetMainMenuList()

{

Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

 Guid tenantId = Context.TenantId;

if (tenantId == Guid.Empty)

throw new System.Security.SecurityException();    

return PresentationLogic.GetMainMenuList(tenantId);

}

}

   

这里调用到了PresentationLogic类, LitwareHR中,这个东西和Service放在一个DLL中,但是不同的cs

这就是前面提到的BusinessLogic,比如PresentationLogic.cs,实现具体的业务逻辑

当然,如果足够复杂的话,其实底层还可以实现业务实体以及业务实体转换的模块,这些可以在service factory里面看到微软的推荐做法,这里不说了    

还有一个没有提到的,就是客户端和服务端的配置文件里面都会有相应的内容,这里不赘述了    

总结:可以看出,使用WCF的时候,其程序层次结构相比较web service会多一些,更为灵活,但是实际上,对于web service,我们也可以抽象出这么多层次,只不过比较别扭而已(比如在web service里面,实际上contract的概念并不是很强)  

原创粉丝点击