為了提供更好的服務,網站可以提供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的普及吧,:-)
|