In continuing the series of the Nucleo MVP framework, the first parts of the steps can be found here:

As I mentioned before, the presenter knows the view through an interface.  The following is a sample interface.

public interface ICreateCustomerView : IView
{
                #region ” Events “
 
                event EventHandler Cancelled;
                event EventHandler Loaded;
                event EventHandler Saved;
                event EventHandler ViewAllCustomers;
 
                #endregion
 
 
 
                #region ” Properties “
 
                string FirstName { get; set; }
                string LastName { get; set; }
                string AccountNumber { get; set; }
 
                #endregion
 
 
 
                #region ” Methods “
 
                void ShowMessage(string message);
 
                #endregion
}

Notice that all views implement the following interfaces:

IView
IView<TModel>

The view, with or without a direct reference to a model, defines an interface contract that the user control implements.  Because of the dynamic compilation of ASP.NET pages and user controls (depending on project type and setup), using an interface makes it really easy for pieces to talk to each other.  I’m going to post the entire view implementation in chunks to make it easier to read through.  Presenters come in various forms.  Presenters can be an ASP.NET page or an ASP.NET user control.  Theoretically, a view could be a custom control too using the interface; however, pages and user controls have two common base classes in the form of:

BasePage
BasePage<TModel>
BaseUserControl
BaseUserControl<TModel> 

to make development life easier.  Depending on whether you are using the IView or IVIew<TModel> interface depends on the base class to use and depending on whether or not a model is being used.  Our sample implementation is a page, but future examples use user controls as well.

public partial class CreateCustomer : BaseViewPageICreateCustomerView { }

The page implements the views interface to the full as shown below:

public event EventHandler Cancelled;
public event EventHandler Loaded;
public event EventHandler Saved;
public event EventHandler ViewAllCustomers;

public string AccountNumber

{
                get { return this.txtAccountNumber.Text; }
                set { this.txtAccountNumber.Text = value; }
}
 
public string FirstName 
{
                get { return this.txtFirstName.Text; }
                set { this.txtFirstName.Text = value; }
}
public string LastName 
{
                get { return this.txtLastName.Text; }
                set { this.txtLastName.Text = value; }
}
 
public void ShowMessage(string message)
{
                this.lblMessage.Text = message;
}

Note that the properties simply read/write from the UI controls. Views implemented this way tend to follow the passive view form of MVP, allowing the presenter to fully control the finer UI aspects of reading and writing data.

There are some additional steps needed.  First, in order to link the view to the presenter, an attribute can be defined reflectively, or the view can be linked to the presenter by overring a method in BaseViewPage or BaseViewUserControl, as was done below:

protected override IPresenter CreatePresenter()
{
                return new CreateCustomerPresenter(this);

You can see that the presenter is explicitly returned, passing the view through the constructor of the presenter.  Again, there are other ways around this, which I’ll blog about later.

Lastly, the events that fire within the view simply propagate the events directly.

void btnCancel_Click(object sender, EventArgs e)
{
                if (Cancelled != null)
                                Cancelled(this, e);
}
 
void btnLoad_Click(object sender, EventArgs e)
{
                if (Loaded != null)
                                Loaded(this, e);
}
 
void btnSave_Click(object sender, EventArgs e)
{
                if (Saved != null)
                                Saved(this, e);
}
 
void btnViewAll_Click(object sender, EventArgs e)
{
                if (ViewAllCustomers != null)
                                ViewAllCustomers(this, e);
}

This has been an initial look at how we can structure the user interface.