简体   繁体   中英

How to manage transaction when dealing with N-tier architecture?

Business entity class:

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

}

Code on .cs page:

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
      }
}

Business Access class:

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;
}

Data access class:

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 access class:

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();
      }
  }
}

I know this is complex architecture. I need to apply transaction concept then how to do it??

Read up on the System.Transaction namespace. A transactionscipe if quite powerfully and perfectuy suitable to project transactions even through a not exactly well designed multi tiered architecture such as yours.

Alternatively a unit of work pattern would be suitable.

But man, you really try to write as much code as possible instead of using established patterns. the lower 3 classes in your list should never be written by a human.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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