private TransitionResult SwitchState(ICollection <CashoutsBatchState> expectedStates, CashoutsBatchState nextState)
        {
            if (expectedStates.Contains(State))
            {
                State = nextState;

                return(TransitionResult.Switched);
            }

            if (State < expectedStates.Max())
            {
                // Throws to retry and wait until aggregate will be in the required state
                throw new InvalidAggregateStateException(State, expectedStates, nextState);
            }

            if (State > expectedStates.Min())
            {
                // Aggregate already in the next state, so this event can be just ignored
                return(State == nextState
                    ? TransitionResult.AlreadyInTargetState
                    : TransitionResult.AlreadyInFutureState);
            }

            throw new InvalidOperationException("This shouldn't be happened");
        }
 public static CashoutsBatchAggregate Restore(
     string version,
     DateTime startMoment,
     DateTime?lastCashoutAdditionMoment,
     DateTime?expirationMoment,
     DateTime?closingMoment,
     DateTime?idRevocationMoment,
     DateTime?finishMoment,
     Guid batchId,
     string blockchainType,
     string assetId,
     string blockchainAssetId,
     string hotWalletAddress,
     int countThreshold,
     TimeSpan ageThreshold,
     ISet <BatchedCashoutValueType> cashouts,
     CashoutsBatchState state,
     CashoutsBatchClosingReason?closingReason)
 {
     return(new CashoutsBatchAggregate
            (
                batchId: batchId,
                startMoment: startMoment,
                blockchainType: blockchainType,
                assetId: assetId,
                blockchainAssetId: blockchainAssetId,
                hotWalletAddress: hotWalletAddress,
                countThreshold: countThreshold,
                ageThreshold: ageThreshold,
                version: version,
                cashouts: cashouts
            )
     {
         LastCashoutAdditionMoment = lastCashoutAdditionMoment,
         ExpirationMoment = expirationMoment,
         ClosingMoment = closingMoment,
         IdRevocationMoment = idRevocationMoment,
         FinishMoment = finishMoment,
         State = state,
         ClosingReason = closingReason
     });
 }
 private TransitionResult SwitchState(CashoutsBatchState expectedState, CashoutsBatchState nextState)
 {
     return(SwitchState(new[] { expectedState }, nextState));
 }