8
8
9
9
namespace ReactiveDomain . UI
10
10
{
11
+ /// <summary>
12
+ /// A <see cref="ReactiveObject"/> that implements Subscribe on the injected bus and
13
+ /// that auto-unsubscribes when disposed.
14
+ /// </summary>
11
15
public abstract class ReactiveTransientSubscriber : ReactiveObject , IDisposable
12
16
{
13
17
private readonly List < IDisposable > _subscriptions = new List < IDisposable > ( ) ;
14
18
private readonly ISubscriber _eventSubscriber ;
15
19
private readonly ICommandSubscriber _commandSubscriber ;
16
20
21
+ /// <summary>
22
+ /// Creates a <see cref="ReactiveTransientSubscriber"/> with the injected dispatcher.
23
+ /// </summary>
24
+ /// <param name="bus">The dispatcher on which to subscribe to commands.</param>
25
+ /// <exception cref="ArgumentNullException"><c>bus</c> is <c>null</c></exception>
17
26
protected ReactiveTransientSubscriber ( IDispatcher bus ) : this ( ( IBus ) bus )
18
27
{
19
28
_commandSubscriber = bus ?? throw new ArgumentNullException ( nameof ( bus ) ) ;
20
29
}
21
30
22
- protected ReactiveTransientSubscriber ( IBus bus ) : this ( ( ISubscriber ) bus ) { }
31
+ /// <summary>
32
+ /// Creates a <see cref="ReactiveTransientSubscriber"/> with the injected bus.
33
+ /// </summary>
34
+ /// <param name="bus">The bus on which to subscribe to messages.</param>
35
+ /// <exception cref="ArgumentNullException"><c>bus</c> is <c>null</c></exception>
36
+ protected ReactiveTransientSubscriber ( IBus bus ) : this ( ( ISubscriber ) bus ) { }
23
37
38
+ /// <summary>
39
+ /// Creates a <see cref="ReactiveTransientSubscriber"/> with the injected bus.
40
+ /// </summary>
41
+ /// <param name="subscriber">The <see cref="ISubscriber"/> on which to subscribe to messages.</param>
42
+ /// <exception cref="ArgumentNullException"><c>bus</c> is <c>null</c></exception>
24
43
protected ReactiveTransientSubscriber ( ISubscriber subscriber )
25
44
{
26
45
_eventSubscriber = subscriber ?? throw new ArgumentNullException ( nameof ( subscriber ) ) ;
27
46
}
28
47
48
+ /// <summary>
49
+ /// Subscribes to messages of type T on the bus. This is typically used when subscribing to events.
50
+ /// </summary>
51
+ /// <typeparam name="T">The type of messages to subscribe to.</typeparam>
52
+ /// <param name="handler">A handler for messages of type T.</param>
29
53
protected void Subscribe < T > ( IHandle < T > handler ) where T : class , IMessage
30
54
{
31
55
_subscriptions . Add ( _eventSubscriber . Subscribe < T > ( handler ) ) ;
32
56
}
33
57
58
+ /// <summary>
59
+ /// Subscribes to commands of type T on the bus.
60
+ /// </summary>
61
+ /// <typeparam name="T">The type of commands to subscribe to.</typeparam>
62
+ /// <param name="handler">A handler for commands of type T.</param>
63
+ /// <exception cref="ArgumentOutOfRangeException">The class has not been given an
64
+ /// <see cref="IDispatcher"/> on which to subscribe to commands.</exception>
34
65
protected void Subscribe < T > ( IHandleCommand < T > handler ) where T : Command
35
66
{
36
67
if ( _commandSubscriber == null ) throw new ArgumentOutOfRangeException ( nameof ( handler ) , @"TransientSubscriber not created with CommandBus to register on." ) ;
37
68
_subscriptions . Add ( _commandSubscriber . Subscribe < T > ( handler ) ) ;
38
69
}
39
70
71
+ /// <summary>
72
+ /// Unsubscribes from all subscribed message types on the bus.
73
+ /// </summary>
40
74
public void Dispose ( )
41
75
{
42
76
Dispose ( true ) ;
@@ -47,10 +81,10 @@ protected virtual void Dispose(bool disposing)
47
81
{
48
82
if ( _disposed )
49
83
return ;
50
- if ( disposing ) {
84
+ if ( disposing ) {
51
85
_subscriptions ? . ForEach ( s => s . Dispose ( ) ) ;
52
86
}
53
- _disposed = true ;
87
+ _disposed = true ;
54
88
}
55
89
}
56
90
}
0 commit comments