CatalogIntegrationEventService Refactored

This commit is contained in:
Cesar De la Torre 2017-04-04 17:01:58 -07:00
parent 3c909ff392
commit 393b47fa93
3 changed files with 12 additions and 9 deletions

View File

@ -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
{ {

View File

@ -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

View File

@ -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);
} }
} }