为了提供更好的服务,网站可以提供notification机制以及时与自己的客户沟通。本文讨论notification服务的一种实现方法。
自上而下,notification服务可以分为三层:
– 具体notification服务的实现层,负责实现具体通知内容的构建;
– notification服务的管理层,负责获取notification数据、分发到具体实现等;
– notification服务的通讯层,负责网络发送,如email、短消息等。
1. 通讯层
通讯层采用factory模式,notifyservicemanager的getnotifyservice(notifyserviceenum type)方法返回以下接口对象:
public interface inotifyservice
{
int send(string sender, string receiver, string subject, string content);
int batchsend(string sender, string[] receivers, string subject, string content);
}
目前notifyserviceenum包括email_service和sms_service两种。
2. 管理层
管理层提供以下三种功能:
– 获取数据:目前采用使用sqlxml的webservice支持一文中的方式访问数据库,返回dataset的集合,每个dataset包含一种需要通知的业务数据;
– 分发通知:采用类似prototype模式的方式,一种通知业务对应一个实现isendnotification的对象;
– 通知机制:可以采用定时间隔通知,或有数据时通知等方式。
2.1 获取数据
如下调用webservice:
mynotificationservice.notification service = new mynotificationservice.notification();
object[] rc = service.getinstantnotification();
2.2 分发通知
对每类通知的每一行内容调用isendnotification的send方法:
for(int notifytype=0; notifytype<rc.length-1; notifytype++)
{
datarowcollection rows = (rc[notifytype] as dataset).tables[0].rows;
for(int i=0; i<rows.count; i++)
{
_sendnotifications[notifytype].send(rows[i]);
}
}
其中isendnotification定义如下:
public interface isendnotification
{
void send(datarow row);
}
_sendnotifications为一isendnotification类的数组,其元素是实现了isendnotification接口的具体发送的实现。
2.3 通知机制
通知机制可以采用定时间隔的方式,相当于poll方式;或者有数据通知的方式,相对于push方式。理论上,push方式效率高一些,但数据源是数据库时要采用push模式需要额外编程。
小鸡射手目前采用的是poll方式,并将在以后的blog中讨论push模式,即所谓的sql dependency的实现。
3. 具体业务
具体业务实现的核心工作是将system.data.datarow对象转化为string对象,可以采用template的方式实现。
4.优缺点
本方法的主要优点是可扩充性,包括通讯方式的扩充和具体业务的扩充;
缺点是仅适合于较简单内容的通知,即通知内容需要放在system.data.datarow中表示。如果通知内容较为复杂,如通知由几个dataset组成,那本方法不适用。如,小鸡射手是采用xslt方式来处理有多个dataset内容通知的,不过这样的通知内容只能发发email啦,短消息是容不下的了,让我们共同等待mms的普及吧,:-)
