Skip to content

Commit a09d7fd

Browse files
committed
EventBus refactor.
Instead to register EventHandlers we register Func<EventHandlers> which solves scope problems (having transient/scoped objects owned by singletons)
1 parent 22cc8da commit a09d7fd

13 files changed

Lines changed: 414 additions & 72 deletions

File tree

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Health
7474
EndProject
7575
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.HealthChecks.SqlServer", "src\BuildingBlocks\HealthChecks\src\Microsoft.Extensions.HealthChecks.SqlServer\Microsoft.Extensions.HealthChecks.SqlServer.csproj", "{4BD76717-3102-4969-8C2C-BAAA3F0263B6}"
7676
EndProject
77+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventBus.Tests", "src\BuildingBlocks\EventBus\EventBus.Tests\EventBus.Tests.csproj", "{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}"
78+
EndProject
7779
Global
7880
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7981
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -952,6 +954,54 @@ Global
952954
{4BD76717-3102-4969-8C2C-BAAA3F0263B6}.Release|x64.Build.0 = Release|Any CPU
953955
{4BD76717-3102-4969-8C2C-BAAA3F0263B6}.Release|x86.ActiveCfg = Release|Any CPU
954956
{4BD76717-3102-4969-8C2C-BAAA3F0263B6}.Release|x86.Build.0 = Release|Any CPU
957+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
958+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
959+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
960+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
961+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
962+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
963+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
964+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
965+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
966+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
967+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
968+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
969+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
970+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|Any CPU.Build.0 = Debug|Any CPU
971+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|ARM.ActiveCfg = Debug|Any CPU
972+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|ARM.Build.0 = Debug|Any CPU
973+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
974+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|iPhone.Build.0 = Debug|Any CPU
975+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
976+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
977+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|x64.ActiveCfg = Debug|Any CPU
978+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|x64.Build.0 = Debug|Any CPU
979+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|x86.ActiveCfg = Debug|Any CPU
980+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.AppStore|x86.Build.0 = Debug|Any CPU
981+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
982+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
983+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|ARM.ActiveCfg = Debug|Any CPU
984+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|ARM.Build.0 = Debug|Any CPU
985+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|iPhone.ActiveCfg = Debug|Any CPU
986+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|iPhone.Build.0 = Debug|Any CPU
987+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
988+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
989+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|x64.ActiveCfg = Debug|Any CPU
990+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|x64.Build.0 = Debug|Any CPU
991+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|x86.ActiveCfg = Debug|Any CPU
992+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Debug|x86.Build.0 = Debug|Any CPU
993+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
994+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|Any CPU.Build.0 = Release|Any CPU
995+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|ARM.ActiveCfg = Release|Any CPU
996+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|ARM.Build.0 = Release|Any CPU
997+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|iPhone.ActiveCfg = Release|Any CPU
998+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|iPhone.Build.0 = Release|Any CPU
999+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1000+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1001+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|x64.ActiveCfg = Release|Any CPU
1002+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|x64.Build.0 = Release|Any CPU
1003+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|x86.ActiveCfg = Release|Any CPU
1004+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D}.Release|x86.Build.0 = Release|Any CPU
9551005
EndGlobalSection
9561006
GlobalSection(SolutionProperties) = preSolution
9571007
HideSolutionNode = FALSE
@@ -987,5 +1037,6 @@ Global
9871037
{D1C47FF1-91F1-4CAF-9ABB-AD642B821502} = {FBF43D93-F2E7-4FF8-B4AB-186895949B88}
9881038
{22A0F9C1-2D4A-4107-95B7-8459E6688BC5} = {A81ECBC2-6B00-4DCD-8388-469174033379}
9891039
{4BD76717-3102-4969-8C2C-BAAA3F0263B6} = {A81ECBC2-6B00-4DCD-8388-469174033379}
1040+
{89D80DF1-32E1-4AAF-970F-DA0AA6881F9D} = {807BB76E-B2BB-47A2-A57B-3D1B20FF5E7F}
9901041
EndGlobalSection
9911042
EndGlobal
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
9+
<PackageReference Include="xunit" Version="2.2.0" />
10+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
15+
<ProjectReference Include="..\EventBus\EventBus.csproj" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
2+
using System;
3+
using System.Linq;
4+
using Xunit;
5+
6+
namespace EventBus.Tests
7+
{
8+
public class InMemory_SubscriptionManager_Tests
9+
{
10+
[Fact]
11+
public void After_Creation_Should_Be_Empty()
12+
{
13+
var manager = new InMemoryEventBusSubscriptionsManager();
14+
Assert.True(manager.IsEmpty);
15+
}
16+
17+
[Fact]
18+
public void After_One_Event_Subscription_Should_Contain_The_Event()
19+
{
20+
var manager = new InMemoryEventBusSubscriptionsManager();
21+
manager.AddSubscription<TestIntegrationEvent,TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
22+
Assert.True(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
23+
}
24+
25+
[Fact]
26+
public void After_All_Subscriptions_Are_Deleted_Event_Should_No_Longer_Exists()
27+
{
28+
var manager = new InMemoryEventBusSubscriptionsManager();
29+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
30+
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
31+
Assert.False(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
32+
}
33+
34+
[Fact]
35+
public void Deleting_Last_Subscription_Should_Raise_On_Deleted_Event()
36+
{
37+
bool raised = false;
38+
var manager = new InMemoryEventBusSubscriptionsManager();
39+
manager.OnEventRemoved += (o, e) => raised = true;
40+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
41+
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
42+
Assert.True(raised);
43+
}
44+
45+
[Fact]
46+
public void Get_Handlers_For_Event_Should_Return_All_Handlers()
47+
{
48+
var manager = new InMemoryEventBusSubscriptionsManager();
49+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
50+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationOtherEventHandler>(() => new TestIntegrationOtherEventHandler());
51+
var handlers = manager.GetHandlersForEvent<TestIntegrationEvent>();
52+
Assert.Equal(2, handlers.Count());
53+
}
54+
55+
}
56+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace EventBus.Tests
7+
{
8+
public class TestIntegrationEvent : IntegrationEvent
9+
{
10+
}
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace EventBus.Tests
8+
{
9+
public class TestIntegrationOtherEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
10+
{
11+
public bool Handled { get; private set; }
12+
13+
public TestIntegrationOtherEventHandler()
14+
{
15+
Handled = false;
16+
}
17+
18+
public async Task Handle(TestIntegrationEvent @event)
19+
{
20+
Handled = true;
21+
}
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace EventBus.Tests
8+
{
9+
public class TestIntegrationEventHandler : IIntegrationEventHandler<TestIntegrationEvent>
10+
{
11+
public bool Handled { get; private set; }
12+
13+
public TestIntegrationEventHandler()
14+
{
15+
Handled = false;
16+
}
17+
18+
public async Task Handle(TestIntegrationEvent @event)
19+
{
20+
Handled = true;
21+
}
22+
}
23+
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2+
using System;
23

34
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
45
{
56
public interface IEventBus
67
{
7-
void Subscribe<T>(IIntegrationEventHandler<T> handler) where T: IntegrationEvent;
8-
void Unsubscribe<T>(IIntegrationEventHandler<T> handler) where T : IntegrationEvent;
8+
void Subscribe<T, TH>(Func<TH> handler)
9+
where T : IntegrationEvent
10+
where TH : IIntegrationEventHandler<T>;
11+
void Unsubscribe<T, TH>()
12+
where TH : IIntegrationEventHandler<T>
13+
where T : IntegrationEvent;
14+
915
void Publish(IntegrationEvent @event);
1016
}
1117
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
2+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
7+
{
8+
public interface IEventBusSubscriptionsManager
9+
{
10+
bool IsEmpty { get; }
11+
event EventHandler<string> OnEventRemoved;
12+
void AddSubscription<T, TH>(Func<TH> handler)
13+
where T : IntegrationEvent
14+
where TH : IIntegrationEventHandler<T>;
15+
16+
void RemoveSubscription<T, TH>()
17+
where TH : IIntegrationEventHandler<T>
18+
where T : IntegrationEvent;
19+
bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent;
20+
bool HasSubscriptionsForEvent(string eventName);
21+
Type GetEventTypeByName(string eventName);
22+
void Clear();
23+
IEnumerable<Delegate> GetHandlersForEvent<T>() where T : IntegrationEvent;
24+
IEnumerable<Delegate> GetHandlersForEvent(string eventName);
25+
}
26+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
2+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Text;
8+
9+
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus
10+
{
11+
public class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptionsManager
12+
{
13+
private readonly Dictionary<string, List<Delegate>> _handlers;
14+
private readonly List<Type> _eventTypes;
15+
16+
public event EventHandler<string> OnEventRemoved;
17+
18+
public InMemoryEventBusSubscriptionsManager()
19+
{
20+
_handlers = new Dictionary<string, List<Delegate>>();
21+
_eventTypes = new List<Type>();
22+
}
23+
24+
public bool IsEmpty => !_handlers.Keys.Any();
25+
public void Clear() => _handlers.Clear();
26+
27+
public void AddSubscription<T, TH>(Func<TH> handler)
28+
where T : IntegrationEvent
29+
where TH : IIntegrationEventHandler<T>
30+
{
31+
var key = GetEventKey<T>();
32+
if (!HasSubscriptionsForEvent<T>())
33+
{
34+
_handlers.Add(key, new List<Delegate>());
35+
}
36+
_handlers[key].Add(handler);
37+
_eventTypes.Add(typeof(T));
38+
}
39+
40+
public void RemoveSubscription<T, TH>()
41+
where TH : IIntegrationEventHandler<T>
42+
where T : IntegrationEvent
43+
{
44+
var handlerToRemove = FindHandlerToRemove<T, TH>();
45+
if (handlerToRemove != null)
46+
{
47+
var key = GetEventKey<T>();
48+
_handlers[key].Remove(handlerToRemove);
49+
if (!_handlers[key].Any())
50+
{
51+
_handlers.Remove(key);
52+
var eventType = _eventTypes.SingleOrDefault(e => e.Name == key);
53+
if (eventType != null)
54+
{
55+
_eventTypes.Remove(eventType);
56+
RaiseOnEventRemoved(eventType.Name);
57+
}
58+
}
59+
60+
}
61+
}
62+
63+
public IEnumerable<Delegate> GetHandlersForEvent<T>() where T : IntegrationEvent
64+
{
65+
var key = GetEventKey<T>();
66+
return GetHandlersForEvent(key);
67+
}
68+
public IEnumerable<Delegate> GetHandlersForEvent(string eventName) => _handlers[eventName];
69+
70+
private void RaiseOnEventRemoved(string eventName)
71+
{
72+
var handler = OnEventRemoved;
73+
if (handler != null)
74+
{
75+
OnEventRemoved(this, eventName);
76+
}
77+
}
78+
79+
private Delegate FindHandlerToRemove<T, TH>()
80+
where T : IntegrationEvent
81+
where TH : IIntegrationEventHandler<T>
82+
{
83+
if (!HasSubscriptionsForEvent<T>())
84+
{
85+
return null;
86+
}
87+
88+
var key = GetEventKey<T>();
89+
foreach (var func in _handlers[key])
90+
{
91+
var genericArgs = func.GetType().GetGenericArguments();
92+
if (genericArgs.SingleOrDefault() == typeof(TH))
93+
{
94+
return func;
95+
}
96+
}
97+
98+
return null;
99+
}
100+
101+
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent
102+
{
103+
var key = GetEventKey<T>();
104+
return HasSubscriptionsForEvent(key);
105+
}
106+
public bool HasSubscriptionsForEvent(string eventName) => _handlers.ContainsKey(eventName);
107+
108+
public Type GetEventTypeByName(string eventName) => _eventTypes.Single(t => t.Name == eventName);
109+
110+
private string GetEventKey<T>()
111+
{
112+
return typeof(T).Name;
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)