Example #1
0
        public async Task Handle(ICorrelationEvent evt)
        {
            if (!evt.CorrelationId.HasValue)
            {
                return;
            }

            var sagaState = await _sagaStateRepository.Read(evt.CorrelationId.Value).ConfigureAwait(false);

            if (sagaState == null)
            {
                return;
            }

            var result = await _eventDispatcher.OnEventAsync(sagaState.StateType, sagaState.State, evt).ConfigureAwait(false);

            if (result == null)
            {
                return;
            }

            if (!result.IsEventHandled)
            {
                return;
            }

            if (result.IsSagaComplete)
            {
                await _sagaStateRepository.Delete(evt.CorrelationId.Value).ConfigureAwait(false);
            }
            else
            {
                await _sagaStateRepository.Update(evt.CorrelationId.Value, sagaState.State).ConfigureAwait(false);
            }
        }
        public async Task <StateMachineProcessingResults> OnEventAsync(Type stateType, object state, ICorrelationEvent evt)
        {
            if (_stateMachineTypes == null)
            {
                throw new Exception("Saga configuration error. You need to configure the SagaStateMachineFactoryBuilder.");
            }

            var stateMachineType = _stateMachineTypes[stateType];

            if (stateMachineType == null)
            {
                if (_stateMachineTypes.Values.Count == 0)
                {
                    throw new Exception($"State machine type {stateType} not found. No state machines have been scanned. Did you add the assembly via SagaStateMachineFactoryBuilder.WithStateMachinesInAssembiles?");
                }
                throw new Exception($"State machine type {stateType} not found out of {_stateMachineTypes.Values.Count} state machines.");
            }

            if (_container == null)
            {
                throw new Exception("IContainer supplied to SagaStateMachineFactoryBuilder is null.");
            }

            var stateMachine = _container.GetInstance(stateMachineType);
            var methodInfo   = stateMachineType.GetMethod("ProcessNextStep", new[] { typeof(Guid), typeof(ICorrelationEvent), stateType, typeof(StateMachineProcessingResults) });

            if (methodInfo == null || stateMachine == null)
            {
                throw new Exception($"No valid state machine {stateMachineType.Name} for state {stateType.Name}.");
            }
            {
                var result = new StateMachineProcessingResults();
                await((Task)methodInfo?.Invoke(stateMachine, new[] { evt.CorrelationId, evt, state, result })).ConfigureAwait(false);
                return(result);
            }
        }
 public abstract Task ProcessNextStep(Guid correlationId, ICorrelationEvent evt, T state, StateMachineProcessingResults results);