在哪里保留验证逻辑 [英] Where to keep validation logic

查看:123
本文介绍了在哪里保留验证逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



应用程序中哪里应该有验证逻辑?



我有一个小应用程序允许将新产品嵌入应用程序数据库。产品名称不同,产品名称,订单号,描述等不同的产品可以插入新产品,更新现有产品。因此,当插入新产品时,所有字段都必须被验证,但是当现有产品被更新时,只有正在更新的字段才需要被验证,也就是仅仅是描述被更新,所以只有该字段才能被验证。 p>

我想到一个抽象类和两个具体类,用于完整和部分产品验证器,每个类都有自己的验证逻辑包含在类级别。



我有这种感觉,虽然这样做有一个更好的模式 - 任何建议?

解决方案


应用程序中哪里应该有验证逻辑?


取决于你的架构。验证可以在多个阶段进行处理以实现响应。通常,尽管模型/控制器似乎是给定MVC架构中的好地方。这个问题有时在 Joel在MVC架构的上下文中的旧论坛一>。模型应该负责接受/拒绝输入似乎是合理的。


因此,当新产品被
插入时,所有字段必须为
验证


是的。


但现有产品
正在更新,那么只有更新的字段
需要验证
即可能只是描述是
被更新,所以只有该字段
应该被验证。


您不可能预测哪些具体部分将被更新。因此,您需要为所有字段(数据库的列)写入验证器。



您可以简化生活并拥有一个验证器类(除非当然验证一组特定的属性太复杂/耗时)。


OK i have seen other posts about this but none really specifically answer my question.

Where in an application should validation logic be?

I have a small application that allows new products to be instered into an applications database. There are different products with different fields i.e. product name, order number, description etc. New products can be inserted and existing products can be updated. Therefore when a new product is being inserted then all fields must be validated but when an existing product is being updated then only the fields being updated need to be validated i.e. maybe just the descirption is being updated so only that field should be validated.

Im thinking of an abstract class and two concrete classes for full and part product validators, each having their own validation logic contained at class level.

I have the feeling though that there must be a better pattern for this - any advice?

解决方案

Where in an application should validation logic be?

Depends on your architecture. Validation can be handled in multiple stages to achieve responsiveness. Typically, though the model/controller seems like a good place in a given MVC architecture. This question came up sometime back on Joel's old forum in context of the MVC architecture. It seems plausible that the model should be responsible for accepting/rejecting input.

Therefore when a new product is being inserted then all fields must be validated

Yes.

but when an existing product is being updated then only the fields being updated need to be validated i.e. maybe just the descirption is being updated so only that field should be validated.

You cannot possibly foretell which exact part will be updated. So, you'll need to write validators for all fields (columns of your database).

You could simplify life and have a single validator class (unless of course validating a particular set of attributes is too complicated/time consuming).

这篇关于在哪里保留验证逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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