MVP with Winforms App using ErrorProvider
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];
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 Fothergill, Oct 10, 2010
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 Bint, Oct 11, 2010