Sample publisher

Publish an event topic:

1
2
3
4
5
6
7
8
9
10
public class Publisher
{
    [EventPublication("topic://EventBrokerSample/SimpleEvent")]
    public event EventHandler SimpleEvent;
     
    public void FireSimpleEvent()
    {
        SimpleEvent(this, EventArgs.Empty);
    }
}

Register the publisher with your event broker (you have to hold an instance of the event broker somewhere in your code).

1
2
3
EventBroker eventBroker = ...;
Publisher publisher = new Publisher();
eventBroker.Register(publisher);

On registration of the publisher, the event broker inspects the publisher for published events (events with the EventPublication attribute).

Sample subscriber

Subscribe to an event topic:

1
2
3
4
5
6
7
8
9
10
11
public class Subscriber
{
    [EventSubscription(
        typeof(OnPublisher))]
 
    public void SimpleEvent(object sender, EventArgs e)
    {
        // do something useful or at least funny
    }
}

Register the subscriber with the event broker:

1
2
3
EventBroker eventBroker = ...;
Subscriber subscriber = new Subscriber();
eventBRoker.Register(subscriber);

The event broker will inspect the subscriber on registration for subscription to event topics (methods with the EventSubscription attribute).

If a publisher fires an event topic for that subscribers are registered, then the event broker will relay them to the subscribers by calling the subscription handler methods with the sender and EventArgs the publisher used to fire its event.

Publication options

Simple

1
2
[EventPublication("topic://Simple")]
public event EventHandler SimpleEvent;

With custom Eventargs

Note: CustomEventArgs has simply to be derived from EventArgs.

1
2
[EventPublication("topic://CustomEventArgs")]
public event EventHandler<CustomEventArguments> CustomEventArgs;

Publish multiple event topics with one single event

1
2
3
4
[EventPublication("Event1")]
[EventPublication("Event2")]
[EventPublication("Event3")]
public event EventHandler MultiplePublicationTopics;

Allow only synchronous subscription handlers

1
2
[EventPublication("topic://Simple", HandlerRestriction.Synchronous)]
public event EventHandler AnEvent;

Allow only asynchronous subscription handlers

1
2
[EventPublication("topic://Simple", HandlerRestriction.Asynchronous)]
public event EventHandler AnEvent;

Subscription options

Simple

1
2
[EventSubscription("topic://Simple", typeof(OnPublisher)]
public void SimpleEvent(object sender, EventArgs e) {}

Custom EventArgs

1
2
[EventSubscription("topic://CustomEventArgs"), typeof(OnPublisher))]
public void CustomEventArgs(object sender, CustomEventArgs e) {}

Subscribe multiple event topics

1
2
3
4
[EventSubscription("Event1", typeof(OnPublisher))]
[EventSubscription("Event2", typeof(OnPublisher))]
[EventSubscription("Event3", typeof(OnPublisher))]
public void MultipleSubscriptionTopics(object sender, EventArgs e) {}

Execute handler on background thread (asynchronous)

The event broker creates a worker thread to execute the handler method on. The publisher can immediately continue processing.

1
2
[EventSubscription("topic://Background", typeof(OnBackground))]
public void BackgroundThread(object sender, EventArgs e) {}

Execute handler on UI thread

Use this option if calling from a background worker thread to a user interface component that updates the user interface - no need for Control.Invoke(...) anymore.

1
2
[EventSubscription("topic://UI", typeof(OnUserInterface))]
public void UI(object sender, EventArgs e) {}

Note that if you use the OnUserInterface handler, you have to make sure that you register the subscriber on the user interface thread. Otherwise, the EventBroker won't be able to switch to the user interface thread, and will throw an exception.

Execute handler on UI thread asynchronously

The same as above, but the publisher is not blocked until the subscriber has processed the event.

1
2
[EventSubscription("topic://UIAsync", typeof(OnUserInterfaceAsync))]
public void UI(object sender, EventArgs e) {} 

Simplified subscription handler signatures

If you are not interested in the sender of the event, you can leave the sender out in the handler method:

1
2
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle(EventArgs e) {} 

If you also don't need the event arguments, you can ignore them, too:

1
2
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle() {} 

And if you have a generic event argument EventArgs<T>, you can directly define the content value of the event arguments:

1
2
3
4
5
[EventPublication("topic://handle.me")]
public event EventHandler<EventArgs<string>> Event;
 
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle(string value) {} 

These are the basics about the EventBroker. See the rest of the documentation for more options and details.

EventBroker的更多相关文章

  1. C#编程实践—EventBroker简单实现

    前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...

  2. EventBus总线讲解

    在我们公司经常用到总线,具体的总线是什么让我理解我也不清楚,但是在这几个月下来,我已经知道总线如何使用,现在加上示例讲解总线如何使用. 1. 首先我们的新建一个类,这个类其实是用于总线传递的模型 us ...

  3. Nop源码分析一

    从Global.asax文件开始逐层分析Nop的架构. Application_Start()方法作为mvc启动的第一个方法. 1,首先初始化一个引擎上下文,如下面的代码: EngineContext ...

  4. NopCommerce使用Autofac实现依赖注入

    NopCommerce的依赖注入是用的AutoFac组件,这个组件在nuget可以获取,而IOC反转控制常见的实现手段之一就是DI依赖注入,而依赖注入的方式通常有:接口注入.Setter注入和构造函数 ...

  5. Enhanced RCP: How views can communicate – The e4 way | Tomsondev Blog

    Some weeks ago I published how views can communicate using the EventAdmin-Service. To get things wor ...

  6. NopCommerce架构分析之一----依赖类生成容器

    NopCommerce为了实现松耦合的框架设计目的,使用了IOC框架:Autofac.据有人测试,Autofac是性能好的IOC工具. 1.在IOC中,组件首先需要在IOC中注册,有通过配置文件注册的 ...

  7. How to detect and avoid memory and resources leaks in .NET applications

    By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...

  8. NopCommerce架构分析(转载)

    原文 一,NopCommerce架构分析之开篇 NopCommerce是.net开源项目中比较成熟的一款业务应用框架,也是电子商务系统中的典范.所以很想多学习一下里面的设计和实现方式. 二,NopCo ...

  9. Eclipse Package Explorer视图无法打开

    打开Eclipse后Package Explorer视图无法打开,显示一个红叉,红叉后面的Deatils后,显示下面的内容: java.lang.ArrayIndexOutOfBoundsExcept ...

随机推荐

  1. SpringMVC+spring-security+sitemesh+hibernate+freemarker整合-转

    http://www.oschina.net/code/snippet_170632_46774 代码分享 当前位置: 代码分享 » Java  » Web编程 搜 索   SpringMVC+spr ...

  2. java中abstract

    abstract(抽象)修饰符,可以修饰类和方法 1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半 ...

  3. Oracle数据库——索引、视图、序列和同义词的创建

    一.涉及内容 1.理解索引的概念和类型. 2.掌握创建索引的命令. 3.理解视图的概念和优点. 4.理解可更新视图应具备的特点. 5.掌握创建一般视图和可更新视图的命令. 6.理解序列和同义词的概念和 ...

  4. linux工具类之硬盘检测

    软raidmount /dev/md0 /opt                [root@localhost root]# cp /usr/share/doc/raidtools-1.00.3/ra ...

  5. js防止回车(enter)键提交表单及javascript中event.keycode

      如何防止回车(enter)键提交表单,其实很简单,就一句话.onkeydown="if(event.keyCode==13)return false;"把这句写在from标签里 ...

  6. ios 获取屏幕的属性和宽度

    app尺寸,去掉状态栏 CGRect r = [ UIScreen mainScreen ].applicationFrame; r=0,20,320,460 屏幕尺寸 CGRect rx = [ U ...

  7. 网站首页title 里显示ico图标

    有两种实现方式 图片尺寸大小 第一种:直接做一个favicon.ico 图标放在项目的根目录里就行 第二种:在网页HEAD标记中添加如下代码:<HEAD> <LINK REL=”SH ...

  8. golang的验证码相关的库

    识别库 https://github.com/goghcrow/capture_easy 生成验证码的库 https://github.com/hanguofeng/gocaptcha 生成图片水印 ...

  9. 使用express搭建第一个Web应用【Node.js初学】

    来源:http://jingyan.baidu.com/article/bad08e1ee501e009c8512106.html express是一个开源的node.js项目框架,初学者使用expr ...

  10. 目前已经知道的乐视所有产品各个型号的强刷方法!更新X50

    http://ui.letv.com/thread-43668-1-1.html 很多网友买来电视/盒子仅仅要看,还要折腾这个电视,有时候不小心把系统折腾死了,肿么办?危难之中显身手,我的神帖来了,敬 ...