• 幸福框架:发布订阅模式 之 同步订阅、异步订阅和离线订阅


    背景

    事件驱动解除了发布者和订阅者之间的耦合,在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 }

    执行结果

    备注

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

  • 相关阅读:
    免费的编程中文书籍索引 from github
    win7 Python 环境 准备 配置
    SQL Server 2008 允许远程链接,适用于广域网和局域网
    CTP API开发期货自动交易平台概论
    一步一步重写 CodeIgniter 框架 (4) —— load_class 管理多个对象实例的思路
    一步一步重写 CodeIgniter 框架 (3) —— 用面向对象重构代码
    一步一步重写 CodeIgniter 框架 (2) —— 实现简单的路由功能
    一步一步重写 CodeIgniter 框架 (1) —— url 如何映射到具体的方法
    一步一步重写 CodeIgniter 框架 -- 原因和思路
    GDI双缓冲绘图
  • 原文地址:https://www.cnblogs.com/happyframework/p/3089785.html
Copyright © 2020-2023  润新知