Objects must be arranged in a certain order: is it the smell of code?

Context

I am working with a Winforms application (.NET 3.5 SP1), which has a workspace concept that can contain n number of panels. Each panel (obtained from Panel) has the form:

    .-----------------------.
    | Workspace |
    | .--------. .--------. |
    || Panel1 | | Panel2 | |
    || .-----. | | .-----. | |
    ||| View1 | | || View2 | | |
    || '-----' | | '-----' | |
    | '--------' '--------' |
    '-----------------------'

All panels are added to the collection this.Controlsof the Workspace class (which is derived from UltraTabPageControlthe Infragistics GUI control). Each view is added to Controlstheir parent's collection . Therefore, when Disposecalled in the workspace, panels and views are automatically deleted, which is quite expected and desirable.

We have another concept called a ViewManager. It tracks all the controls Viewin the workspace and is responsible for maintaining one “main” view. Whenever created View, it registers with this manager. This adds Viewto the list and then runs some logic to define a new “main” view, and then calls a method Synchronize()for each view.

, , View.Dispose(), ViewManager. View , , .

, Panel, . , Dispose, :

protected override void Dispose(bool disposing)
{
    var theSpecialPanel = GetSpecialPanel();
    if (theSpecialPanel != null)
    {
        theSpecialPanel.Dispose();
    }
    base.Dispose(disposing);
}

, theSpecialPanel. -, Synchronize() View, .

"InvalidComObjectException: COM-, RCW, ."

? , - ?

+1
3

, .

, , . , :

foreach (panel in Panels.Where(p => p != theSpecialPanel))
{
   panel.Dispose();
}
theSpecialPanel.Dispose();
+1

, . , , - . Froboz9000Connection ( Froboz 9000), SerialPort, . , . Frobozz9000Connection. Dispose , Dispose SerialPort. SerialPort, Frobozz9000Connection , , .

, , ( ) , . , , , , , , Dispose .

+2

SpecialPane? , Synchronize() ? , View Control.

public void Synchronize()
{
    if (this.IsDisposed || this.Disposing) return; // or return a 'remove me' flag
    ... 
    // sync
}

, , , Finalizer. , , , .

I believe this will also help in maintenance. Relying on the disposal order can be difficult, and if another developer needs to come, they can skip this at all. It will also work if the view is deleted in other ways, and not called from the form deletion method.

Oh, and if View does not extend Control, then give View a link to parent and check the Disposal state on the parent control.

+2
source

All Articles