protected bool IsRepeatedCommand(IPhysicalInventoryCommand command, IEventStoreAggregateId eventStoreAggregateId, IPhysicalInventoryState state) { bool repeated = false; if (((IPhysicalInventoryStateProperties)state).Version > command.AggregateVersion) { var lastEvent = EventStore.GetEvent(typeof(IPhysicalInventoryEvent), eventStoreAggregateId, command.AggregateVersion); if (lastEvent != null && lastEvent.CommandId == command.CommandId) { repeated = true; } } return(repeated); }
protected virtual void Update(IPhysicalInventoryCommand c, Action <IPhysicalInventoryAggregate> action) { var aggregateId = c.AggregateId; var state = StateRepository.Get(aggregateId, false); var aggregate = GetPhysicalInventoryAggregate(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(IPhysicalInventoryCommand command, IPhysicalInventoryLineCommand innerCommand) { var properties = command as ICreateOrMergePatchOrDeletePhysicalInventory; var innerProperties = innerCommand as ICreateOrMergePatchOrRemovePhysicalInventoryLine; if (properties == null || innerProperties == null) { return; } if (innerProperties.PhysicalInventoryDocumentNumber == default(string)) { innerProperties.PhysicalInventoryDocumentNumber = properties.DocumentNumber; } else { var outerDocumentNumberName = "DocumentNumber"; var outerDocumentNumberValue = properties.DocumentNumber; var innerPhysicalInventoryDocumentNumberName = "PhysicalInventoryDocumentNumber"; var innerPhysicalInventoryDocumentNumberValue = innerProperties.PhysicalInventoryDocumentNumber; ThrowOnInconsistentIds(innerProperties, innerPhysicalInventoryDocumentNumberName, innerPhysicalInventoryDocumentNumberValue, outerDocumentNumberName, outerDocumentNumberValue); } }// END ThrowOnInconsistentCommands /////////////////////
private static bool IsCommandCreate(IPhysicalInventoryCommand c) { return(c.Version == PhysicalInventoryState.VersionZero); }
}// END Map(IMergePatch... //////////////////////////// protected virtual IPhysicalInventoryLineStateRemoved MapRemove(IRemovePhysicalInventoryLine c, IPhysicalInventoryCommand outerCommand, long version) { c.RequesterId = outerCommand.RequesterId; var stateEventId = new PhysicalInventoryLineEventId(c.PhysicalInventoryDocumentNumber, c.InventoryItemId, version); IPhysicalInventoryLineStateRemoved e = NewPhysicalInventoryLineStateRemoved(stateEventId); e.CreatedBy = (string)c.RequesterId; e.CreatedAt = ApplicationContext.Current.TimestampService.Now <DateTime>(); return(e); }// END Map(IRemove... ////////////////////////////
}// END Map(ICreate... //////////////////////////// protected virtual IPhysicalInventoryLineStateMergePatched MapMergePatch(IMergePatchPhysicalInventoryLine c, IPhysicalInventoryCommand outerCommand, long version, IPhysicalInventoryState outerState) { c.RequesterId = outerCommand.RequesterId; var stateEventId = new PhysicalInventoryLineEventId(c.PhysicalInventoryDocumentNumber, c.InventoryItemId, version); IPhysicalInventoryLineStateMergePatched e = NewPhysicalInventoryLineStateMergePatched(stateEventId); var s = outerState.PhysicalInventoryLines.Get(c.InventoryItemId); e.BookQuantity = c.BookQuantity; e.CountedQuantity = c.CountedQuantity; e.Processed = c.Processed; e.LineNumber = c.LineNumber; e.ReversalLineNumber = c.ReversalLineNumber; e.Description = c.Description; e.IsPropertyBookQuantityRemoved = c.IsPropertyBookQuantityRemoved; e.IsPropertyCountedQuantityRemoved = c.IsPropertyCountedQuantityRemoved; e.IsPropertyProcessedRemoved = c.IsPropertyProcessedRemoved; e.IsPropertyLineNumberRemoved = c.IsPropertyLineNumberRemoved; e.IsPropertyReversalLineNumberRemoved = c.IsPropertyReversalLineNumberRemoved; e.IsPropertyDescriptionRemoved = c.IsPropertyDescriptionRemoved; e.CreatedBy = (string)c.RequesterId; e.CreatedAt = ApplicationContext.Current.TimestampService.Now <DateTime>(); return(e); }// END Map(IMergePatch... ////////////////////////////
}// END ThrowOnInconsistentCommands ///////////////////// protected virtual IPhysicalInventoryLineEvent Map(IPhysicalInventoryLineCommand c, IPhysicalInventoryCommand outerCommand, long version, IPhysicalInventoryState outerState) { var create = (c.CommandType == CommandType.Create) ? (c as ICreatePhysicalInventoryLine) : null; if (create != null) { return(MapCreate(create, outerCommand, version, outerState)); } var merge = (c.CommandType == CommandType.MergePatch || c.CommandType == null) ? (c as IMergePatchPhysicalInventoryLine) : null; if (merge != null) { return(MapMergePatch(merge, outerCommand, version, outerState)); } var remove = (c.CommandType == CommandType.Remove) ? (c as IRemovePhysicalInventoryLine) : null; if (remove != null) { return(MapRemove(remove, outerCommand, version)); } throw new NotSupportedException(); }