CatalogIntegrationEventService Refactored
This commit is contained in:
parent
3c909ff392
commit
393b47fa93
@ -152,13 +152,16 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
|
|||||||
catalogItem = productToUpdate;
|
catalogItem = productToUpdate;
|
||||||
_catalogContext.CatalogItems.Update(catalogItem);
|
_catalogContext.CatalogItems.Update(catalogItem);
|
||||||
|
|
||||||
if (raiseProductPriceChangedEvent) // Save and publish event if price has changed
|
if (raiseProductPriceChangedEvent) // Save and publish integration event if price has changed
|
||||||
{
|
{
|
||||||
|
//Create Integration Event to be published through the Event Bus
|
||||||
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
|
var priceChangedEvent = new ProductPriceChangedIntegrationEvent(catalogItem.Id, productToUpdate.Price, oldPrice);
|
||||||
|
|
||||||
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
|
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
|
||||||
await _catalogIntegrationEventService.SaveEventAsync(priceChangedEvent);
|
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(priceChangedEvent);
|
||||||
// Publish to Event Bus only if product price changed
|
|
||||||
await _catalogIntegrationEventService.PublishAsync(priceChangedEvent);
|
// Publish through the Event Bus and mark the saved event as published
|
||||||
|
await _catalogIntegrationEventService.PublishThroughEventBusAsync(priceChangedEvent);
|
||||||
}
|
}
|
||||||
else // Save updated product
|
else // Save updated product
|
||||||
{
|
{
|
||||||
|
@ -27,13 +27,13 @@ namespace Catalog.API.IntegrationEvents
|
|||||||
_eventLogService = _integrationEventLogServiceFactory(_catalogContext.Database.GetDbConnection());
|
_eventLogService = _integrationEventLogServiceFactory(_catalogContext.Database.GetDbConnection());
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task PublishAsync(IntegrationEvent evt)
|
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
_eventBus.Publish(evt);
|
_eventBus.Publish(evt);
|
||||||
await _eventLogService.MarkEventAsPublishedAsync(evt);
|
await _eventLogService.MarkEventAsPublishedAsync(evt);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveEventAsync(IntegrationEvent evt)
|
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
||||||
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||||
|
@ -8,7 +8,7 @@ namespace Catalog.API.IntegrationEvents
|
|||||||
{
|
{
|
||||||
public interface ICatalogIntegrationEventService
|
public interface ICatalogIntegrationEventService
|
||||||
{
|
{
|
||||||
Task SaveEventAsync(IntegrationEvent evt);
|
Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt);
|
||||||
Task PublishAsync(IntegrationEvent evt);
|
Task PublishThroughEventBusAsync(IntegrationEvent evt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user