public object Execute(string procedureName, string parameters)
        {
            OperationHandler.OnOperating(new OperationArgs(CurrentUser.Id, TableOperation.Execute, ObjectType.StoreProcedure, procedureName, parameters));

            if (string.IsNullOrEmpty(procedureName))
            {
                throw new ArgumentNullException("procedureName");
            }

            using (var cnn = connectionManager.WriteConnection)
            {
                object param = null;
                if (!string.IsNullOrEmpty(parameters))
                {
                    var obj = JsonConvert.DeserializeObject(parameters) as JObject;
                    param = DynamicParametersBuilder.Build(obj);
                }

                var result = SqlMapper.Query(cnn, procedureName, param, null, false, null, System.Data.CommandType.StoredProcedure);

                OperationHandler.OnOperated(new OperationArgs(CurrentUser.Id, TableOperation.Execute, ObjectType.StoreProcedure, procedureName, parameters, result));

                return(result);
            }
        }
        /// <summary>
        /// Invoke the specified code with connection directly.
        /// </summary>
        /// <param name="conn">The specific connection.</param>
        /// <param name="code">The specific code.</param>
        /// <param name="parameters">The invocation parameters. Could be JObject, JArrary and string (json format).</param>
        /// <param name="queryOnly">Indicates if using readOnly database connection.</param>
        /// <param name="transaction">The specific transaction.</param>
        /// <param name="multipleResults">Indicates the result set is array or single object.</param>
        /// <returns>The invocation result.</returns>
        public dynamic InvokeCodeWithConnection <T>(IDbConnection conn, string code, object parameters, bool queryOnly = true, IDbTransaction transaction = null, bool multipleResults = true)
        {
            if (typeof(IEnumerable).IsAssignableFrom(typeof(T)))
            {
                throw new ArgumentException("Generic type '{0}' should not be an IEnumerable.".FormatWith(typeof(T)));
            }

            try
            {
                var context = new InvokeContext
                {
                    Repository  = this,
                    CodeItem    = CSIItem.Build(code, queryOnly, transaction != null),
                    Connection  = conn,
                    Transaction = transaction
                };

                // Converted param could be JObject or JArray.
                JToken convertedParam = ProcessParameters(parameters);
                context.Param = convertedParam;

                var commandParam = convertedParam == null ? null : DynamicParametersBuilder.Build(convertedParam);
                context.CommandParam = commandParam;
                context.FireOperatingEvent(ObjectType.CodeInvocation);

                var result = InvokeCodeInternal <T>(code, context);

                if (transaction != null)
                {
                    transaction.Commit();
                }

                context.Result = multipleResults ? (dynamic)result : result.FirstOrDefault();
                context.FireOperatedEvent(ObjectType.CodeInvocation);

                return(context.Result);
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                throw new CSIInvocationException($"Invoke code with connection was failed. {Environment.NewLine}code: `{code}` {Environment.NewLine}parameters: {JsonConvert.SerializeObject(parameters)}", ex);
            }
        }
        private dynamic InvokeInternal(InvokeContext context, object parameters, bool withoutTransactionHandling = false)
        {
            if (parameters == null)
            {
                parameters = new JObject();
            }
            // Converted param could be JObject or JArray.
            JToken convertedParam = ProcessParameters(parameters);

            context.Param = convertedParam;

            // Resolve middle wares for BeforeBuildCommandParam.
            MiddleWareFactory.Resolve(context, ActivePosition.BeforeBuildCommandParam);

            var commandParam = context.Param == null ? null : DynamicParametersBuilder.Build(context.Param);

            context.CommandParam = commandParam;

            // Fire the operating event.
            context.FireOperatingEvent();

            dynamic result = null;

            if (context.CodeItem.code is JObject)
            {
                JObject data            = new JObject();
                var     exportResultSet = context.CodeItem.resultSet != null?context.CodeItem.resultSet.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) : null;

                var resultIndex = 0;
                foreach (var code in context.CodeItem.code as JObject)
                {
                    var multipleResults = exportResultSet == null || exportResultSet.Length <= resultIndex || exportResultSet[resultIndex++].Equals("M", StringComparison.CurrentCultureIgnoreCase);
                    // Resolve middle wares for BeforeInvoke.
                    MiddleWareFactory.Resolve(context, ActivePosition.BeforeInvoke);
                    var itemResult = InvokeCodeInternal <dynamic>(context.CodeItem.code[code.Key].Value <string>(), context);
                    data.Add(code.Key, JToken.FromObject(multipleResults ? itemResult : itemResult.FirstOrDefault()));
                }

                result = data;
            }
            else
            {
                var multipleResults = string.IsNullOrEmpty(context.CodeItem.resultSet) || context.CodeItem.resultSet.Equals("M", StringComparison.CurrentCultureIgnoreCase);
                // Resolve middle wares for BeforeInvoke.
                MiddleWareFactory.Resolve(context, ActivePosition.BeforeInvoke);
                var data = InvokeCodeInternal <dynamic>(context.CodeItem.code.Value <string>(), context);
                result = multipleResults ? data : data.FirstOrDefault();
            }

            if (!withoutTransactionHandling && context.CodeItem.requiredTransaction && context.Transaction != null)
            {
                context.Transaction.Commit();
            }

            context.Result = result;

            // Resolve middle wares for AfterInvoke.
            MiddleWareFactory.Resolve(context, ActivePosition.AfterInvoke);

            context.FireOperatedEvent();

            return(context.Result);
        }