private static CSIItem GetCodeItemWithName(string invokeName) { if (!ServiceResourceManager.CSIs.ContainsKey(invokeName)) { throw new CSINotFoundException("The csi is not found with name: " + invokeName); } return(CSIItem.Clone(ServiceResourceManager.CSIs[invokeName])); }
public static CSIItem Build(string code, bool queryOnly = true, bool requiredTransaction = false) { var result = new CSIItem { name = "Dynamic", requiredTransaction = requiredTransaction, resultSet = "M", queryOnly = queryOnly, code = code }; return(result); }
/// <summary> /// Invoke the Configured Code with specified connection. /// </summary> /// <param name="connection">The specified connection.</param> /// <param name="invokeName">The name of the csi code item.</param> /// <param name="parameters">The invocation parameters. Could be JObject, JArrary and string (json format).</param> /// <param name="transaction">The transaction.</param> /// <returns>The invocation result.</returns> public dynamic InvokeWithConnection(IDbConnection connection, string invokeName, object parameters, IDbTransaction transaction = null) { CSIItem codeItem = GetCodeItemWithName(invokeName); var context = new InvokeContext { Repository = this, CodeItem = codeItem, Connection = connection, Transaction = transaction }; return(InvokeInternal(context, parameters, true)); }
/// <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); } }
public static CSIItem Clone(CSIItem codeItem) { var result = new CSIItem { name = codeItem.name, requiredTransaction = codeItem.requiredTransaction, resultSet = codeItem.resultSet, queryOnly = codeItem.queryOnly, middleWares = codeItem.middleWares }; result.code = codeItem.code is JValue ? new string(codeItem.code.ToString().ToCharArray()) : JsonConvert.DeserializeObject <JToken>(codeItem.code.ToString()); return(result); }
/// <summary> /// Entry for the Configured Code Invocation. /// </summary> /// <param name="invokeName">The name of the csi code item.</param> /// <param name="parameters">The json format parameters.</param> /// <returns>The invocation result.</returns> public dynamic Invoke(string invokeName, object parameters) { CSIItem codeItem = GetCodeItemWithName(invokeName); using (var connection = codeItem.queryOnly ? connectionManager.ReadConnection : connectionManager.WriteConnection) { IDbTransaction transaction = null; connection.Open(); if (codeItem.requiredTransaction) { transaction = connection.BeginTransaction(); } try { var context = new InvokeContext { Repository = this, CodeItem = codeItem, Connection = connection, Transaction = transaction }; return(InvokeInternal(context, parameters)); } catch (Exception ex) { if (codeItem.requiredTransaction) { transaction.Rollback(); } throw new CSIInvocationException($"Invoke {invokeName} failed with parameters: {Environment.NewLine}{JsonConvert.SerializeObject(parameters)}", ex); } } }