I have following method in my presenters which validates the user inputs on the UI. Basically to capture the value of the text property of the text box, it uses sender object which was passed in by the event fire on the form.
PRESENTER
_View.ValidateInPut += new EventHandler(_View_ValidateInPut);
private void _View_ValidateInPut(object sender, EventArgs e)
{
var controller = (TextBox)sender;
var text = controller.Text;
If (text is InValid)
MessageBox.Show ("The value in the Text box is invalid");
}
VIEW
private void txtACNo_Validating(object sender, CancelEventArgs e)
{
ValidateInPut(sender, e); // Firing the event
}
Will this lead to unexpected errors?
If so please let me know a better way to do this?
Best Answer
I don't think it is a wise idea to have View and Presenter being coupled so tight.
Based on either the Passive Screen or Supervising Presenter the presenter and the view are separated by an interface that is implemented by the view. The presenter calls the interface. Here is an example of a supervising presenter.
PRESENTER
this class has no dependency on the used GUI library
ViewInterface
VIEW
Rationale
By utilizing your view and presenter the presenter (with all its (busines)logic) becomes easily testable or the view can be replaced by a WebPage without the need to adapt your presenter logic.
Test
WebPage
PS
Remember that the cast operator on an object may fail. Your code
will only work as long as
sender
is aTextBox
. It is better to check if there is a more generalized type up the hierarchy that still provides you with the needed interface. In your case that could beControl
that has aText
property as well. A more defensive approach would be: