Beispiel #1
0
        public virtual IDbResult <T> ExecuteContained()
        {
            IDbResult <T> result = new DbResult <T>();

            if (typeof(T).IsClass)
            {
                result.Data = Activator.CreateInstance <T>();
            }
            else
            {
                result.Data = default(T);
            }


            if (_onInit != null)
            {
                result = _onInit();
            }

            try
            {
                using (var conn = this.CreateConnectionInstance())
                {
                    conn.ConnectionString = _connectionString;
                    conn.Open();


                    var cmdModelItems =
                        _onSetCommand != null
                           ? _onSetCommand() : new List <IDbConnectorSettings>
                    {
                        new DbConnectorSettings()
                    };


                    using (var transaction = conn.BeginTransaction(cmdModelItems.First().TransactionIsolationLevel))
                    {
                        try
                        {
                            foreach (var cmdModel in cmdModelItems)
                            {
                                using (var cmd = conn.CreateCommand())
                                {
                                    cmd.Connection  = conn;
                                    cmd.CommandType = cmdModel.CommandType ?? System.Data.CommandType.StoredProcedure;
                                    cmd.CommandText = cmdModel.CommandText;


                                    if (cmdModel.Parameters != null && cmdModel.Parameters.Any())
                                    {
                                        cmd.Parameters.AddRange(cmdModel.Parameters.ToDbParameters(cmd).ToArray());
                                    }


                                    if (_onOpen != null)
                                    {
                                        result.Data = _onOpen(cmd);
                                    }
                                }
                            }

                            transaction.Commit();
                        }
                        catch (Exception e)
                        {
                            transaction.Rollback();
                            throw e;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Log();
                result.Error = ex;

                if (_onError != null)
                {
                    result = _onError(ex, result);
                }
            }

            return(result);
        }
Beispiel #2
0
        public static IDbResult <bool> ExecuteAll(
            IsolationLevel isolationLevel = IsolationLevel.ReadCommitted,
            params DbConnectorJob <T, TDbConnection>[] taskItems)
        {
            IDbResult <bool> result = new DbResult <bool>();

            try
            {
                if (taskItems != null && taskItems.Length > 0)
                {
                    string connectionString = taskItems[0].ConnectionString;

                    using (var conn = taskItems[0].CreateConnectionInstance())
                    {
                        conn.ConnectionString = connectionString;
                        conn.Open();

                        using (var transaction = conn.BeginTransaction(isolationLevel))
                        {
                            try
                            {
                                foreach (var item in taskItems)
                                {
                                    var cmdModelItems = item.OnSetCommand != null
                                                                ? item.OnSetCommand() : new List <IDbConnectorSettings>
                                    {
                                        new DbConnectorSettings()
                                    };

                                    foreach (var cmdModel in cmdModelItems)
                                    {
                                        using (var cmd = conn.CreateCommand())
                                        {
                                            cmd.Connection  = conn;
                                            cmd.CommandType = cmdModel.CommandType ?? System.Data.CommandType.StoredProcedure;
                                            cmd.CommandText = cmdModel.CommandText;

                                            if (cmdModel.Parameters != null && cmdModel.Parameters.Any())
                                            {
                                                cmd.Parameters.AddRange(cmdModel.Parameters.ToDbParameters(cmd).ToArray());
                                            }

                                            if (item.OnOpen != null)
                                            {
                                                item.OnOpen(cmd);
                                            }
                                        }
                                    }
                                }

                                transaction.Commit();

                                result.Data = true;
                            }
                            catch (Exception e)
                            {
                                transaction.Rollback();
                                throw e;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Log();
                result.Error = ex;
            }

            return(result);
        }