解耦模型和输入检查 [英] Decoupling the model and input checking

查看:166
本文介绍了解耦模型和输入检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将输入检查与模型分离并将其处理到其他地方(例如控制器)是否是一个好习惯?如果是这样,怎么能从MVC或DDD的角度来做呢?

Is it a good practise to decouple input checking from a model and have it handled elsewhere, say by a controller? If so, how could this be done from an MVC or DDD standpoint?

推荐答案

执行UI验证是一种很好的做法。

It is a good practice to perform UI validation.

例如如果您的域对象需要日期时间,那么如果应用程序的UI部分确保它将从用户正确的字符串接收,则将其解析为日期时间并将其传递给域对象是正确的。

E.g. if Your domain object expects date time, it is correct if UI part of application ensures it will receive from user correct string, will parse it to date time and pass it to domain object.

错误示例:UI部分验证银行帐户是否有足够的转帐资金。

Bad example: UI part validates if bank account has enough money for transfer.

但是 - 无法提供任何有关如何正确实施此类验证的提示你正在使用的框架(我不使用java)。

However - can't give any tips how to implement properly this kind of validation in framework You are using (I'm not working with java).

只是不要将它保存在控制器中。这不是控制人的责任。

Just don't keep it in controller. That is not controllers responsibility.

这篇关于解耦模型和输入检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆