Skip to content

Commit 393b47f

Browse files
CatalogIntegrationEventService Refactored
1 parent 3c909ff commit 393b47f

3 files changed

Lines changed: 12 additions & 9 deletions

File tree

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,16 @@ public async Task<IActionResult> UpdateProduct([FromBody]CatalogItem productToUp
152152
catalogItem = productToUpdate;
153153
_catalogContext.CatalogItems.Update(catalogItem);
154154

155-
if (raiseProductPriceChangedEvent) // Save and publish event if price has changed
156-
{
155+
if (raiseProductPriceChangedEvent) // Save and publish integration event if price has changed
156+
{
157+
//Create Integration Event to be published through the Event Bus
157158
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
159+
158160
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
159-
await _catalogIntegrationEventService.SaveEventAsync(priceChangedEvent);
160-
// Publish to Event Bus only if product price changed
161-
await _catalogIntegrationEventService.PublishAsync(priceChangedEvent);
161+
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
162+
163+
// Publish through the Event Bus and mark the saved event as published
164+
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
162165
}
163166
else // Save updated product
164167
{

src/Services/Catalog/Catalog.API/IntegrationEvents/CatalogIntegrationEventService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public CatalogIntegrationEventService(IEventBus eventBus, CatalogContext catalog
2727
_eventLogService = _integrationEventLogServiceFactory(_catalogContext.Database.GetDbConnection());
2828
}
2929

30-
public async Task PublishAsync(IntegrationEvent evt)
30+
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
3131
{
3232
_eventBus.Publish(evt);
3333
await _eventLogService.MarkEventAsPublishedAsync(evt);
3434
}
3535

36-
public async Task SaveEventAsync(IntegrationEvent evt)
36+
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
3737
{
3838
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
3939
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency

src/Services/Catalog/Catalog.API/IntegrationEvents/ICatalogIntegrationEventService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Catalog.API.IntegrationEvents
88
{
99
public interface ICatalogIntegrationEventService
1010
{
11-
Task SaveEventAsync(IntegrationEvent evt);
12-
Task PublishAsync(IntegrationEvent evt);
11+
Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt);
12+
Task PublishThroughEventBusAsync(IntegrationEvent evt);
1313
}
1414
}

0 commit comments

Comments
 (0)