繁体   English   中英

当其他类在C#中获得结果时,对类进行回滚或提交(企业库事务)

[英]RollBack or Commit (Enterprise Library Transaction) over classes when other class get result in C#

我有一堂课是桌子的镜子。

    public class Patient
    {
        public int Patient_id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public bool Sex { get; set; }

        public Patient f_UpdatePatient(Patient _patient)
        {
            string QUpdate = "  UPDATE Patients "
                             + "   SET Name    = '" + _patient.Name + "'"
                             + "      ,Address = '" + _patient.Address + "'"
                             + "      ,Sex = " + (_patient.Sex ? "M" : "F")
                             + " WHERE patient_id = " + _patient.Patient_id;

            Database db1 = DatabaseFactory.CreateDatabase("cnnStr");
            DataSet ds = db1.ExecuteDataSet(CommandType.Text, QUpdate);

            // First, How can i set my instance which is coming as a parameter 
            // to my real object. Without passing each value to eac prop of obj.
            return this = _patient;
        }
    }

我还有另一个使用耐心的课程。 在使用它之前,它正在为Patient类的一个实例设置新值,因此我可以将最后一个值发送到Web服务。 (实际上,我需要在使用不在我的项目中的Web服务之前编写db)

    public class ServiceConnection
    {
        public static void Main(string[] args)
        {
            Patient pt = new Patient(12);

            // Just want to show that i need DB update.
            pt.Name = args[0];
            pt.Address = args[1];

            try
            {
                // Now i am connection web service and sending pt instance
                bool isOk = f_SendToService(pt);
            }
            catch (Exception ex)
            {
                // Now, at this point, I WANT TO ROLLBACK MY UPDATE with TRANSACTION 
                // or something else
                throw (ex);
            }
        }

        private static bool f_SendToService(Patient pt)
        {
            // trying to send. But sometimes, i get timeout errors
            // sometimes i get false result.
            throw new WebException("Sorry it couldn't reach to WS");
        }
    }

如果我得到异常或错误结果,您对我有什么建议吗?

我想使用TRANSACTION(因为我不想再次设置同一行。)但是如何呢?

感谢您的建议...

您可以实现System.ComponentModel.IEditableObject接口并像这样使用该实体

  Patient pt = new Patient(12);
  pt.BeginEdit();
  pt.Name = args[0];
  pt.Address = args[1];
  if (WSCallIsOK())
     pt.EndEdit();       // save is OK
  else
     pt.CancelEdit();   // sets values back

仅针对属性Sex的示例代码

// holds backup of values
private string __Sex;
private string _Sex;
public string Sex
{
    get
    {
        return _Sex;
    }
    set
    {
        bool changed = _Sex != value;
          if (changed)
            {
                this.RaisePropertyChanged("Sex");
            }

    }
}


#region IEditableObject Members

private bool _editing = false;

public void BeginEdit()
{
    if (!_editing)
    {
        // create copy of property
        __Sex = _Sex;
        //other properties here
        _editing = true;
    }
}

public void CancelEdit()
{
    if (_editing)
    {
        // revert back
        _Sex = __Sex;
        //other properties here
        _editing = false;

    }
}

public void EndEdit()
{
    if (_editing)
    {
        _editing = false;
    }
}

#endregion

暂无
暂无

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

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