(MVP Template) How do I forward error messages from Presenter to View?

I have a method in which the user can search for the article number, and if it is available in the database, the articlenumber is bound to a BindingList. Now I want to tell the user if there is an article in the database. How do I do it right?

Just pass the errorMessage message to my interface method?

Leading:

string errorMessage;
_view.ErrorMessage(errorMessage);

View:

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

Would you do it the same way?

+5
source share
5 answers

What am I doing.

Another way I read will be for the model to know how to show the error (possibly using ErrorMessagePresenter), so the error is separated from the original presenter.

, .

0

. :

public event PresenterEventHandler Message;

:

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

:

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
}

, . , , , , .

+3

. , , / .

:

_windowManager.NoItemFound(errorMessage)

:

_statusBox.Text = errorMessage; MessageBox.Show(errorMessage);
+1

MVP - , View , :

public interface IViewArticleList {
    // 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(IViewArticleList 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;
        // whatever, whatever...    
        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();
    }
}
+1

We must not reinvent the wheel ....

You should just throw an exception into your model.

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

In catch, show your message box.

+1
source

All Articles