Skip to content

Commit f3099a8

Browse files
committed
Update event aggregator pattern examples
1 parent 386b87c commit f3099a8

File tree

8 files changed

+15
-13
lines changed

8 files changed

+15
-13
lines changed

src/AdditionalPatterns/EventAggregator/StoreManagement/Components/EventAggregator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public IDisposable Subscribe<T>(ICustomObserver<T> newObserver)
6868
{
6969
if (!_typedObservers.TryGetValue(typeof(T), out List<IObserver<IEvent>> observers))
7070
{
71-
observers = _typedObservers[typeof(T)] = new List<IObserver<IEvent>>();
71+
observers = new List<IObserver<IEvent>>();
72+
_typedObservers[typeof(T)] = observers;
7273
}
7374

7475
return SubscribeAndSendEvents(observers, newObserver, _events.Where(evt => evt is T));

src/AdditionalPatterns/EventAggregator/StoreManagement/Components/Subscribers/BusinessTrackingSubscriber.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@ namespace StoreManagement.Components.Subscribers;
77
/// </summary>
88
public class BusinessTrackingSubscriber : ICustomObserver<IEvent>
99
{
10-
private readonly IEventAggregator _eventAggregator;
1110
private readonly IDisposable _unsubscriber;
1211

1312
public BusinessTrackingSubscriber(IEventAggregator eventAggregator)
1413
{
15-
_eventAggregator = eventAggregator;
16-
_unsubscriber = _eventAggregator.Subscribe(this);
14+
_unsubscriber = eventAggregator.Subscribe(this);
1715
}
1816

1917
public void OnNext(IEvent @event)

src/AdditionalPatterns/EventAggregator/StoreManagement/Components/Subscribers/OrderSubscriber.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ namespace StoreManagement.Components.Subscribers;
88
/// </summary>
99
public class OrderSubscriber : ICustomObserver<OrderCreatedEvent>
1010
{
11-
private readonly IEventAggregator _eventAggregator;
1211
private readonly IDisposable _unsubscriber;
1312

1413
public OrderSubscriber(IEventAggregator eventAggregator)
1514
{
16-
_eventAggregator = eventAggregator;
17-
_unsubscriber = _eventAggregator.Subscribe(this);
15+
_unsubscriber = eventAggregator.Subscribe(this);
1816
}
1917

2018
public void OnNext(IEvent @event) =>

src/AdditionalPatterns/EventAggregator/StoreManagement/Components/Unsubscriber.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public void Dispose()
2121
GC.SuppressFinalize(this);
2222
}
2323

24-
// The bulk of the clean-up code is implemented in Dispose(bool)
2524
protected virtual void Dispose(bool disposing)
2625
{
2726
if (_isDisposed)

src/AdditionalPatterns/EventAggregator/StoreManagement/Contracts/IEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
public interface IEvent
44
{
5-
public string Description { get; set; }
5+
public string Description { get; }
66
}

src/AdditionalPatterns/EventAggregator/StoreManagement/Events/OrderCreatedEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public OrderCreatedEvent(string description)
99
Description = description;
1010
}
1111

12-
public string Description { get; set; }
13-
}
12+
public string Description { get; }
13+
}

src/AdditionalPatterns/EventAggregator/StoreManagement/Events/WarehouseReceivedNewSuppliesEvent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ public WarehouseReceivedNewSuppliesEvent(string description)
99
Description = description;
1010
}
1111

12-
public string Description { get; set; }
13-
}
12+
public string Description { get; }
13+
}

src/AdditionalPatterns/EventAggregator/StoreManagement/Executor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ public override void Execute()
3333

3434
Console.WriteLine("\nPreparing order 3...");
3535
orderPublisher.Publish("Order 3");
36+
37+
businessTrackingSubscriber.Unsubscribe();
38+
39+
// This event won't be handled, because there are no subscribers anymore.
40+
Console.WriteLine("\nPreparing order 4...");
41+
orderPublisher.Publish("Order 4");
3642
}
3743
}

0 commit comments

Comments
 (0)