tags:

views:

65

answers:

3

I'm new to MVC / MVP and learning it by creating a Winform application.

I have to some extent created the Models, Presenters and Views... Now where do my validations fit.

I think the initial datatype validation (like only numbers in Age field), should be done by view. Whereas the other validations (like whether age is within 200) should be done by model.

Regarding datatype validation, my view exposes the values as properties

public int? Age 
{ 
    get 
    { 
        int val; 
        if (Int32.TryParse(TbxAge.Text, out val))
        { 
            return val; 
        } 
        return null; 
    } 
    set 
    { 
        TbxAge.Text = value; 
    } 
} 

I can perform validation seperately, but how do I inform presenter that validation is still pending, when it tries to access the property Age ?. Particularly when the field is optional.

Is it good to throw a validationpending exception, but then the presenter must catch it at every point.

Is my understanding correct, or am I missing something.

Update (for the sake of clarity) : In this simple case where the age field is optional, What should I do when the user typed his name instead of a number. I cant pass null as that would mean the field has been left empty by the user. So how do I inform the presenter that an invalid data has been entered...

A: 

I believe view validation is only relevant in javascript as the view does not run any code on post, only the controller does.

But I would also not ever trust javascript validation as a malicious user could bypass it or an ignorant user might have JS disabled so repeat any JS validation in serverside code in the controller.

The view might have means to display any errors though .

David Mårtensson
Thanks... but my question is on winforms, not asp.net.
The King
+1  A: 

Coming from the MVP side (I believe it's more appropriate for WinForms) the answer to your question is debatable. However the key for my understanding was that at anytime you should be able to change your view. That is, I should be able to provide a new WinForms view to display your application or hook it upto a ASP.NET MVC frontend.

Once you realise this, the validation becomes aparant. The application itself (the business logic) should throw exceptions, handle errors and so forth. The UI logic should be dumb. In other words, for a WinForms view you should ensure the field is not empty, or so forth. A lot of the properties for the controls allow this - the properties panel of Visual Studio. Coding validation in the GUI for the likes of throwing exceptions is a big no no. If you were to have validation on both the view and the model you'd be duplicating code - all you require is some simple validation such as controls not being empty for example. Let the actual application itself perform the actual validation.

Imagine if I switched your view to a ASP.NET MVC front end. I would not have said controls, and thus some form of client side scripting would be required. The point I'm making is that the only code you should need to write is for the views - that is do not try to generalise the UI validation across views as it will defeat the purpose of separating your concerns.

Your core application should have all your logic within it. The specalised view logic (WinForms properties, Javascript etc...) should be unique per view. Having properties and interfaces that each view must validate against is wrong in my opinion.

Finglas
I should add, what you're attempting to do is possible, but it feels wrong. If you are deadset on this approach however, you'd need to use events to propate validation failures to the app. Using the decoupled "dumb" approach I mentioned provides a simplier app overall.
Finglas
Okay.. If you are saying I should do Datatype validation in Presenter... Then should my views expose all the values as string property, which could be read and validated by the controller?
The King
Not all as strings. Use primitive types to begin with. Take your Age example. An int is an int whether or not its WinForms or ASP.NET MVC. They're both C#. You could always factor these primitives out after into custom value classes such as PersonalDetails (in which age would be stored). These value classes would then be used as your 'ViewModel'. What you don't want to do with the MVP/MVC pattern is return types specific to your view. E.g HTML streams would not work in a WinForms app, yet a string/custom type representing a fragment of text would be universal.
Finglas
Sorry, its going little over my head.. In this simple case where the age field is optional, What should I do when the user Typed his name instead of a number. I cant pass null as that would mean the field has been left by the user. So how do I inform the presenter that an invalid data has been entered...
The King
No worries - I remember struggling with this some time back. If the user provides a string in the Age field, let the model handle that. Let it check/decide whether the value is valid. To inform the presenter that validation has failed, raise an event (check Google for how to do this) - your view would have a reference to this and could display a label with the reason the validation failed.
Finglas
Do you have any examples anywhere for such scenarios ?
The King
I don't, not publicly available. Besides, MVP is an architectural pattern, so there are different variations. As a test, try changing your view. If it's hard, you've done it wrong. If it's easy, and the only code you write is the new view - you've nailed it. Good luck.
Finglas
A: 

If your "view exposes the values as properties", I suspect that you are missing something. The principal distinction between MVP/MVC and some of the other UI decoupling patterns is that they include a model, which is intended to be the main container for data shared by the view and presenter or controller.

If you are using the model as a data container, the responsibility for validation becomes quite clear. Since only the presenter (or controller) actually does anything besides display the data, it is the one responsible for verifying that the data is in an acceptable state for the operation which it is about to perform.

Addition of visual indicators of data validation problems to an editor form/window during editing is certainly nice to have. However, it should be considered more or less equivalent to view "eye candy", and it should be treated as an add-on to the real validation logic (even if it is based on shared metadata or code). The presenter (or controller) should remain the true authority for data validity, and its validation code should not be hosted in the view.

Nicole Calinoiu
Then should the presenter be allowed direct access to the controls in the views... Are you saying you dont want a getter / setter methods in the views? Also please read my update to the question.
The King
No, the presenter should not have direct access to the controls. The data properties (e.g.: your Age example) should be on the model, not the view. Input parsing errors (such as your example where a name is typed in the Age text box) can be tricky to handle, but the underlying approach should be that unparsable data should never make it into the model. The exact mechanism to best address this will depend on your technology choices. For example, in Window Forms, judicious choice of controls and input masks can help prevent the parsing problem.
Nicole Calinoiu