public void Handle(ItemCreatedEvent <TItem> applicationEvent) { if (!_items.TryAdd(applicationEvent.Item.Id, applicationEvent.Item)) { throw new InvalidOperationException("Item operation with the specified id already added."); } }
/// <summary> /// Handles all types of command asynchronously. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> /// <exception cref="ArgumentNullException">If command is null</exception> /// <exception cref="ArgumentOutOfRangeException">If command has unsupported type</exception> /// <exception cref="Exception">In case of more generic exception</exception> public async Task <BaseItemEvent> HandleAsync(BaseItemCommand command) { try { if (command == null) { throw new ArgumentNullException(nameof(command)); } BaseItemEvent evt; switch (command) { case CreateItemCommand create: evt = new ItemCreatedEvent(create.Item.Id, create.Item.Name, command.UserName, Guid.NewGuid(), DateTime.UtcNow); break; case DeleteItemCommand delete: evt = new ItemDeletedEvent(delete.ItemId, delete.UserName, Guid.NewGuid(), DateTime.UtcNow); break; case UpdateItemCommand update: evt = new ItemUpdatedEvent(update.Item.Id, update.Item.Name, update.UserName, Guid.NewGuid(), DateTime.UtcNow); break; default: throw new ArgumentOutOfRangeException(nameof(command)); } var applicationResult = await this.root.ApplyEventAsync(evt); switch (applicationResult) { case SuccessAggregateApplicationResult success: await this.root.CommitAsync(); await this.publisher.PublishAsync(evt); return(evt); case ItemNotFoundApplicationResult notFound: throw new ItemNotFoundException(); case ItemAlreadyExistsApplicationResult itemExists: throw new ItemAlreadyExistsException(itemExists.Message); case FailedAggregateApplicationResult failed: throw new Exception(failed.Error); default: throw new ArgumentOutOfRangeException(nameof(applicationResult)); } } catch (Exception e) { await this.root.RollbackAsync(); this.logger.LogError(e, $"Failed to process command {command.GetType().Name}"); throw; } }
private void Apply(ItemCreatedEvent itemCreatedEvent, bool isNew = false) { base.AddEvent(itemCreatedEvent, isNew); Code = itemCreatedEvent.Code; Price = itemCreatedEvent.Price; ItemTypeId = itemCreatedEvent.ItemTypeId; PercentOff = Percent.Zero; }
public void Handle(ItemCreatedEvent <LifetimeScope> applicationEvent) { if (applicationEvent.Item.IsRootScope) { return; } _activeScopes.Add(applicationEvent.Item, DateTime.Now); }
public void Handle(ItemCreatedEvent <ResolveOperation> applicationEvent) { if (IsPaused) { return; } if (applicationEvent.Item.Parent == null) { var vm = new ResolveOperationEventViewModel(applicationEvent.Item.Id, _navigator); _dispacher.Foreground(() => _events.Insert(0, vm)); } }
public Item( Guid id, string code, decimal price, Guid itemTypeId) : base(id) { var itemCreatedEvent = new ItemCreatedEvent { Id = id, Version = CurrentVersion + 1, Code = code, Price = price, ItemTypeId = itemTypeId }; Apply(itemCreatedEvent, isNew: true); }
public void Handle(ItemCreatedEvent <RegistrationSource> applicationEvent) { var vm = CreateViewModel(applicationEvent.Item); _dispatcher.Foreground(() => _components.Add(vm)); }
public void Handle(ItemCreatedEvent <ResolveOperation> applicationEvent) { Enqueue(applicationEvent); }