Issue with designer not recognizing concrete overridden method returns

I am currently building an application framework.  There is a base form and a strip builder form that inherits from it to build tool, menu and status strips as needed.  A concrete form then inherits from the strip builder form.  This architecture uses MVP and the presenters are "set" in an overridden "hook" method as follows:

private AbstractPresenter _presenterDelegate;
public FW_BaseForm()
{
   HookBeforeInitialize();
   InitializeComponent();
   HookAfterInitialize();
}

protected virtual void HookBeforeInitialize()
{
   SetPresenter(GetConcretePresenter());
}

// Had to use a methods here instead of a property because
// the form kept initializing my public property to null in "InitializeComponent()"

public virtual void SetPresenter(AbstractPresenter pPresenter)

   _presenterDelegate = pPresenter;
}

public AbstractPresenter GetPresenter() { return _presenterDelegate; }


// null return here was a problem as well so I tried a "dummy"

protected virtual AbstractPresenter GetConcretePresenter()
{
   return
new DummyPresenter();
}

The architecture also uses 2 interfaces ( and their subclass interfaces) IViewToPresenter and IPresenterToView to communicate back and forth to/from the presenter and the view.  The tool strip form from above, implements an interface that inherits from IViewToPresenter top "get" the information from the presenter on how to build the tool/menu/status strips.  Each inheriting interface also defines a property to cast the form at that level to the inheriting interface as follows:

public interface IV2P_GetToolStrips : IViewToPresenter
{
   ToolStripDescriptor GetToolStrip();
   MenuStripDescriptor GetMenuStrip();
   StatusStripDescriptor GetStatusStrip();
   IV2P_GetToolStrips IV2P_GetToolStrips { get;}
}

// the tool strip form implements the interface as follows: (only 1 method and the property are shown for brevity)

#region IV2P_GetToolStrips Members

public ToolStripDescriptor GetToolStrip()
{
   // call the presenter
   return IV2P_GetToolStrips.GetToolStrip();
}

public IV2P_GetToolStrips IV2P_GetToolStrips
{
get { return ((IV2P_GetToolStrips)GetPresenter()); } }


// the concrete class overrides the "GetConcretePresenter()" method:

protected
override AbstractPresenter GetConcretePresenter()
{
   return new CustomerOrderManagementPresenter();
}


// this is the abstract presenter that implements "IV2P_GetToolStrips"
public class AbstractPresenterWithToolStrips : AbstractPresenter,
                                                               
IV2P_GetToolStrips
{
   public AbstractPresenterWithToolStrips() { }

   #region IV2P_GetToolStrips Members

   public virtual ToolStripDescriptor GetToolStrip()
   {
      
return null;
   }
#endregion


//  the only concrete code for the "CustomerOrderManagementForm" test is:

public partial class CustomerOrderManagementForm : FW_BaseFormWithToolStrips
{
   
public CustomerOrderManagementForm() 
   {
      InitializeComponent();
      HookAfterInitialize();
   }

   protected override AbstractPresenter GetConcretePresenter()
   {
      return new CustomerOrderManagementPresenter();
   }
}


The designer chokes on trying to cast the "DummyPresenter" to the interface "IV2P_GetToolStrips" which means that it is not processing the overridden method and getting the concrete instance of the "CustomerOrderManagementPresenter" .  Does anyone have a work around for this  

The application runs and works great, but it's going to be impossible to use the framework if the designer cannot recognize overridden template methods that load concrete objects at run time.

Any and all help/suggestions would be greatly appreciated.




Answer this question

Issue with designer not recognizing concrete overridden method returns

  • onetrakmindz

    Bill,

    The reason for this is due to the architecture of the Windows Forms designer. The designer doesn't actually create an instance of the form you are designing, but rather its base class, which in your case, is BaseFormWithToolStrips.

    So when GetConcretePresenter is called, BaseFormWithToolStrips.GetConcretePresenter is called, not CustomerOrderManagementPresenter.GetConcretePresenter.

    For more information see the following blog post: http://www.urbanpotato.net/default.aspx/document/1772

    To get around this there is a couple of ways:

    1. Don't use the designer (obviously not ideal)

    2. Derive another class from CustomerOrderManagementPresenter and design that instead. This will cause the correct presenter to be created and returned.

    3. Using Type descriptors and a custom designer, implement a class that stands in for CustomerOrderManagementForm that returns the correct presenter, for more details on this see: http://www.urbanpotato.net/default.aspx/document/2001

    HTH

    David

  • Issue with designer not recognizing concrete overridden method returns