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
/
eventbrokerregistrationbyattribute.html
151 lines (136 loc) · 5.14 KB
/
eventbrokerregistrationbyattribute.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
---
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>Registration by Attribute</h2>
<p>
Registering events and event handler method with attributes is the simplest way to make events and event handler methods interact
with the event broker. However, there exist scenarios where attributes are not sufficient because only compile-time constant
values can be used in attribute definitions. For these dynamic scenarios see
<a href="eventbrokerregistrationbyregistrar.html">registration by event broker registrar</a>.
</p>
<h3>Register Events and Handler Methods</h3>
<p>
The event broker listens to events with the attribute <code>EventPublication</code> and can call methods decorated with the
<code>EventSubscription</code> attribute.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
public class MyClass
{
[EventPublication("topic://SomeTopic")]
public event EventHandler MyEvent;
[EventSubscription("topic://SomeOtherTopic", typeof(OnPublisher))]
public void HandleOtherTopic(object sender, EventArgs e)
{
// do something here
}
}
]]></script>
<p>
After registration on the event broker, fired events are routed to corresponding subscribers.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
var eventBroker = new EventBroker();
var myInstance = new MyClass();
eventBroker.Register(myInstance);
]]></script>
<p>
If you want to stop interaction with event broker, you can unregister the instance from the event broker.
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
eventBroker.Unregister(myInstance);
]]></script>
<h3>Event Topics</h3>
<p>
The string defined in the attributes is used to uniquely identify the event topic.
Subscribers get notified about events fired with the same topic.
</p>
<h3>Signature</h3>
<p>
The event broker can only listen to events with an event handler type <code>EventHandler</code> or
<code>EventHandler<TEventArgs> where TEventArgs : EventArgs</code>.
Other delegate types are not supported.
</p>
<p>
The signature of subscribers has to match the event handler type specified by the publication the subscriber listens to:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
event EventHandler Event -> void Handle(object sender, EventArgs e)
event EventHandler<MyEventArgs> -> void Handle(object sender, MyEventArgs e)
]]></script>
<h3>Handlers</h3>
<p>
You have to define how the event should be handled in the event subscription:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventSubscription("topic://Topic", typeof(OnPublisher))]
]]></script>
<p>
See documentation about <a href="eventbrokerhandlers.html">handlers</a> for a list of available handlers.
</p>
<h3>Handler Restrictions</h3>
<p>
Handler restrictions (see <a href="eventbrokerhandlers.html">Handlers</a>) can be defined in the <code>EventPublication</code>:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://Topic", HandlerRestriction.Synchronous)]
[EventPublication("topic://Topic", HandlerRestriction.Asynchronous)]
]]></script>
<h3>Matchers</h3>
<p>
Publication or subscription specific matchers (see <a href="eventbrokermatchers.html">Matchers</a>) can be defined with the attributes:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("topic://Topic", typeof(MyPublicationMatcher)]
[EventSubscription("topic://Topic", typeof(OnPublisher), typeof(MySubscriptionMatcher)]
]]></script>
<h3>Multiple Event Topics</h3>
<p>
You can fire multiple event topics or handle multiple event topics with a single event or event handler method:
</p>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[
[EventPublication("Event1")]
[EventPublication("Event2")]
[EventPublication("Event3")]
public event EventHandler MultiplePublicationTopics;
[EventSubscription("Event1", typeof(OnPublisher))]
[EventSubscription("Event2", typeof(OnPublisher))]
[EventSubscription("Event3", typeof(OnPublisher))]
public void HandleMultipleSubscriptionTopics(
object sender,
EventArgs e)
{
}
]]></script>