public async Task ExecuteAsync(HookType type, IDomainEntityContext <Invoice> context, CancellationToken cancellationToken) { var isStatusPropertyDirty = context.EditMode == EditMode.Create || context.EditMode == EditMode.Update && context.IsPropertyDirty(x => x.Status); if (isStatusPropertyDirty) { switch (context.Entity.Status) { case InvoiceStatus.Sent: await context.AddEventAsync(InvoiceSentEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); break; case InvoiceStatus.Paid: await context.AddEventAsync(InvoicePaidEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); break; case InvoiceStatus.Cancelled: await context.AddEventAsync(InvoiceCancelledEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); break; } } }
public async Task ExecuteAsync(HookType type, IDomainEntityContext <UserPreferences> context, CancellationToken cancellationToken) { switch (context.EditMode) { case EditMode.Create: case EditMode.Update: await context.AddEventAsync(UserPreferencesUpdatedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); break; case EditMode.Delete: await context.AddEventAsync(UserPreferencesDeletedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); break; } }
public Task ExecuteAsync(HookType type, IDomainEntityContext <User> context, CancellationToken cancellationToken) { switch (context.EditMode) { case EditMode.Create: context.AddEventAsync(UserCreatedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); break; case EditMode.Update: context.AddEventAsync(UserUpdatedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); break; case EditMode.Delete: context.AddEventAsync(UserDeletedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); break; } return(Task.CompletedTask); }
public async Task ExecuteAsync(HookType type, IDomainEntityContext <Invoice> context, CancellationToken cancellationToken) { if (type == HookType.BeforeCreate) { // An invoice always has an accompanying PDF document. We can create this by requesting synchronization via a domain event. context.Entity.PdfIsSynced = false; await context.AddMessageAsync(SynchronizeInvoicePdfMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } else if (type == HookType.BeforeUpdate && context.Pristine.Status == InvoiceStatus.Draft) { var invoiceToPdfModel = await _invoiceToPdfModelMapper.MapAsync(context.Entity, cancellationToken); var checksum = invoiceToPdfModel.GetChecksum(); // Compare calculated checksum against checksum when PDF was last generated if (!string.Equals(checksum, context.Entity.PdfChecksum)) { context.State.TryGet(InvoiceStateKeys.ThrowIfPdfIsNotSynced, out bool throwIfPdfIsNotSynced); if (throwIfPdfIsNotSynced) { // Prevent a possible infinite loop by not allowing synchronization when in the context of // updating the invoice directly after PDF synchronization has occured. In this scenario the // checksums should have been equal. throw new DomainException("Expected invoice PDF to have been synced"); } // Changes made to entity DO affect PDF content. Requesting synchronization. context.Entity.PdfIsSynced = false; await context.AddMessageAsync(SynchronizeInvoicePdfMessage.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } else { // Changes made to entity DIDNT affect PDF content. Not requesting synchronization. context.Entity.PdfIsSynced = true; } } if (context.Entity.PdfIsSynced && context.IsPropertyDirty(x => x.PdfIsSynced)) { await context.AddEventAsync(InvoicePdfSynchronizedEvent.Create(_correlationIdProvider.Id, context.Entity.Id), cancellationToken); } }
public async Task ExecuteAsync(HookType type, IDomainEntityContext <FeatureFlagSettings> context, CancellationToken cancellationToken) { await context.AddEventAsync(FeatureFlagSettingsUpdatedEvent.Create(_correlationIdProvider.Id, context.Entity.Id, _currentUser.Id), cancellationToken); }