COM Interop and Model-View-Presenter (now Supervising Controller and Passive View)

I had made a couple of comments about implementing the model view presenter at last night’s Edmonton .NET User Group meeting about reasons to use the MVP pattern. I had commented that, at the company I work for, we are using it as a method to decrease the eventual cost of switching from a VB6 font-end WinForms client.
 
First, it is a rule of COM that types MUST have a default constructor to be implemented through COM. So, it would appear, on first inspection, that that would blow away the dependency injection used in most samples of COM interop. This does not end up to be the case, as I will explain momentarily.
 
Second, it is also a COM convention to (generally) make a constructor that does not throw exceptions (see response by Harri for a good explanation as to why). 
 
The general pattern of implementing COM is to instantiate the COM object, then immediately call an Initialize method that can then throw an exception that is more easily handled.
 
So, this leaves you with an MVP Pattern (Passive Controller) that looks like the following:
 
[Guid("AF57B41F-6534-4f0c-BC35-797EEA609A2F")]
public interface IMyView {
    …
    //Methods and Properties implemented by the view

}
 
[

Guid("55B39A6D-0672-48e6-ACB5-B11E73A68F3A")]

public interface IMyPresenter {
    void Initialize(IMyView view);
    …
    //Other methods implemented by the presenter

}
 
[

ClassInterface(ClassInterfaceType.None)]

[

Guid("53AA0E3B-8AE7-4de0-8440-6685C441B528")]

public class MyPresenter : IMyPresenter {

    IMyView view;

 

    // Default Constructor required by COM

    public MyPresenter() { }

    //Overloaded constructor usable in .NET

    public MyPresenter(IMyView view) {

        Initialize(view);
    }
 

    public void Initialize(IMyView view) {

        if (view == null) throw new ArgumentNullException("view");

        this.view = view;

    }
 
    …

    //Other methods implemented by the presenter

}

 
 
This truly is the method we are using to decrease the eventual cost of re-implmenting a full .NET solution of our application. So far, it’s working pretty well.
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s