This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
/
scopingeventbrokerunitofwork.html
94 lines (87 loc) · 3.55 KB
/
scopingeventbrokerunitofwork.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
---
layout: documentation
title: Scoping Event Broker
teaser: Appccelerate your asynchronous events by scoping it
navigation:
- name: Overview
link: scopingeventbroker.html
- name: Tutorial
link: scopingeventbrokertutorial.html
- name: Unit Of Work
link: scopingeventbrokerunitofwork.html
- name: Transaction Scope
link: scopingeventbrokertransactionscope.html
- name: Restrictions
link: scopingeventbrokerrestrictions.html
---
<h2>Unit of Work</h2>
The scoping event broker comes with its own unit of work implementation which can be used directly in your code or hooked into third party unit of works (i.ex. the NHibernate ISession implementation). There are two important concepts which need to be understood to work properly with the scoping event broker, namely:
<ul>
<li>The scope context</li>
<li>The scope</li>
</ul>
Let us explore what they are and what their purpose is.
<h3>Scope context</h3>
The scope context is the context which allows to create scopes. The scope context is created by the EventScopingStandardFactory. You don't need to pass around the EventScopingStandardFactory in order to be able to do scoping of events. Only the scope context is required.
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[IEventScopeContext scopeContext = scopingStandardFactory.CreateScopeContext();
var component = new Publisher(scopeContext);
]]>
</script>
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[public class Publisher {
public Publisher(IEventScopeContext context) {
this.context = context;
}
[EventPublication("topic://SomeEvent", HandlerRestriction.Asynchronous]
public event EventHandler SomeEvent = delegate { };
public void Publish() {
// Use the context to acquire the scope when scoping is needed.
using(IEventScope scope = this.context.Acquire())
{
// This event is only fired when the scope is released
this.SomeEvent(this, EventArgs.Empty);
// Do other operations which might throw an exception
ForExampleDatabaseAccess();
// Release the scope
scope.Release();
}
}
}
]]>
</script>
<h3>Scope</h3>
The scope allows to safeguard several event invocations into one scope. Only when the scope is released properly the events will be fired while releasing the scope. The scope takes care that the events are fired in the order which they were scheduled for invocation. The scope is an IDisposable and should be used together with an using block. Below you see the different operation modes of a scope.
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[public void Publish() {
this.SomeEvent(this, EventArgs.Empty);
}
]]>
</script>
The event will be fired.
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[public void Publish() {
using (IEventScope scope = scopeContext.Acquire())
{
this.SomeEvent(this, EventArgs.Empty);
scope.Cancel();
}
}
]]>
</script>
The actual event will never be fired in this case because the scope is cancelled.
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[public void Publish() {
using (scopeContext.Acquire())
{
this.SomeEvent(this, EventArgs.Empty);
}
}
]]>
</script>
The actual event will never be fired in this case because the scope is disposed without being released.
<script type="syntaxhighlighter" class="brush: csharp"><![CDATA[public void Publish() {
using (IEventScope scope = scopeContext.Acquire())
{
this.SomeEvent(this, EventArgs.Empty);
scope.Release();
}
}
]]>
</script>
The actual event type will be fired as soon as the scope is released.