為了提供更好的服務(wù),網(wǎng)站可以提供Notification機制以及時與自己的客戶溝通。本文討論Notification服務(wù)的一種實現(xiàn)方法。
自上而下,Notification服務(wù)可以分為三層: - 具體Notification服務(wù)的實現(xiàn)層,負責實現(xiàn)具體通知內(nèi)容的構(gòu)建; - Notification服務(wù)的管理層,負責獲取Notification數(shù)據(jù)、分發(fā)到具體實現(xiàn)等; - Notification服務(wù)的通訊層,負責網(wǎng)絡(luò)發(fā)送,如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. 管理層 管理層提供以下三種功能: - 獲取數(shù)據(jù):目前采用使用SQLXML的WebService支持一文中的方式訪問數(shù)據(jù)庫,返回DataSet的集合,每個DataSet包含一種需要通知的業(yè)務(wù)數(shù)據(jù); - 分發(fā)通知:采用類似Prototype模式的方式,一種通知業(yè)務(wù)對應(yīng)一個實現(xiàn)ISendNotification的對象; - 通知機制:可以采用定時間隔通知,或有數(shù)據(jù)時通知等方式。
2.1 獲取數(shù)據(jù) 如下調(diào)用WebService: myNotificationService.Notification service = new myNotificationService.Notification(); object[] rc = service.GetInstantNotification(); 2.2 分發(fā)通知 對每類通知的每一行內(nèi)容調(diào)用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類的數(shù)組,其元素是實現(xiàn)了ISendNotification接口的具體發(fā)送的實現(xiàn)。
2.3 通知機制 通知機制可以采用定時間隔的方式,相當于Poll方式;或者有數(shù)據(jù)通知的方式,相對于Push方式。理論上,Push方式效率高一些,但數(shù)據(jù)源是數(shù)據(jù)庫時要采用Push模式需要額外編程。 小雞射手目前采用的是Poll方式,并將在以后的Blog中討論Push模式,即所謂的SQL Dependency的實現(xiàn)。
3. 具體業(yè)務(wù) 具體業(yè)務(wù)實現(xiàn)的核心工作是將System.Data.DataRow對象轉(zhuǎn)化為string對象,可以采用Template的方式實現(xiàn)。
4.優(yōu)缺點 本方法的主要優(yōu)點是可擴充性,包括通訊方式的擴充和具體業(yè)務(wù)的擴充; 缺點是僅適合于較簡單內(nèi)容的通知,即通知內(nèi)容需要放在System.Data.DataRow中表示。如果通知內(nèi)容較為復雜,如通知由幾個DataSet組成,那本方法不適用。如,小雞射手是采用XSLT方式來處理有多個DataSet內(nèi)容通知的,不過這樣的通知內(nèi)容只能發(fā)發(fā)Email啦,短消息是容不下的了,讓我們共同等待MMS的普及吧,:-)
|