I have a class which is mirror of table.
我有一个桌子的镜子。
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;
}
}
I have another class which uses Patient. Before use it, it is setting new values to one instance of Patient class, thus i can send the last value to web service. (In fact i need to write db before i use web service which is not in my project)
我有另一个使用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");
}
}
If i get exception or false result, do you have any suggest to me about how can i handle it??
如果我得到异常或错误的结果,你有什么建议我怎么处理?
I want to use TRANSACTION (because i don't want to set same row again.) but HOW?
我想使用TRANSACTION(因为我不想再设置相同的行。)但如何?
Thanks in advice ...
谢谢你的建议......
1 个解决方案
#1
You could implement System.ComponentModel.IEditableObject interface and use that entity like this
您可以实现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
Example code only for property Sex
示例代码仅适用于属性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
#1
You could implement System.ComponentModel.IEditableObject interface and use that entity like this
您可以实现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
Example code only for property Sex
示例代码仅适用于属性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