public async Task <WidgetDto> CreateWidget(WidgetDto dto) { var entity = new Domain.Widget() { Text = dto.Text, Width = dto.Width, Height = dto.Height }; // user initiated transaction with retry strategy set needs to execute in new strategy var strategy = db.Database.CreateExecutionStrategy(); await strategy.ExecuteAsync(async() => { // need a transaction and 2 savechanges so that I have the id for the widget in the event using (var tx = await db.Database.BeginTransactionAsync().ConfigureAwait(false)) { try { db.Widgets.Add(entity); await db.SaveChangesAsync().ConfigureAwait(false); var @event = new WidgetStageChangedEvent() { WidgetId = entity.WidgetId, Text = entity.Text, Width = entity.Width, Height = entity.Height, Timestamp = DateTime.UtcNow }; await publisher.PublishAsync(@event).ConfigureAwait(false); await db.SaveChangesAsync().ConfigureAwait(false); await tx.CommitAsync().ConfigureAwait(false); } catch (Exception ex) { logger.LogError(ex, "unhandled exception"); await tx.RollbackAsync().ConfigureAwait(false); throw; } } }); return(ToWidgetDto(entity)); }
public async Task Handle(AddWidget request, CancellationToken cancellationToken) { var widget = new Domain.Widget { Name = request.Name }; await this.DataService.Insert(widget); }
private WidgetDto ToWidgetDto(Domain.Widget entity) { return(new WidgetDto() { WidgetId = entity.WidgetId, Text = entity.Text, Width = entity.Width, Height = entity.Height }); }