views:

83

answers:

2

Hi Experts,

I am working on a webform project, where I want to implement MVP Pattern. I have gone through few articles and project based on MVP. It seems fine to me.

At most of the places, If there is any need to display error message, it has been made a method in the view interface. I am giving an example here for clarity.

public interface IAdminSettingsView { string Name { get; set; } string Password { get; set; } string Email { get; set; } void ShowErrorMessage(string errorMessage); }

here is my control implementing IAdminSettingsView

public partial class AdminSettingsEdit : BaseControl, IAdminSettingsView {

    private AdminSettingsPresenter _adminSettingsPresenter;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        _adminSettingsPresenter = new AdminSettingsPresenter(this);

    }


    protected void BtnSave_Click(object sender, EventArgs e)
    {
        _adminSettingsPresenter.Save();
    }

    #region Implementation of IAdminSettingsView

    public string Name
    {
        get { return AdminName.Text; }
        set { AdminName.Text = value; }
    }

    public string Password
    {
        get { return AdminPassword.Text; }
        set { AdminPassword.Text = value; }
    }

    public string Email
    {
        get { return AdminEmail.Text; }
        set { AdminEmail.Text = value; }
    }


    public void ShowErrorMessage(string errorMessage)
    {
        lblErrorMessage.Text = errorMessage;
    }

    #endregion
}

here is my presenter

public class AdminSettingsPresenter 
{
    public AdminSettingsPresenter(IAdminSettingsView view)
    {
        _view = view;

    }

    private IAdminSettingsView _view;
    public void Save()
    {

        try
        {
            //Trying Save Data Here

        }
        catch (Exception exception )
        {
            _view.ShowErrorMessage("Couldnt Save Data");

        }

    }

}

My question is, how can I seperate Error Related messages into a different Interface and then make a communication between them. for example if I have an interface

interface IShowErrorMessage { //somemethod here

}

how to use this interface to work with my main IAdminSettingsView interface.

Your help will be appreciated. If there is any better solution to this problem, I would love to hear.

Regards Parminder

A: 

At the lowest level (for example the Utility DLL) I put the classes for error handling. One of which is a interface that forms can implement. On startup the software registers the form implementing the interface with the low level DLL. This form can be defined at the highest level (the EXE).

When an error occurs at any level of the software the appropriate calls can be made to the error framework and if a form implemented the display error interface then a form will display at that point.

As an aside I do this as well for status and progress messages. There is a IStatusDisplay interface and a IProgressDisplay interface. The EXE registers the forms or classes that implement these interfaces

RS Conley
A: 

First, in IShowErrorMessage, define the properties/methods needed to show an error message.

Something like:

interface IShowErrorMessage
{
    void Show(string errorMessage);
}

Next, your view will implement that interface. The view's implementation of Show(string) will setup a literal.

The presenter will remain unchanged.

Create some unit tests and you are good!

Hope it helps.

Maxime