MVP with Winforms App using ErrorProvider

 
89   96.5
Oct 10, 2010
 
Hi

I have been looking through the Patterns in Action solution especially in terms of the MVP pattern as I would like to refactor an existing application to use MVP as a lot of logic is tied up in the code behind the Forms.  One issue I can't seem to find a clean solution to is how to wire up a Presenter with a windows forms ErrorProvider control.

One possible solution I have read about is to have a SetError method defined in the View and implemented by the Form which takes the control name and error message as arguments and sets the error for the specified control e.g.

Form code behind:

public void SetError(string controlName, string errorMessage)
{
    Control c = Controls[controlName];
    errorProvider.SetError(c, errorMessage);
}

However, from what I can see, this would require the Presenter to know about controls on the Form (at least control names) as the Presenter would call for example:

_view.SetError("txtName", "Please enter name.");

Is this a suitable implementation or are there any other ways to have the presenter validate and set error messages on form controls?

Any help is greatly appreciated.

Matt



60   95.3
Oct 11, 2010
Matt

Although I have no experience in MVP, I can offer a possible suggestion.

The way that we achieve this within out framework is to encapsulate the rules within the concrete class and have a collection of 'BrokenRules' available to the client. For example, if you are constructing the class and it requires a date to be set, the 'Validate' method with the class can be called and this will add the rule to the collection. You can then check this collection or alternatively have a property of 'IsValid' which is set after the Validate method has been called. Although it does not tie into a specific control, you will have access to the rules that are broken and can display these within a generic control capable of displaying them.

This requires the use of a factory to build the object and have the associated code built in.

If this is something that may work for you, let me know and I can stick up some code.

Chris
 1 comment
 
Hi Chris Thanks for your reply. I like the idea of having a collection of broken rules. I have a lot of Forms that currently use the error provider which users are now accustomed to but this may be an option as each broken rule/error message could just be displayed as a list on screen which should hopefully still be clear enough for users. The nice thing about the error provider however is it clearly highlights the control that needs attention which on large forms would be quicker for the user to spot the issue. Thanks Matt --- Matt Fothergill  Oct 11, 2010