C ++ MVC

编程入门 行业动态 更新时间:2024-10-25 20:21:14
C ++ MVC - 数据验证(C++ MVC - data validation)

简短形式:如果我验证用户在Controller中输入的数据(通过视图),是否违犯了MVC? 它是否真的必须在模型中完成(我读过这个地方)?

长形式:假设我有一个C ++模型 - 视图 - 控制器论坛应用程序(如果它很重要,它的GUI是使用Qt制作的)。 我有一个视图和它的控制器。 用户通过认证并按下“新帖子”按钮。 该按钮的动作侦听器唯一的作用是它调用Controller-> createNewPost(),而后一种方法执行此操作:

string postContent = View-> readPostContentFromInputDialog(); (此方法显示一个包含文本框的用户对话框,并返回键入为字符串的内容)

现在,剩下要做的是:

验证postContent至少有3个字符

将帖子存储在某种数据库中(当前用户的用户名/ ID是已知的,并且在存储到数据库时将与postContent相关联)

问题如下:在Controller中执行此检查相对容易。 但是,我知道MVC中的数据验证(我受限于使用MVC)应该在模型中完成。

因此,我们假设我调用Model-> store(postContent),它也对数据进行验证,以决定是否要存储数据。 但是如果数据无效,那么返回Controller 出错的最好方法是什么,以便它可以通过View显示错误(if(INVALID DATA)View-> displayErrorDialog(“WHAT WAS WRONG”);) ? 我可以让Model-> store方法返回true / false,但真正的问题是如何返回关于什么是无效的detalis? 我猜这些异常可以在这里工作,但是每次我必须验证用户输入(不仅仅是“新帖子”操作)时,这样做似乎相当复杂。

检查控制器似乎更容易,那么这是否真的违反了MVC? 或者,您能否建议我以其他方式返回(从模型到控制器)关于数据的什么是无效的?

Short form: Do I violate MVC if I validate data input by the user (through the View) in the Controller? Does it really have to be done in the Model (I read that someplace)?

Long form: Suppose I have a C++ Model-View-Controller forum application (whose GUI is made using Qt, if it matters). I have a View and its Controller. A user is authenticated and presses the "New Post" button. The only thing the button's action listener does is that it calls Controller->createNewPost() and this latter method does this:

string postContent = View->readPostContentFromInputDialog(); (this method displays a dialog to the user containing a text box and returns what was typed as a string)

Now, what's left to do is:

validate that the postContent has at least 3 characters

store the post in some kind of database (the current user's username/id is known and will be associated with the postContent when stored to the database)

The problem is the following: it would be relatively easy to do this check right here in the Controller. However, I understand that data validation in MVC (I'm constrained to use MVC) should be done in the Model.

So, let's suppose I call Model->store(postContent), which also does validation for the data to decide if it's going to be stored or not. But in case of invalid data, what would be the best way to return what was wrong to the Controller so that it can display an error through the View (if(INVALID DATA) View->displayErrorDialog("WHAT WAS WRONG");)? I could just make the Model->store method return true/false, but the REAL problem is how do I return detalis about WHAT was invalid? I guess exceptions could work here, but it seems quite complicated to do this every time I have to validate user input (not just for the "New Post" action).

It seems easier to just check in the Controller, so does this REALLY violate MVC? Or, could you please suggest me some other way of returning (from Model to Controller) what was invalid regarding the data?

更多推荐

本文发布于:2023-08-06 17:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!