Xomega Framework has built-in support for tracking modifications to your data objects and data properties, which allows you to easily implement such features as confirmation dialogs about unsaved changes, lazy loading as well as optimizing your calls to update operations. The following tutorial describes this feature and its usages in more details.
In order to check the modification state of your data object you can call the
IsModified() method on your object, which returns a nullable boolean.
- If the result is null then it means that the data for your objects has never been loaded, i.e. none of the properties have been set and no child objects have been added to any of its object lists. This allows you to implement lazy loading, where you can load only parts of the object on as needed basis.
- If the result is false, then your object has been loaded and never modified since then.
- If the result is true, then your object has been modified since it's been loaded, and you can prompt the user for unsaved changes if need be. The following example demonstrates such a prompt.
Code:
void CheckUnsavedChanges(object sender, CancelEventArgs e)
{
bool? modified = obj.IsModified();
if (modified.HasValue && modified.Value && MessageBox.Show(
"You have unsaved changes. Do you want to discard them and close the window?",
"Unsaved Changes", MessageBoxButton.YesNo, MessageBoxImage.Warning,
MessageBoxResult.No) == MessageBoxResult.No)
e.Cancel = true;
}
After saving your changes to the object in a details form, you want to make sure that you reset the modification state on the object and all its data properties and child objects as follows.
Code:
obj.SetModified(false, true); // true means recursive
In a similar manner, you can check the modification state of any of the object's individual data properties and child objects. This can help you identify which parts of the object have never been initialized and therefore need to be lazy loaded, as well as which parts have been modified by the user to determine which update operation to call.
The following snippet illustrates how to work with the modification state of individual data properties.
Code:
bool? dobModified = obj.BirthDateProperty.Modified;
if (!dobModified.HasValue)
{
// DOB has never been set, read it from DB
}
if (dobModified.HasValue && dobModified.Value)
{
// DOB changed, update it in DB
}