Any thoughts on this approach for generically handling simple save/rebinding scenarios across the UI? Did I miss something?
This uses DevExpress message boxes for errors currently, but I'd probably refine this later on.
public static void RebindUI<T>(BindingSource source, ref T bo, bool saveObject, bool rebind) where T:
Csla.Core.ISavable, Csla.Core.ISupportUndo, ICloneable
{
// disable events
source.RaiseListChangedEvents = false;
try
{
// unbind the UI
UnbindBindingSource(source, saveObject, true);
// save or cancel changes
if (saveObject)
{
bo.ApplyEdit();
try
{
T temp = (T)bo.Clone();
bo = (T)temp.Save();
}
catch (Csla.DataPortalException ex)
{
DevExpress.XtraEditors.XtraMessageBox.Show(ex.BusinessException.ToString(),
"Error saving", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
catch (Exception ex)
{
DevExpress.XtraEditors.XtraMessageBox.Show(ex.ToString(),
"Error Saving", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
else
bo.CancelEdit();
// rebind UI if requested
if (rebind)
{
bo.BeginEdit();
source.DataSource = bo;
}
}
finally
{
// restore events
source.RaiseListChangedEvents = true;
if (rebind)
{
// refresh the UI if rebinding
source.ResetBindings(false);
}
}
}
Here is the dependent method it uses:
static void UnbindBindingSource(BindingSource source, bool apply, bool isRoot)
{
System.ComponentModel.IEditableObject current = null;
try
{
current = source.DataSource as System.ComponentModel.IEditableObject;
}
catch (Exception)
{
current = null; // Ignore
}
// Workaround:
// If can't retrieve the current, use Currency Manager to be sure edit applied or cancelled?
if (current == null)
{
if (apply)
source.CurrencyManager.EndCurrentEdit();
else
source.CurrencyManager.CancelCurrentEdit();
}
if (isRoot)
source.DataSource = null;
if (current != null)
{
if (apply)
current.EndEdit();
else
current.CancelEdit();
}
}
I'm using CSLA 3.0.4. Could this be back ported to that version?
Maybe it's time to upgrade.... :)
Copyright (c) Marimer LLC