Checks in MVC / MVP

I am new to MVC / MVP and learn it by building a Winform application.

To some extent, I created models, presenters, and presentations ... Now, where are my tests coming up.

I think that the initial check of the data type (for example, only the numbers in the "Age" field) should be performed on presentation. While other checks (for example, age within 200) should be performed according to the model.

Regarding data type validation, my view provides 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 the test separately, but how can I tell the facilitator that the test has not yet been completed when she tries to access the Age? Resource. In particular, if the field is optional.

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

I understand correctly, or something is missing.

Update (for clarity):. In this simple case, when the age field is optional, what to do when the user entered his name instead of a number. I cannot pass null, as this would mean that the user was left empty by the user. So, how do I inform the presenter that the wrong data is entered ...

+7
c # model-view-controller mvp winforms
source share
3 answers

On the MVP side (I think this is more suitable for WinForms), the answer to your question is moot. However, the key to my understanding was that you could change your mind at any time. That is, I would have to provide a new WinForms view to display your application or bind it to the ASP.NET MVC interface.

Once you realize this, validation becomes aparant. The application itself (business logic) should throw exceptions, handle errors, etc. The user interface logic should be dumb. In other words, to represent WinForms, you must make sure that the field is not empty, or so on. Many control properties allow this - the Visual Studio Property Panel. Checking the correct coding in the GUI for throw-like exceptions is a big no. If you must have validation both in terms of the representation and the model that you will duplicate, all you need is a simple check, for example, controls that are not empty. Let the actual application itself perform the actual verification.

Imagine if I switched your view to the front end of ASP.NET MVC. I would not specify controls, and therefore some form of client-side scripting would be required. What I am doing is that the only code you need to write is for the views, that is, do not try to generalize the UI authentication for the different views, as this will defeat the purpose of separating your problems.

Your main application should have all your logic in it. The specified viewing logic (WinForms, Javascript, etc. properties) Must be unique for each view. In my opinion, the properties and interfaces that each view should check are wrong.

+1
source share

If your "view provides values ​​as properties," I suspect you have missed something. The main difference between MVP / MVC and some other UI decoupling patterns is that they include a model that is designed for the main container for data shared by the view and presenter or controller.

If you use the model as a data container, the responsibility for validating it becomes clear. Since only the presenter (or controller) actually does nothing but display the data, he is responsible for verifying that the data is in an acceptable state for the operation he is about to perform.

Adding visual indicators of data verification problems to the editor form / window during editing is certainly nice to have. However, it should be considered more or less equivalent for viewing the “eye candy”, and it should be considered as an addition to the real validation logic (even if it is based on common metadata or code). The master (or controller) must remain true authority for the reliability of the data, and its verification code should not be placed in the view.

0
source share

I believe that the validation applies only to javascript, since the view does not run any code on the post, only the controller does.

But I would never trust a javascript check, because a malicious user could bypass it, or an ignorant user might have JS disabled, so repeat any JS check in the server code in the controller.

A view may have facilities for displaying any errors.

-one
source share

All Articles