Esempio n. 1
0
        protected virtual void ExecuteValidations <T>(ICPRContext currentCtx, CPRExecutionResult result, T cmd)
            where T : CPRCommand <T>
        {
            var validators = cmd.Validators.ToList();

            result.AddErrors(validators.SelectMany(v => v.Validate(currentCtx, cmd)));
            if (cmd is ICPRValidator <T> )
            {
                result.AddErrors(((ICPRValidator <T>)cmd).Validate(currentCtx, cmd));
            }
        }
Esempio n. 2
0
        public virtual CPRExecutionResult Execute <T>(T cmd, bool ensureCommandsHaveSecurityValidators = true)
            where T : CPRCommand <T>
        {
            try
            {
                BeforeExecute();

                var currentCtx = m_ctxFetcher.GetCurrentContext();

                var interceptors = cmd.Interceptors.ToList();

                foreach (var interceptor in interceptors)
                {
                    interceptor.Intercept(currentCtx, cmd);
                }
                if (cmd is ICPRInterceptor <T> )
                {
                    ((ICPRInterceptor <T>)cmd).Intercept(currentCtx, cmd);
                }

                var result = new CPRExecutionResult();

                //
                // Security checks
                //
                if (ensureCommandsHaveSecurityValidators && !cmd.SecurityValidators.Any())
                {
                    throw new Exception("Command does not have at least on security validator");
                }

                result.AddErrors(cmd.SecurityValidators.SelectMany(v => v.Authenticate(currentCtx, cmd)));
                // Security Validator will not be automatic (via is ICPRSecurityAValidator), since we want people to make a conscious decision
                // about which security policy applies
                if (result.Errors.Any())
                {
                    // Security errors cause immediate abort.
                    return(result);
                }

                //
                // Validation and Business Results
                //
                ExecuteValidations(currentCtx, result, cmd);
                if (result.Errors.Any())
                {
                    return(result);
                }

                DateTime utcStart = DateTime.UtcNow;
                if (cmd.ID == Guid.Empty)
                {
                    cmd.ID = Guid.NewGuid();
                }

                try
                {
                    ExecuteProjections <T>(currentCtx, cmd);

                    m_store.Persist(cmd, currentCtx.UserID, utcStart);
                    result.Success = true;
                }
                catch (CPRValidationException cprEx)
                {
                    m_logger.LogWarning(cprEx.ToString() + " - " + cprEx.StackTrace);
                    result.Success = false;
                    foreach (var err in cprEx.Errors)
                    {
                        result.AddError(err);
                    }
                }
                catch (Exception ex)
                {
                    m_logger.LogError(ex.ToString() + " - " + ex.StackTrace);
                    result.Success = false;
                    result.AddError(new CPRError()
                    {
                        Message = "Exception during execution: " + ex.Message,
                    });
                }

                return(result);
            }
            finally
            {
                AfterExecute();
            }
        }