protected bool IsRepeatedCommand(IShipmentPackageCommand command, IEventStoreAggregateId eventStoreAggregateId, IShipmentPackageState state) { bool repeated = false; if (((IShipmentPackageStateProperties)state).Version > command.AggregateVersion) { var lastEvent = EventStore.GetEvent(typeof(IShipmentPackageEvent), eventStoreAggregateId, command.AggregateVersion); if (lastEvent != null && lastEvent.CommandId == command.CommandId) { repeated = true; } } return(repeated); }
protected virtual void Update(IShipmentPackageCommand c, Action <IShipmentPackageAggregate> action) { var aggregateId = c.AggregateId; var state = StateRepository.Get(aggregateId, false); var aggregate = GetShipmentPackageAggregate(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(IShipmentPackageCommand command, IShipmentPackageContentCommand innerCommand) { var properties = command as ICreateOrMergePatchOrDeleteShipmentPackage; var innerProperties = innerCommand as ICreateOrMergePatchOrRemoveShipmentPackageContent; if (properties == null || innerProperties == null) { return; } if (innerProperties.ShipmentPackageId == default(ShipmentPackageId)) { innerProperties.ShipmentPackageId = properties.ShipmentPackageId; } else { var outerShipmentPackageIdName = "ShipmentPackageId"; var outerShipmentPackageIdValue = properties.ShipmentPackageId; var innerShipmentPackageIdName = "ShipmentPackageId"; var innerShipmentPackageIdValue = innerProperties.ShipmentPackageId; ThrowOnInconsistentIds(innerProperties, innerShipmentPackageIdName, innerShipmentPackageIdValue, outerShipmentPackageIdName, outerShipmentPackageIdValue); } }// END ThrowOnInconsistentCommands /////////////////////
private static bool IsCommandCreate(IShipmentPackageCommand c) { return(c.Version == ShipmentPackageState.VersionZero); }
}// END Map(IMergePatch... //////////////////////////// protected virtual IShipmentPackageContentStateRemoved MapRemove(IRemoveShipmentPackageContent c, IShipmentPackageCommand outerCommand, long version) { c.RequesterId = outerCommand.RequesterId; var stateEventId = new ShipmentPackageContentEventId(c.ShipmentPackageId, c.ShipmentItemSeqId, version); IShipmentPackageContentStateRemoved e = NewShipmentPackageContentStateRemoved(stateEventId); e.CreatedBy = (string)c.RequesterId; e.CreatedAt = ApplicationContext.Current.TimestampService.Now <DateTime>(); return(e); }// END Map(IRemove... ////////////////////////////
}// END Map(ICreate... //////////////////////////// protected virtual IShipmentPackageContentStateMergePatched MapMergePatch(IMergePatchShipmentPackageContent c, IShipmentPackageCommand outerCommand, long version, IShipmentPackageState outerState) { c.RequesterId = outerCommand.RequesterId; var stateEventId = new ShipmentPackageContentEventId(c.ShipmentPackageId, c.ShipmentItemSeqId, version); IShipmentPackageContentStateMergePatched e = NewShipmentPackageContentStateMergePatched(stateEventId); var s = outerState.ShipmentPackageContents.Get(c.ShipmentItemSeqId); e.Quantity = c.Quantity; e.SubProductId = c.SubProductId; e.SubProductQuantity = c.SubProductQuantity; e.Active = c.Active; e.IsPropertyQuantityRemoved = c.IsPropertyQuantityRemoved; e.IsPropertySubProductIdRemoved = c.IsPropertySubProductIdRemoved; e.IsPropertySubProductQuantityRemoved = c.IsPropertySubProductQuantityRemoved; e.IsPropertyActiveRemoved = c.IsPropertyActiveRemoved; e.CreatedBy = (string)c.RequesterId; e.CreatedAt = ApplicationContext.Current.TimestampService.Now <DateTime>(); return(e); }// END Map(IMergePatch... ////////////////////////////
}// END ThrowOnInconsistentCommands ///////////////////// protected virtual IShipmentPackageContentEvent Map(IShipmentPackageContentCommand c, IShipmentPackageCommand outerCommand, long version, IShipmentPackageState outerState) { var create = (c.CommandType == CommandType.Create) ? (c as ICreateShipmentPackageContent) : null; if (create != null) { return(MapCreate(create, outerCommand, version, outerState)); } var merge = (c.CommandType == CommandType.MergePatch || c.CommandType == null) ? (c as IMergePatchShipmentPackageContent) : null; if (merge != null) { return(MapMergePatch(merge, outerCommand, version, outerState)); } var remove = (c.CommandType == CommandType.Remove) ? (c as IRemoveShipmentPackageContent) : null; if (remove != null) { return(MapRemove(remove, outerCommand, version)); } throw new NotSupportedException(); }