RollBack or Commit (enterprise library transaction) over classes when another class receives result in C #
I have a class that is a mirror of a 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 that uses Patient. Before using it, it sets new values for one instance of the Patient class, so I can send the latest value to the web service. (Actually I need to write a db before I use a webservice which is not in my project)
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 an exception or false result, do you have any suggestions for me on how I can handle this?
I want to use TRANSACTION (because I don't want to set the same line again), but HOW?
Thanks for the consultation...
0
a source to share
1 answer
You can implement System.ComponentModel.IEditableObject interface and use this object like this
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
Sample code for Gender property only
// 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
0
a source to share