简体   繁体   English

如何管理ado.net交易

[英]How to manage ado.net transaction

I'm not sure what a good way to manage this transaction in code would be. 我不确定在代码中管理此事务的好方法是什么。

Say I have the following 说我有以下内容

Service layer (non-static class) Repository layer (static class) 服务层(非静态类)存储库层(静态类)

// Service layer class //服务层类

/// <summary>
/// Accept offer to stay
/// </summary>
public bool TxnTest()
{
    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        conn.Open();
        SqlTransaction txn = conn.BeginTransaction();

        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.Transaction = txn;

            try
            {
                DoThis(cmd);
                DoThat(cmd);

                txn.Commit();
            }
            catch (SqlException sqlError)
            {
                txn.Rollback();
            }
        }
    }
}

// Repo Class // Repo Class

   /// <summary>
    /// Update Rebill Date
    /// </summary>
    public static void DoThis(SqlCommand cmd)
    {
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@SomeParam", 1);

        cmd.CommandText = "Select * from sometable1";
        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();
    }



 /// <summary>
        /// Update Rebill Date
        /// </summary>
        public static void DoThat(SqlCommand cmd)
        {
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@SomeParam", 2);

            cmd.CommandText = "Select * from sometable2";
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
  1. Is the above approach any good? 以上方法有什么好处吗? Is it wise to use a static class for the respository or will that create problems? 为存储库使用静态类是否明智或是否会产生问题?
  2. Is there a way to do this without having to pass a command (cmd) object around? 有没有办法在不必传递命令(cmd)对象的情况下执行此操作?

You might want to take a look at the unit of work pattern . 您可能想看一下工作单元模式

The unit of work pattern defines exactly what it suggests, a unit of work that is committed all at once, or not at all. 工作单元模式准确地定义了它所建议的内容,即一次性提交或根本不提交的工作单元。

This occurs by defining an interface that has two parts: 这通过定义具有两个部分的接口来实现:

  • Methods that handle your insert, update, deleted operations (note, you don't have to expose all of these operations, and you aren't limited to one entity type) 处理插入,更新,删除操作的方法(注意,您不必公开所有这些操作,并且您不限于一种实体类型)
  • A method to commit (if you rollback, you simply don't call commit). 提交的方法(如果您回滚,则只是不调用commit)。 This is where you would handle the transaction as well as the inserting, updating and/or deletion of all the entities that registered to be changed. 您可以在此处理事务,以及插入,更新和/或删除所有已注册要更改的实体。

Then, you would pass an implementation of this interface around, and commit the changes at the outer boundaries (your service) when all the operations are complete. 然后,您将传递此接口的实现,并在所有操作完成时在外部边界(您的服务)提交更改。

Note that the ObjectContext class in LINQ-to-Entities and the DataContext class in LINQ-to-SQL are both examples of units of work (you perform operations and then save them in a batch). 请注意,LINQ-to-Entities中的ObjectContext和LINQ-to-SQL中的DataContext都是工作单元的示例(您执行操作然后将它们保存在批处理中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM