EventBroker
Sample publisher
Publish an event topic:
1
2
3
4
5
6
7
8
9
10
|
public class Publisher { 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
|
public event EventHandler SimpleEvent; |
With custom Eventargs
Note: CustomEventArgs
has simply to be derived from EventArgs
.
1
2
|
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
|
public event EventHandler AnEvent; |
Allow only asynchronous subscription handlers
1
2
|
public event EventHandler AnEvent; |
Subscription options
Simple
1
2
|
public void SimpleEvent( object sender, EventArgs e) {} |
Custom EventArgs
1
2
|
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
|
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
|
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
|
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
|
public void Handle(EventArgs e) {} |
If you also don't need the event arguments, you can ignore them, too:
1
2
|
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
|
public event EventHandler<EventArgs< string >> Event; public void Handle( string value) {} |
These are the basics about the EventBroker. See the rest of the documentation for more options and details.
EventBroker的更多相关文章
- C#编程实践—EventBroker简单实现
前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...
- EventBus总线讲解
在我们公司经常用到总线,具体的总线是什么让我理解我也不清楚,但是在这几个月下来,我已经知道总线如何使用,现在加上示例讲解总线如何使用. 1. 首先我们的新建一个类,这个类其实是用于总线传递的模型 us ...
- Nop源码分析一
从Global.asax文件开始逐层分析Nop的架构. Application_Start()方法作为mvc启动的第一个方法. 1,首先初始化一个引擎上下文,如下面的代码: EngineContext ...
- NopCommerce使用Autofac实现依赖注入
NopCommerce的依赖注入是用的AutoFac组件,这个组件在nuget可以获取,而IOC反转控制常见的实现手段之一就是DI依赖注入,而依赖注入的方式通常有:接口注入.Setter注入和构造函数 ...
- 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 ...
- NopCommerce架构分析之一----依赖类生成容器
NopCommerce为了实现松耦合的框架设计目的,使用了IOC框架:Autofac.据有人测试,Autofac是性能好的IOC工具. 1.在IOC中,组件首先需要在IOC中注册,有通过配置文件注册的 ...
- 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 ...
- NopCommerce架构分析(转载)
原文 一,NopCommerce架构分析之开篇 NopCommerce是.net开源项目中比较成熟的一款业务应用框架,也是电子商务系统中的典范.所以很想多学习一下里面的设计和实现方式. 二,NopCo ...
- Eclipse Package Explorer视图无法打开
打开Eclipse后Package Explorer视图无法打开,显示一个红叉,红叉后面的Deatils后,显示下面的内容: java.lang.ArrayIndexOutOfBoundsExcept ...
随机推荐
- Android 反编译 代码注入之HelloWorld
为了向经典的"Hello, World"致敬,我们也从一个简单的程序开始HelloWorld.apk.当你把这个APK安装到手机上运行后,在屏幕上就显示一行文字"Hell ...
- *.bz2和*.gz分别是什么压缩格式
这两个都是linux常用的压缩格式,通常用来压缩源代码包,因为源代码文件过多,它们还经常跟tar命令结合使用所以一般下载linux的源代码就有.tar.bz2,.tar.gz这样的格式其中bz2格式的 ...
- 项目ppt演讲与阶段性总结
☆车老师讲解PPT项目: 1.汉企0410天启网络公司 2.Ppt--画龙点睛 3.项目制作背景-->点到人心上,别一堆文字,别虚,点出1234 4.说话量化.具象化:明天下午5.00做完,做不 ...
- css字体文件
├── glyphicons-halflings-regular.eot├── glyphicons-halflings-regular.svg├── glyphicons-halflings-reg ...
- WCF 遇到 由于线程退出或应用程序请求,已放弃 I/O 操作 ListenerContextInputStream
异常类型:IOException 异常消息:An exception has been thrown when reading the stream. 异常信息: at System.ServiceM ...
- 通信原理读书笔记:常规AM调制的功率
Proakis,通信系统原理,p101: 两个不同频率正弦和的功率为其功率的和. 计算功率时,和的平方展开后会出现两个正弦乘积项,按积化和差展开后在公共周期内积分为零.
- 战胜忧虑<4>——让平均概率来替你分忧
让平均概率来替你分忧. 我们可以根据事情发生的平均率来评估我们的忧虑究竟值不值,如此一来,我想你和我应该可以去除99%的忧虑. 故事 我从小生长在密苏里州的一个农场,有一天,正帮妈妈采摘樱桃的时候,我 ...
- [tty与uart]UART中的硬件流控RTS与CTS
转自:http://blog.csdn.net/zeroboundary/article/details/8966586 在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES ) ...
- 两分钟彻底让你明白Android Activity生命周期(图文)!
大家好,今天给大家详解一下Android中Activity的生命周期,我在前面也曾经讲过这方面的内容,但是像网上大多数文章一样,基本都是翻译Android API,过于笼统,相信大家看了,会有一点点的 ...
- 值得推荐的C/C++框架和库 (真的很强大)
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...