在处理N层架构时如何管理事务?

时间:2014-02-18 11:08:19

标签: c# asp.net sql transactions

商业实体类:

public class TextileApplication
{    
  private System.Int64 _UserId;
  private System.String _ApplicationNo;
  private System.Int32 _SchemeId;

}

.cs页面上的代码:

protected void ibtnSubmitFirstPanel_Click(object sender, EventArgs e)
{
      TextileApplication _TextileApplication = new TextileApplication();
      _TextileApplication.UserId = 1;//static
      _TextileApplication.ApplicationNo = GenerateApplicationNo();
      _TextileApplication.SchemeId = Convert.ToInt16(rblScheme.SelectedValue);

      string i = blTextileApplication.InsertTextileApplication(_TextileApplication);
      if (Convert.ToInt16(i.Split(',').GetValue(0)) > 0)
      {

       // insert into another table 

      }
      else
      {                   
           // rollback
      }
}

商务访问类:

public static string InsertTextileApplication(TextileApplication _TextileApplication)
{
    string i = "0";
    try
    {
        daTextileApplication _daTextileApplication = new daTextileApplication();
        object [] o = _daTextileApplication.InsertTextileApplication(_TextileApplication);
        i = o[0].ToString();

    }
    catch (Exception ex)
    {
        LogErrorToLogFile logFile = new LogErrorToLogFile();
        logFile.LogError(ex);
        throw ex;
    }
    return i;
}

数据访问类:

public object[] InsertTextileApplication(TextileApplication _TextileApplication)
{
    try
    {

      pList = new List<SqlParameter>();
      pList.Add(new SqlParameter("@UserId", _TextileApplication.UserId));
      pList.Add(new SqlParameter("@ApplicationNo", _TextileApplication.ApplicationNo));
      pList.Add(new SqlParameter("@SchemeId", _TextileApplication.SchemeId));

      SqlParameter _AppNoOut = new SqlParameter("@AppNoOut", SqlDbType.VarChar,50);
      _AppNoOut.Direction = ParameterDirection.Output;
      pList.Add(_AppNoOut);

      object[] o = sa.ExecuteQueryWithOutParameters("SPInsertTextileApplication", pList);

      return o;

    }
    catch (Exception ex)
    {
       // logFile.LogError(ex);
       // throw ex;
    }
}

SQL访问类:

public class SqlAccess
{
     public object[] ExecuteQueryWithOutParameters(String procedureName, List<SqlParameter> param)
     {
        int count = 0;

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = procedureName;

        cmd.Parameters.Clear();

        if (param != null)
        {
            foreach (SqlParameter p in param)
            {
                cmd.Parameters.Add(p);

                if (p.Direction == ParameterDirection.Output)
                {
                    count++;
                }
            }
        }

        try
        {
           connect();
           cmd.Connection = con;
           cmd.ExecuteNonQuery();
           //iRet = Convert.ToInt32(retValReference.Value);
           object[] obj = new object[count];
           count = 0;

           if (param != null)
           {
               for (int i = 0; i < param.Count; i++)
               {
                   if (param[i].Direction == ParameterDirection.Output)
                   {
                        obj[count] = param[i].Value.ToString();
                        count++;
                   }
               }
           }

           return obj;
      }
      catch (Exception ex)
      {
        throw ex;
      }
      finally
      {
        closeconnect();
      }
  }
}

我知道这是一个复杂的架构。我需要应用事务概念然后该怎么做??

1 个答案:

答案 0 :(得分:2)

阅读System.Transaction命名空间。事务管理如果非常强大和完美适用于项目交易,即使是通过一个设计不完善的多层架构(如您的)。

或者,单位的工作模式也是合适的。

但是男人,你真的尝试编写尽可能多的代码,而不是使用已建立的模式。列表中较低的3个类不应该由人写。