Пример #1
0
        private async Task <FlowResult <TResultModel> > HandleRequestAsync <TModel, TResultModel>(
            IFlowRequest <TModel, TResultModel> request, TModel model)
            where TModel : class where TResultModel : class
        {
            try
            {
                Logger.LogInfo(FlowLogs.RequestStarted, args: model.GetType().Name);
                if (request == null)
                {
                    throw new FlowException(FlowErrors.ServiceIsNull, nameof(request));
                }

                var validator = ObjectActivator.GetValidator <TModel>();

                var validateResult = await validator.ValidateAsync(StateManager, model);

                Logger.LogInfo(FlowLogs.RequestHasWarn, args: validateResult.Warns.Count.ToString());
                Logger.LogInfo(FlowLogs.RequestHasError, args: validateResult.Errors.Count.ToString());

                if (!validateResult.Succeeded)
                {
                    return(FlowResult <TResultModel> .Failed(validateResult.Errors.ToArray()));
                }


                Logger.LogInfo(FlowLogs.RequestOperationStarted, args: model.GetType().Name);
                var requestResult = await request.DoAsync(model);

                Logger.LogInfo(FlowLogs.RequestOperationFinished, args: model.GetType().Name);

                if (validateResult.Warned)
                {
                    requestResult.Warns.AddRange(validateResult.Warns);
                }

                Logger.LogInfo(FlowLogs.RequestFinished, args: model.GetType().Name);
                return(requestResult);
            }
            catch (Exception ex)
            {
                Logger.LogError(FlowLogs.ExceptionOccured, ex.Message);
                return(FlowResult <TResultModel> .Failed(new FlowError(FlowErrors.ErrorOccurred)));
            }
        }