Fork me on GitHub
发布订阅模式 之 同步订阅、异步订阅和离线订阅

发布订阅模式 之 同步订阅、异步订阅和离线订阅

背景

 

事件驱动解除了发布者和订阅者之间的耦合,在UI层面,我明经常采用这种编程理念。服务器端最近也开始流行起来了,我也一直小范围的在采用。今天就跟大家分享一下我写的一个小框架。

 

框架原理

 

一张图片胜过前言万语。

 

 

代码示例

 

下载地址:http://yunpan.cn/Q5SUcWdiA2mmk

 

项目结构

 

 

关键代码

 

TestEvent.cs

 

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 using Happy.Event;
 8 using Happy.Event.Offline;
 9 
10 namespace Happy.Event.Demo.Simple
11 {
12     [TestEventInterceptor1Attibute(1)]
13     [TestEventInterceptor2Attibute(2)]
14     [Persistable(3)]
15     internal sealed class TestEvent : IEvent
16     {
17     }
18 }
复制代码

 

Program.cs

 

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Threading;
 7 
 8 using Microsoft.Practices.ServiceLocation;
 9 using Microsoft.Practices.Unity;
10 
11 using Happy.Event;
12 using Happy.Event.Offline;
13 
14 namespace Happy.Event.Demo.Simple
15 {
16     class Program
17     {
18         static readonly string _QueueName = "Happy.Event.Demo.Simple";
19 
20         static void Main(string[] args)
21         {
22             InitUnity();
23 
24             var writeQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName);
25             EventPublisher.Current.AddService(writeQueue);
26 
27             EventPublisher.Current.Publish(new TestEvent());
28 
29             new Thread(() =>
30             {
31                 var readQueue = OfflineEventQueueFactory.CreateMSMQOfflineEventQueue(_QueueName);
32                 var offlineEventProcessor = new OfflineEventProcessor(readQueue);
33 
34                 offlineEventProcessor.Start();
35             })
36             .Start();
37 
38             Console.ReadLine();
39         }
40 
41         static void InitUnity()
42         {
43             var container = new UnityContainer();
44 
45             ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
46 
47             container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber1>("TestSyncEventSubscriber1");
48             container.RegisterType<ISyncEventSubscriber<TestEvent>, TestSyncEventSubscriber2>("TestSyncEventSubscriber2");
49             container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber1>("TestAsyncEventSubscriber1");
50             container.RegisterType<IAsyncEventSubscriber<TestEvent>, TestAsyncEventSubscriber2>("TestAsyncEventSubscriber2");
51             container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber1>("TestOfflineEventSubscriber1");
52             container.RegisterType<IOfflineEventSubscriber<TestEvent>, TestOfflineEventSubscriber2>("TestOfflineEventSubscriber2");
53         }
54     }
55 }
复制代码

 

执行结果

 

 

备注

 

基于事件的编程,我在真实项目中有过实战的,这个框架目前还没在项目中使用。研发先行。

 

 

Ajax解决浏览器的缓存问题

 
        今天做项目,几乎所有的提交都是通过ajax来提交,我测试的时候发现,每次提交后得到的数据都是一样的,调试可以排除后台
 
代码的问题,所以问题肯定是出在前台。每次清除缓存后,就会得到一个新的数据,所以归根到底就是浏览器缓存问题。纠结了很
 
久,终于解决了,在这里总结一下。
 
        我们都知道ajax能提高页面载入的速度主要的原因是通过ajax减少了重复数据的载入,也就是说在载入数据的同时将数据缓存到
 
内存中,一旦数据被加载其中,只要我们没有刷新页面,这些数据就会一直被缓存在内存中,当我们提交 的URL与历史的URL一致
 
时,就不需要提交给服务器,也就是不需要从服务器上面去获取数据,虽然这样降低了服务器的负载提高了用户的体验,但是我们
 
不能获取最新的数据。为了保证我们读取的信息都是最新的,我们就需要禁止他的缓存功能。
    
        解决方案有如下几种:
 
        1、在ajax发送请求前加上 anyAjaxObj.setRequestHeader("If-Modified-Since","0")。
 
        2、在ajax发送请求前加上 anyAjaxObj.setRequestHeader("Cache-Control","no-cache")。
 
        3、在URL后面加上一个随机数: "fresh=" + Math.random();。
 
        4、在URL后面加上时间搓:"nowtime=" + new Date().getTime();。
    
        5、如果是使用jQuery,直接这样就可以了$.ajaxSetup({cache:false})。这样页面的所有ajax都会执行这条语句就是不需要保存缓
 
存记录。
 
 

posted on 2013-05-21 16:59  HackerVirus  阅读(751)  评论(0编辑  收藏  举报