Пример #1
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="definition"></param>
 /// <param name="objs"></param>
 /// <returns></returns>
 public static List <T> Update <T>(this ICommandDefinition definition, List <T> objs)
     where T : class
 {
     using (var connection = new SqlConnection(definition.ConnectionString)) {
         using (var command = definition.GetCommand()) {
             command.Connection = connection;
             connection.Open();
             try {
                 foreach (var obj in objs)
                 {
                     ParameterMethods.SetInputs(obj, command);
                     command.ExecuteNonQuery();
                     ParameterMethods.SetOutputs(obj, command);
                 }
             }
             catch (SqlException sqlEx) when(sqlEx.ShouldTryReexecute())
             {
                 if (command.IsCommandBuilt())
                 {
                     objs = definition.Update(objs);
                 }
                 else
                 {
                     throw sqlEx;
                 }
             }
             catch {
                 throw;
             }
         }
     }
     return(objs);
 }
Пример #2
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="definition"></param>
 /// <param name="parameter"></param>
 /// <returns></returns>
 public static T Execute <T>(this ICommandDefinition definition, T parameter = null)
     where T : class
 {
     using (var connection = new SqlConnection(definition.ConnectionString)) {
         using (var command = definition.GetCommand()) {
             command.Connection = connection;
             connection.Open();
             try {
                 ParameterMethods.SetInputs(parameter, command);
                 command.ExecuteNonQuery();
                 ParameterMethods.SetOutputs(parameter, command);
             }
             catch (SqlException sqlEx) when(sqlEx.ShouldTryReexecute())
             {
                 if (command.IsCommandBuilt())
                 {
                     parameter = definition.Execute(parameter);
                 }
                 else
                 {
                     throw sqlEx;
                 }
             }
             catch {
                 throw;
             }
         }
     }
     return(parameter);
 }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="definition"></param>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public static List <T> ToList <T>(this ICommandDefinition definition, T parameter = null)
            where T : class, new()
        {
            List <T> ret = new List <T>();

            using (var connection = new SqlConnection(definition.ConnectionString)) {
                using (var command = definition.GetCommand()) {
                    command.Connection = connection;
                    connection.Open();
                    try {
                        ParameterMethods.SetInputs(parameter, command);
                        executeReaderAndFillList(command, ret);
                        ParameterMethods.SetOutputs(parameter, command);
                    }
                    catch (SqlException sqlEx) when(sqlEx.ShouldTryReexecute())
                    {
                        if (command.IsCommandBuilt())
                        {
                            ret = definition.ToList(parameter);
                        }
                        else
                        {
                            throw sqlEx;
                        }
                    }
                    catch {
                        throw;
                    }
                }
            }
            return(ret);
        }
        /// <summary>
        /// the command should have the connections string set,  doesnt have to be open but
        /// the string should be set.
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="P"></typeparam>
        /// <param name="parameter"></param>
        /// <param name="command"></param>
        /// <returns></returns>
        public static R FirstOrDefault <R, P>(this P parameter, ICommandDefinition definition)
            where R : class, new()
            where P : class
        {
            R newObj = null;

            using (var connection = new SqlConnection(definition.ConnectionString)) {
                using (var command = definition.GetCommand()) {
                    command.Connection = connection;
                    connection.Open();
                    try {
                        ParameterMethods.SetInputs(parameter, command);
                        newObj = DataReaderMethods.GetFirst <R>(command);
                        ParameterMethods.SetOutputs(parameter, command);
                    }
                    catch (SqlException sqlEx) when(sqlEx.ShouldTryReexecute())
                    {
                        if (command.IsCommandBuilt())
                        {
                            newObj = parameter.FirstOrDefault <R, P>(definition);
                        }
                        else
                        {
                            throw sqlEx;
                        }
                    }
                    catch {
                        throw;
                    }
                }
            }
            return(newObj);
        }