A Generic Rebinding Method?

A Generic Rebinding Method?

Old forum URL: forums.lhotka.net/forums/t/5523.aspx


rsbaker0 posted on Friday, October 03, 2008

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();
    }
}

detritus replied on Friday, October 03, 2008

How about using new CslaActionExtender. It does the same thing behind the scenes and you don't write any method at all.

Sinan

rsbaker0 replied on Friday, October 03, 2008

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