Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(MVP Pattern) How to forward error messages from Presenter to View?

Tags:

mvp

I have a method where the user can search for a article number and if its available in the database the articlenumber gets bound to a BindingList. Now I want to let the user know if the article is not available in database. How do I do that the right way?

Just pass the message errorMessage to my interface method?

Presenter:

string errorMessage;
_view.ErrorMessage(errorMessage);

View:

public void ErrorMessage(string errorMessage)
{
      MessageBox.Show(errorMessage);
}

Would you do it the same way?

like image 608
Sreedhar Avatar asked Apr 27 '09 11:04

Sreedhar


4 Answers

We bubble an event. In the presenter you register that event:

public event PresenterEventHandler Message;

And then raise it like so:

PresenterEventArgs pe = new PresenterEventArgs("Error message", Status.Error);
this.Message(this, pe);

Then in the view:

protected override void OnInit(EventArgs e)
{
    this.presenter = new MyPresenter(this, MyBusinessService.Instance);
    this.presenter.Message += new PresenterEventHandler(presenter_Message);
}

void presenter_Message(object sender, PresenterEventArgs pe)
{
    // display error message
}

You can pass different types of statuses back to the view in this way, and not just error messages. We have Success, Error, Locked, Warning, Help.

like image 96
Rebecca Avatar answered Nov 13 '22 21:11

Rebecca


In the case of error messages I would call some base functionality. This way you could choose wether to update the status window on the bottom left and/or display a modal message box.

In the presenter:

_windowManager.NoItemFound(errorMessage)

In the window manager:

_statusBox.Text = errorMessage; MessageBox.Show(errorMessage);
like image 31
Dries Van Hansewijck Avatar answered Nov 13 '22 19:11

Dries Van Hansewijck


We should not re-invent the wheel ....

You should simply throw an exception in your model.

Then, the view will catch the exception using a try catch block.

In the "catch", show your message box.

like image 24
flamandier Avatar answered Nov 13 '22 21:11

flamandier


If its MVP - Passive View, then the View interface should have a property that could read:

public interface IArticleListView {
    // more stuff here...
    bool ErrorMessageVisible { set; }
    string ErrorMessage { set; }
    // more stuff here...
    int ArticleNumber { get; }
}

public interface IPresenter {
    public void Update();
}

public class ArticleListPresenter : IPresenter {
    IViewArticleList _view;

    public ArticleListPresenter(IArticleListView view) {
        this._view = view;
        // you could update the view here or with an update method,
        // completely up to you.
    }

    // Assuming you are using a fine grained presenter
    public void HandleArticleNumberSearch() {
        bool articleNotFound;
        // search for the article ...    
        if ( articleNotFound ) {
            this._view.ErrorMessageVisible = true;
            this._view.ErrorMessage = string.Format("The article #{0} was not found.", number);
        }
    }
}

public class ArticleList : Page, IArticleListView {
    ArticleListPresenter _presenter;

    public bool ErrorMessageVisible {
        set { this.lblErrorMessage.Visible = value; }
    }

    public bool ErrorMessage {
        set { this.lblErrorMessage.Text = value; }
    }

    public int ArticleNumber {
        // You have to do some sort of validation here, but I'll keep it simple
        get { return Integer.Parse(this.txtArticleNumber.Text); }
    }

    protected override void OnInit(EventArgs e) {
        this._presenter = new ArticleListPresenter(this);
    }

    protected void Page_Load(object sender, EventArgs e) {
        // this implementation keeps the state in the View, and updates it
        // in the presenter: Passive View
        if(!this.IsPostBack) {
            this._presenter.Update();
        }
    }

    protected void OnSearchArticleButtonClick(object sender, EventArgs e) {
        this._presenter.HandleArticleNumberSearch();
    }
}
like image 21
Marcel Valdez Orozco Avatar answered Nov 13 '22 21:11

Marcel Valdez Orozco