- Rongsen.Com.Cn 版权所有 2008-2010 京ICP备08007000号 京公海网安备11010802026356号 朝阳网安编号:110105199号
- 北京黑客防线网安工作室-黑客防线网安服务器维护基地为您提供专业的
服务器维护
,企业网站维护
,网站维护
服务 - (建议采用1024×768分辨率,以达到最佳视觉效果) Powered by 黑客防线网安 ©2009-2010 www.rongsen.com.cn
作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0 |
经过一番思考后我还是回到我自己的Solution上,不过我又有了新的看法了。不是要将数据先缓存么?看看这个缓存,实际上她也是个观察者,至于她执行怎样的缓存策略是她的事情,如是我又有了一个新类:
//这里的代码接上篇Post
using System;
using System.Collections.Generic;
using System.Text;
namespace ForyourSoft.NetTraffic.Framework
{
public sealed class DataBase
{
private IList<string> _buffer = new List<string>();
private static DataBase _instance = null;
public static void Subscribe()
{
if (_instance == null)
_instance = new DataBase(Monitor.Current);
}
public DataBase(Monitor monitor)
{
monitor.OnMessage += new EventHandler<Monitor.MessageEventArgs>(monitor_OnMessage);
}
void monitor_OnMessage(object sender, Monitor.MessageEventArgs e)
{
_buffer.Add(e.Message);
if (_buffer.Count >= Config.BufferSize)
{
//将数据添加到数据库
}
}
}
}
PS:由于系统中我们只需要这样唯一一个订阅者,所以我将其实现为一个单件,在Application_Start的时候调用DataBase.Subscribe()。
现在系统是这样的结构:
[img]http://images.cnblogs.com/cnblogs_com/yuyijq/aspnetpattrern2.jpg[/img]
可以设想以后还会有更多的订阅者。果然,昨天客户要求在下一个版本中管理员分角色,各个角色看到的数据不同的,只有超级管理员才可以监视所有数据,OMG,呵呵,不过还好,我只需要添加几个订阅者就可以轻松搞定。
后记:也许是我的文章标题没有起好,也许很多人得到模式恐惧症,提到模式总是要来考察一下你的case,不是那种Enterprise级别的用了pattern就是过火了。其实这篇文章的内容里没有一点模式的气息,只是用.net的Event实现观察者模式的思想,我想如果合适,今天模式的投资,明天你会有收获的。
在.net里面我们有事件(event),那就无需使用传统的观察者模式的模型了
那么我首先实现一个Monitor类,这个类用来接收客户端传递来的数据并将数据广播出去
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
public class DataEventArgs : EventArgs
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
public string Message
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{get;set;}
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
public DataEventArgs(string message)
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
this.Message = message;
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
}
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]
}
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
public class Monitor
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
public event EventHandler<DataEventArgs> DataIn;
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
private void SendData(string message)
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
if (DataIn != null)
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
DataEventArgs e = new DataEventArgs(message);
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
DataIn(this, e);
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
}
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
}
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
/**//// <summary>
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
/// 这个方法被一个HttpHandler调用,客户端向这个Handler发送数据
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
/// 数据处理后作为字符串传递给该方法,该方法然后将数据广播出去
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
/// </summary>
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
/// <param name="message">处理后的数据</param>
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
public void ReciveData(string message)
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
[img]http://www.cnblogs.com/Images/dot.gif[/img]{
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
SendData(message);
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
}
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]}
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
我要申请本站:N点 | 黑客防线官网 | |
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479 |