Skip to content

Commit 0121132

Browse files
committed
Merge branch 'order-processflow-redesign' of https://github.com/dotnet-architecture/eShopOnContainers into order-processflow-redesign
# Conflicts: # src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs
2 parents 3d05230 + e64a4fa commit 0121132

45 files changed

Lines changed: 589 additions & 268 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/BuildingBlocks/EventBus/EventBus.Tests/InMemory_SubscriptionManager_Tests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public void After_Creation_Should_Be_Empty()
1818
public void After_One_Event_Subscription_Should_Contain_The_Event()
1919
{
2020
var manager = new InMemoryEventBusSubscriptionsManager();
21-
manager.AddSubscription<TestIntegrationEvent,TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
21+
manager.AddSubscription<TestIntegrationEvent,TestIntegrationEventHandler>();
2222
Assert.True(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
2323
}
2424

2525
[Fact]
2626
public void After_All_Subscriptions_Are_Deleted_Event_Should_No_Longer_Exists()
2727
{
2828
var manager = new InMemoryEventBusSubscriptionsManager();
29-
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
29+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
3030
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
3131
Assert.False(manager.HasSubscriptionsForEvent<TestIntegrationEvent>());
3232
}
@@ -37,7 +37,7 @@ public void Deleting_Last_Subscription_Should_Raise_On_Deleted_Event()
3737
bool raised = false;
3838
var manager = new InMemoryEventBusSubscriptionsManager();
3939
manager.OnEventRemoved += (o, e) => raised = true;
40-
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
40+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
4141
manager.RemoveSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
4242
Assert.True(raised);
4343
}
@@ -46,8 +46,8 @@ public void Deleting_Last_Subscription_Should_Raise_On_Deleted_Event()
4646
public void Get_Handlers_For_Event_Should_Return_All_Handlers()
4747
{
4848
var manager = new InMemoryEventBusSubscriptionsManager();
49-
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>(() => new TestIntegrationEventHandler());
50-
manager.AddSubscription<TestIntegrationEvent, TestIntegrationOtherEventHandler>(() => new TestIntegrationOtherEventHandler());
49+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationEventHandler>();
50+
manager.AddSubscription<TestIntegrationEvent, TestIntegrationOtherEventHandler>();
5151
var handlers = manager.GetHandlersForEvent<TestIntegrationEvent>();
5252
Assert.Equal(2, handlers.Count());
5353
}

src/BuildingBlocks/EventBus/EventBus/Abstractions/IEventBus.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
55
{
66
public interface IEventBus
77
{
8-
void Subscribe<T, TH>(Func<TH> handler)
8+
void Subscribe<T, TH>()
99
where T : IntegrationEvent
1010
where TH : IIntegrationEventHandler<T>;
11-
void SubscribeDynamic<TH>(string eventName, Func<TH> handler)
11+
void SubscribeDynamic<TH>(string eventName)
1212
where TH : IDynamicIntegrationEventHandler;
1313

1414
void UnsubscribeDynamic<TH>(string eventName)

src/BuildingBlocks/EventBus/EventBus/IEventBusSubscriptionsManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public interface IEventBusSubscriptionsManager
1010
{
1111
bool IsEmpty { get; }
1212
event EventHandler<string> OnEventRemoved;
13-
void AddDynamicSubscription<TH>(string eventName, Func<TH> handler)
13+
void AddDynamicSubscription<TH>(string eventName)
1414
where TH : IDynamicIntegrationEventHandler;
1515

16-
void AddSubscription<T, TH>(Func<TH> handler)
16+
void AddSubscription<T, TH>()
1717
where T : IntegrationEvent
1818
where TH : IIntegrationEventHandler<T>;
1919

src/BuildingBlocks/EventBus/EventBus/InMemoryEventBusSubscriptionsManager.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,41 @@ public InMemoryEventBusSubscriptionsManager()
2626
public bool IsEmpty => !_handlers.Keys.Any();
2727
public void Clear() => _handlers.Clear();
2828

29-
public void AddDynamicSubscription<TH>(string eventName, Func<TH> handler)
29+
public void AddDynamicSubscription<TH>(string eventName)
3030
where TH : IDynamicIntegrationEventHandler
3131
{
32-
DoAddSubscription(handler, eventName, isDynamic: true);
32+
DoAddSubscription(typeof(TH), eventName, isDynamic: true);
3333
}
3434

35-
public void AddSubscription<T, TH>(Func<TH> handler)
35+
public void AddSubscription<T, TH>()
3636
where T : IntegrationEvent
3737
where TH : IIntegrationEventHandler<T>
3838
{
3939
var eventName = GetEventKey<T>();
40-
DoAddSubscription(handler, eventName, isDynamic: false);
40+
DoAddSubscription(typeof(TH), eventName, isDynamic: false);
4141
_eventTypes.Add(typeof(T));
4242
}
4343

44-
private void DoAddSubscription(Delegate handler, string eventName, bool isDynamic)
44+
private void DoAddSubscription(Type handlerType, string eventName, bool isDynamic)
4545
{
4646
if (!HasSubscriptionsForEvent(eventName))
4747
{
4848
_handlers.Add(eventName, new List<SubscriptionInfo>());
4949
}
50+
51+
if (_handlers[eventName].Any(s => s.HandlerType == handlerType))
52+
{
53+
throw new ArgumentException(
54+
$"Handler Type {handlerType.Name} already registered for '{eventName}'", nameof(handlerType));
55+
}
56+
5057
if (isDynamic)
5158
{
52-
_handlers[eventName].Add(SubscriptionInfo.Dynamic(handler));
59+
_handlers[eventName].Add(SubscriptionInfo.Dynamic(handlerType));
5360
}
5461
else
5562
{
56-
_handlers[eventName].Add(SubscriptionInfo.Typed(handler));
63+
_handlers[eventName].Add(SubscriptionInfo.Typed(handlerType));
5764
}
5865
}
5966

@@ -115,7 +122,7 @@ private void RaiseOnEventRemoved(string eventName)
115122
private SubscriptionInfo FindDynamicSubscriptionToRemove<TH>(string eventName)
116123
where TH : IDynamicIntegrationEventHandler
117124
{
118-
return DoFindHandlerToRemove(eventName, typeof(TH));
125+
return DoFindSubscriptionToRemove(eventName, typeof(TH));
119126
}
120127

121128

@@ -124,25 +131,18 @@ private SubscriptionInfo FindSubscriptionToRemove<T, TH>()
124131
where TH : IIntegrationEventHandler<T>
125132
{
126133
var eventName = GetEventKey<T>();
127-
return DoFindHandlerToRemove(eventName, typeof(TH));
134+
return DoFindSubscriptionToRemove(eventName, typeof(TH));
128135
}
129136

130-
private SubscriptionInfo DoFindHandlerToRemove(string eventName, Type handlerType)
137+
private SubscriptionInfo DoFindSubscriptionToRemove(string eventName, Type handlerType)
131138
{
132139
if (!HasSubscriptionsForEvent(eventName))
133140
{
134141
return null;
135142
}
136-
foreach (var subscription in _handlers[eventName])
137-
{
138-
var genericArgs = subscription.Factory.GetType().GetGenericArguments();
139-
if (genericArgs.SingleOrDefault() == handlerType)
140-
{
141-
return subscription;
142-
}
143-
}
144143

145-
return null;
144+
return _handlers[eventName].SingleOrDefault(s => s.HandlerType == handlerType);
145+
146146
}
147147

148148
public bool HasSubscriptionsForEvent<T>() where T : IntegrationEvent

src/BuildingBlocks/EventBus/EventBus/SubscriptionInfo.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ public partial class InMemoryEventBusSubscriptionsManager : IEventBusSubscriptio
77
public class SubscriptionInfo
88
{
99
public bool IsDynamic { get; }
10-
public Delegate Factory { get; }
10+
public Type HandlerType{ get; }
1111

12-
private SubscriptionInfo(bool isDynamic, Delegate factory)
12+
private SubscriptionInfo(bool isDynamic, Type handlerType)
1313
{
1414
IsDynamic = isDynamic;
15-
Factory = factory;
15+
HandlerType = handlerType;
1616
}
1717

18-
public static SubscriptionInfo Dynamic(Delegate factory)
18+
public static SubscriptionInfo Dynamic(Type handlerType)
1919
{
20-
return new SubscriptionInfo(true, factory);
20+
return new SubscriptionInfo(true, handlerType);
2121
}
22-
public static SubscriptionInfo Typed(Delegate factory)
22+
public static SubscriptionInfo Typed(Type handlerType)
2323
{
24-
return new SubscriptionInfo(false, factory);
24+
return new SubscriptionInfo(false, handlerType);
2525
}
2626
}
2727
}

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
1+
using Autofac;
2+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
23
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
34
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
45
using Microsoft.Extensions.Logging;
@@ -26,17 +27,20 @@ public class EventBusRabbitMQ : IEventBus, IDisposable
2627
private readonly IRabbitMQPersistentConnection _persistentConnection;
2728
private readonly ILogger<EventBusRabbitMQ> _logger;
2829
private readonly IEventBusSubscriptionsManager _subsManager;
29-
30+
private readonly ILifetimeScope _autofac;
31+
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
3032

3133
private IModel _consumerChannel;
3234
private string _queueName;
3335

34-
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger, IEventBusSubscriptionsManager subsManager)
36+
public EventBusRabbitMQ(IRabbitMQPersistentConnection persistentConnection, ILogger<EventBusRabbitMQ> logger,
37+
ILifetimeScope autofac, IEventBusSubscriptionsManager subsManager)
3538
{
3639
_persistentConnection = persistentConnection ?? throw new ArgumentNullException(nameof(persistentConnection));
3740
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
3841
_subsManager = subsManager ?? new InMemoryEventBusSubscriptionsManager();
3942
_consumerChannel = CreateConsumerChannel();
43+
_autofac = autofac;
4044

4145
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
4246
}
@@ -97,20 +101,20 @@ public void Publish(IntegrationEvent @event)
97101
}
98102
}
99103

100-
public void SubscribeDynamic<TH>(string eventName, Func<TH> handler)
101-
where TH: IDynamicIntegrationEventHandler
104+
public void SubscribeDynamic<TH>(string eventName)
105+
where TH : IDynamicIntegrationEventHandler
102106
{
103107
DoInternalSubscription(eventName);
104-
_subsManager.AddDynamicSubscription<TH>(eventName,handler);
108+
_subsManager.AddDynamicSubscription<TH>(eventName);
105109
}
106110

107-
public void Subscribe<T, TH>(Func<TH> handler)
111+
public void Subscribe<T, TH>()
108112
where T : IntegrationEvent
109113
where TH : IIntegrationEventHandler<T>
110114
{
111115
var eventName = _subsManager.GetEventKey<T>();
112116
DoInternalSubscription(eventName);
113-
_subsManager.AddSubscription<T, TH>(handler);
117+
_subsManager.AddSubscription<T, TH>();
114118
}
115119

116120
private void DoInternalSubscription(string eventName)
@@ -140,7 +144,7 @@ public void Unsubscribe<T, TH>()
140144
}
141145

142146
public void UnsubscribeDynamic<TH>(string eventName)
143-
where TH: IDynamicIntegrationEventHandler
147+
where TH : IDynamicIntegrationEventHandler
144148
{
145149
_subsManager.RemoveDynamicSubscription<TH>(eventName);
146150
}
@@ -195,25 +199,28 @@ private IModel CreateConsumerChannel()
195199
private async Task ProcessEvent(string eventName, string message)
196200
{
197201

202+
198203
if (_subsManager.HasSubscriptionsForEvent(eventName))
199204
{
200-
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
201-
202-
foreach (var subscription in subscriptions)
205+
using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
203206
{
204-
if (subscription.IsDynamic)
205-
{
206-
var handler = subscription.Factory.DynamicInvoke() as IDynamicIntegrationEventHandler;
207-
dynamic eventData = JObject.Parse(message);
208-
await handler.Handle(eventData);
209-
}
210-
else
207+
var subscriptions = _subsManager.GetHandlersForEvent(eventName);
208+
foreach (var subscription in subscriptions)
211209
{
212-
var eventType = _subsManager.GetEventTypeByName(eventName);
213-
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
214-
var handler = subscription.Factory.DynamicInvoke();
215-
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
216-
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
210+
if (subscription.IsDynamic)
211+
{
212+
var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler;
213+
dynamic eventData = JObject.Parse(message);
214+
await handler.Handle(eventData);
215+
}
216+
else
217+
{
218+
var eventType = _subsManager.GetEventTypeByName(eventName);
219+
var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
220+
var handler = scope.ResolveOptional(subscription.HandlerType);
221+
var concreteType = typeof(IIntegrationEventHandler<>).MakeGenericType(eventType);
222+
await (Task)concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
223+
}
217224
}
218225
}
219226
}

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Autofac" Version="4.5.0" />
1011
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
1112
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
1213
<PackageReference Include="Polly" Version="5.0.6" />

src/Services/Basket/Basket.API/Basket.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
</ItemGroup>
2323

2424
<ItemGroup>
25+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.0.0" />
2526
<PackageReference Include="System.Threading" Version="4.3.0" />
2627
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
2728
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />

src/Services/Basket/Basket.API/Controllers/BasketController.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
1010
using Basket.API.IntegrationEvents.Events;
1111
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
12+
using Basket.API.Model;
1213

1314
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
1415
{
@@ -50,20 +51,23 @@ public async Task<IActionResult> Post([FromBody]CustomerBasket value)
5051
return Ok(basket);
5152
}
5253

53-
[Route("checkouts")]
54-
[HttpPost]
55-
public async Task<IActionResult> Checkout()
54+
[Route("checkout")]
55+
[HttpPut]
56+
public async Task<IActionResult> Checkout([FromBody]BasketCheckout value)
5657
{
5758
var userId = _identitySvc.GetUserIdentity();
5859
var basket = await _repository.GetBasketAsync(userId);
59-
_eventBus.Publish(new UserCheckoutAccepted(userId, basket));
60+
var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, value.City, value.Street,
61+
value.State, value.Country, value.ZipCode, value.CardNumber, value.CardHolderName,
62+
value.CardExpiration, value.CardSecurityNumber, value.CardTypeId, value.Buyer, value.RequestId, basket);
63+
64+
_eventBus.Publish(eventMessage);
65+
6066
if (basket == null)
6167
{
6268
return BadRequest();
6369
}
6470

65-
66-
6771
return Accepted();
6872
}
6973

src/Services/Basket/Basket.API/IntegrationEvents/Events/UserCheckoutAccepted.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)