protected bool IsRepeatedCommand(ISellableInventoryItemCommand command, IEventStoreAggregateId eventStoreAggregateId, ISellableInventoryItemState state) { bool repeated = false; if (((ISellableInventoryItemStateProperties)state).Version == command.AggregateVersion + 1) { if (state.CommandId == command.CommandId) { repeated = true; } } return(repeated); }
protected virtual void Update(ISellableInventoryItemCommand c, Action <ISellableInventoryItemAggregate> action) { var aggregateId = c.AggregateId; var state = StateRepository.Get(aggregateId, false); var aggregate = GetSellableInventoryItemAggregate(state); var eventStoreAggregateId = ToEventStoreAggregateId(aggregateId); var repeated = IsRepeatedCommand(c, eventStoreAggregateId, state); if (repeated) { return; } aggregate.ThrowOnInvalidStateTransition(c); action(aggregate); Persist(eventStoreAggregateId, aggregate, state); }
protected void ThrowOnInconsistentCommands(ISellableInventoryItemCommand command, ISellableInventoryItemEntryCommand innerCommand) { var properties = command as ICreateOrMergePatchOrDeleteSellableInventoryItem; var innerProperties = innerCommand as ICreateOrMergePatchOrRemoveSellableInventoryItemEntry; if (properties == null || innerProperties == null) { return; } if (innerProperties.SellableInventoryItemId == default(InventoryItemId)) { innerProperties.SellableInventoryItemId = properties.SellableInventoryItemId; } else { var outerSellableInventoryItemIdName = "SellableInventoryItemId"; var outerSellableInventoryItemIdValue = properties.SellableInventoryItemId; var innerSellableInventoryItemIdName = "SellableInventoryItemId"; var innerSellableInventoryItemIdValue = innerProperties.SellableInventoryItemId; ThrowOnInconsistentIds(innerProperties, innerSellableInventoryItemIdName, innerSellableInventoryItemIdValue, outerSellableInventoryItemIdName, outerSellableInventoryItemIdValue); } }// END ThrowOnInconsistentCommands /////////////////////
private static bool IsCommandCreate(ISellableInventoryItemCommand c) { return(c.Version == SellableInventoryItemState.VersionZero); }
protected virtual ISellableInventoryItemEntryStateCreated MapCreate(ICreateSellableInventoryItemEntry c, ISellableInventoryItemCommand outerCommand, long version, ISellableInventoryItemState outerState) { c.RequesterId = outerCommand.RequesterId; var stateEventId = new SellableInventoryItemEntryEventId(c.SellableInventoryItemId, c.EntrySeqId, version); ISellableInventoryItemEntryStateCreated e = NewSellableInventoryItemEntryStateCreated(stateEventId); var s = outerState.Entries.Get(c.EntrySeqId, true); e.SellableQuantity = c.SellableQuantity; e.SourceEventId = c.SourceEventId; e.CreatedBy = (string)c.RequesterId; e.CreatedAt = ApplicationContext.Current.TimestampService.Now <DateTime>(); return(e); }// END Map(ICreate... ////////////////////////////
}// END ThrowOnInconsistentCommands ///////////////////// protected virtual ISellableInventoryItemEntryEvent Map(ISellableInventoryItemEntryCommand c, ISellableInventoryItemCommand outerCommand, long version, ISellableInventoryItemState outerState) { var create = (c.CommandType == CommandType.Create) ? (c as ICreateSellableInventoryItemEntry) : null; if (create != null) { return(MapCreate(create, outerCommand, version, outerState)); } throw new NotSupportedException(); }