Topic: How do I know what has changed? |
Posted On: 9/18/2008 10:51 PM
Posted By: Ryan Aljets
|
Hello,
I have a business object that is bound to a form using BindingSource but before I save the changes I need to show the changes made by the user. I think this should be a simple question but I didn't see any sample or tutorial showing how I could do this. I am thinking that may be I should clone the object after I load it so I can compare the two before updating. Or is there a better way?
Ryan
Business object already performs a concurrency check even without timestamp column so this means object already has this information. Just not sure how to get it...
| 9/18/2008 11:19:07 PM Ryan Aljets
|
Hi Ryan,
This is indeed a very simple thing. What you are looking for is available in a property called OriginalValue on each field. Take a look at the following example code and that should help. If still something is unclear please do let us know.
| using (Customers customer = new Customers()) | | { | | customer.CustomerID.Value = 1; | | customer.Load(); | | customer.FirstName.Value = customer.FirstName.Value + " :)"; | | foreach (IField fld in customer.Fields) | | { | | // each field has a property called OriginalValue | | if (fld.OriginalValue != null) | | { | | // compare if both value and original value are not null | | if (!fld.IsNull && !fld.OriginalValue.IsNull && fld.Value.Equals(fld.OriginalValue.Value)) // You can compare the fld.Value with fld.OriginalValue.Value | | { | | MessageBox.Show(fld.GetResultSetName() + " has not changed!"); | | } | | // if both are null then there is no change either. | | else if (fld.IsNull && fld.OriginalValue.IsNull) | | { | | MessageBox.Show(fld.GetResultSetName() + " has not changed!"); | | } | | else | | { | | MessageBox.Show(fld.GetResultSetName() + " has changed"); | | } | | } | | else | | { | | MessageBox.Show(fld.GetResultSetName() + " can not be compared since the original value is not available."); | | } | | } | | } | | |
The above method is actually reusable, and you can simply create a method and pass it any business object instance to perform such comparison.
You can also get the original value like this:
| using (Customers customer = new Customers()) | | { | | customer.CustomerID.Value = 1; | | customer.Load(); | | customer.FirstName.Value = customer.FirstName.Value + " :)"; | | bool equals = customer.FirstName.Value.Equals(customer.FirstName.OriginalValue.Value); | | MessageBox.Show(equals.ToString()); | | } | | |
Thanks, Ish
| 9/19/2008 6:09:27 PM Ish Singh
|
Ish,
This is exactly what I needed to do and this is even simpler than what I expected. The solution you have provided works great and I am able to compare any of the objects rather than hard coding the same thing for each seperate class. While looking for this I also saw the IsDirty property and that is very useful as well.
I have couple of suggestions that I'll be posting soon but overall I am super impressed with what your framework can do.
Ryan
| 9/20/2008 6:44:53 PM Ryan Aljets
|