This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
eventbrokertutorial.html
242 lines (205 loc) · 7.75 KB
/
eventbrokertutorial.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
---
layout: documentation
title: EventBroker
teaser: Decoupled eventing with automatic thread switching
navigation:
- name: Overview
link: eventbroker.html
- name: Tutorial
link: eventbrokertutorial.html
- name: Registration by Attribute
link: eventbrokerregistrationbyattribute.html
- name: Registration over Interface
link: eventbrokerregistrationoverinterface.html
- name: Registration by Registrar
link: eventbrokerregistrationbyregistrar.html
- name: Simplified Handler Methods
link: eventbrokersimplifiedhandlermethods.html
- name: Direct Interaction
link: eventbrokerdirectinteraction.html
- name: Handlers
link: eventbrokerhandlers.html
- name: Matchers
link: eventbrokermatchers.html
- name: Exception Handling
link: eventbrokerexceptionhandling.html
- name: Extensions
link: eventbrokerextensions.html
- name: Logging
link: eventbrokerlogging.html
- name: Testability
link: eventbrokertestability.html
- name: Tips and Tricks
link: eventbrokertipsandtricks.html
- name: Specifications
link: eventbrokerspecifications.html
---
<h2>Tutorial</h2>
<p>
This tutorial gives you a quick look at how to use the EventBroker in simple scenarios.
Check out the rest of the documention for a full list of features.
</p>
<h3>Sample publisher</h3>
<p>
Publish an event topic:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
public class Publisher
{
[EventPublication("topic://EventBrokerSample/SimpleEvent")]
public event EventHandler SimpleEvent;
public void FireSimpleEvent()
{
SimpleEvent(this, EventArgs.Empty);
}
}
]]></script>
<p>
Register the publisher with your event broker (you have to hold an instance of the event broker somewhere in your code).
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
EventBroker eventBroker = ...;
Publisher publisher = new Publisher();
eventBroker.Register(publisher);
]]></script>
<p>
On registration of the publisher, the event broker inspects the publisher for published events
(events with the <code>EventPublication</code> attribute).
</p>
<h3>Sample subscriber</h3>
<p>
Subscribe to an event topic:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
public class Subscriber
{
[EventSubscription(
"topic://EventBrokerSample/SimpleEvent",
typeof(OnPublisher))]
public void SimpleEvent(object sender, EventArgs e)
{
// do something useful or at least funny
}
}
]]></script>
<p>
Register the subscriber with the event broker:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
EventBroker eventBroker = ...;
Subscriber subscriber = new Subscriber();
eventBRoker.Register(subscriber);
]]></script>
<p>
The event broker will inspect the subscriber on registration for subscription to event topics
(methods with the <code>EventSubscription</code> attribute).
</p>
<p>
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 <code>sender</code> and <code>EventArgs</code> the publisher used to fire its event.
</p>
<h3>Publication options</h3>
<h4>Simple</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://Simple")]
public event EventHandler SimpleEvent;
]]></script>
<h4>With custom Eventargs</h4>
<p>
Note: <code>CustomEventArgs</code> has simply to be derived from <code>EventArgs</code>.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://CustomEventArgs")]
public event EventHandler<CustomEventArguments> CustomEventArgs;
]]></script>
<h4>Publish multiple event topics with one single event</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("Event1")]
[EventPublication("Event2")]
[EventPublication("Event3")]
public event EventHandler MultiplePublicationTopics;
]]></script>
<h4>Allow only synchronous subscription handlers</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://Simple", HandlerRestriction.Synchronous)]
public event EventHandler AnEvent;
]]></script>
<h4>Allow only asynchronous subscription handlers</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://Simple", HandlerRestriction.Asynchronous)]
public event EventHandler AnEvent;
]]></script>
<h3>Subscription options</h3>
<h4>Simple</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://Simple", typeof(OnPublisher)]
public void SimpleEvent(object sender, EventArgs e) {}
]]></script>
<h4>Custom EventArgs</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://CustomEventArgs"), typeof(OnPublisher))]
public void CustomEventArgs(object sender, CustomEventArgs e) {}
]]></script>
<h4>Subscribe multiple event topics</h4>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("Event1", typeof(OnPublisher))]
[EventSubscription("Event2", typeof(OnPublisher))]
[EventSubscription("Event3", typeof(OnPublisher))]
public void MultipleSubscriptionTopics(object sender, EventArgs e) {}
]]></script>
<h4>Execute handler on background thread (asynchronous)</h4>
<p>
The event broker creates a worker thread to execute the handler method on. The publisher can immediately continue processing.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://Background", typeof(OnBackground))]
public void BackgroundThread(object sender, EventArgs e) {}
]]></script>
<h4>Execute handler on UI thread</h4>
<p>
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.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://UI", typeof(OnUserInterface))]
public void UI(object sender, EventArgs e) {}
]]></script>
<p>
Note that if you use the <code>OnUserInterface</code> 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.
</p>
<h4>Execute handler on UI thread asynchronously</h4>
<p>
The same as above, but the publisher is not blocked until the subscriber has processed the event.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://UIAsync", typeof(OnUserInterfaceAsync))]
public void UI(object sender, EventArgs e) {}
]]></script>
<h3>Simplified subscription handler signatures</h3>
<p>
If you are not interested in the sender of the event, you can leave the sender out in the handler method:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle(EventArgs e) {}
]]></script>
<p>
If you also don't need the event arguments, you can ignore them, too:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle() {}
]]></script>
<p>
And if you have a generic event argument <code>EventArgs<T></code>, you can directly define the content value of the event arguments:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://handle.me")]
public event EventHandler<EventArgs<string>> Event;
[EventSubscription("topic://handle.me", typeof(OnPublisher))]
public void Handle(string value) {}
]]></script>
<p>
These are the basics about the EventBroker. See the rest of the documentation for more options and details.
</p>