예제 #1
0
        async Task <PreProcessResult> ILogic <TState, TAction> .PreProcess(IStore <TState> store, TAction action)
        {
            if (_killed)
            {
                // Once fatal poision pill is received don't continue.
                return(new PreProcessResult(false, action));
            }

            CancellationTokenSource = new CancellationTokenSource();
            // Transformation occurs first
            action = await OnTransfrom(() => store.State, action, CancellationTokenSource.Token);

            // Validation occurs second.
            ValidationResult validationResult;

            using (var dispatcher = MultiDispatcher.Create(store))
            {
                validationResult = await OnValidate(() => store.State, action, dispatcher, CancellationTokenSource.Token);
            }

            if (!validationResult.IsValid)
            {
                // If validation fails.
                using (var dispatcher = MultiDispatcher.Create(store))
                {
                    OnValidationError(validationResult.Error, action, dispatcher);
                }
                return(new PreProcessResult(continueToNext: false, action: action));
            }
            // When validation passes.
            return(new PreProcessResult(continueToNext: true, action: action));
        }
예제 #2
0
 private void ProcessLongRunningLogic(Func <IMultiDispatcher, Task> longRunningProcess)
 {
     // Run long running process in it's own thread.
     Task.Factory.StartNew(async() =>
     {
         using (var dispatcher = MultiDispatcher.Create(Store))
         {
             await longRunningProcess(dispatcher);
         }
     }, TaskCreationOptions.LongRunning).Unwrap();
 }
예제 #3
0
            /// <summary>
            /// Execute business logic process.
            /// </summary>
            /// <typeparam name="TAction"></typeparam>
            /// <param name="action"></param>
            /// <param name="logicChain"></param>
            /// <returns></returns>
            private async Task ExecuteProcessMethod <TAction>(TAction action, List <object> logicChain)
            {
                foreach (var logic in logicChain)
                {
                    switch (logic)
                    {
                    case ILogic <TState, AnyAction> anyActionLogic:
                        if (anyActionLogic.IsLongRunning)
                        {
                            ProcessLongRunningLogic((dispatcher) => anyActionLogic.Process(() => Store.State, new AnyAction(action), dispatcher));
                        }
                        else
                        {
                            using (var dispatcher = MultiDispatcher.Create(Store))
                            {
                                await(anyActionLogic.Process(() => Store.State, new AnyAction(action), dispatcher) ?? Task.WhenAll());
                            }
                        }
                        break;

                    case ILogic <TState, TAction> actionLogic:
                        if (actionLogic.IsLongRunning)
                        {
                            ProcessLongRunningLogic((dispatcher) => actionLogic.Process(() => Store.State, action, dispatcher));
                        }
                        else
                        {
                            using (var dispatcher = MultiDispatcher.Create(Store))
                            {
                                await(actionLogic.Process(() => Store.State, action, dispatcher) ?? Task.WhenAll());
                            }
                        }
                        break;
                    }
                }
            }