1

In a perfect world you have the validation (verification) of inputs in the business logic layer, not in the presentation or persistence layer. In reality you can (or want) to place it anywhere.

Lets make a simple sample (web-application using a framework like JSF or ZK): A certain input field accepts 4 digits between 0001 and 0500.

  1. You could use the constraint features of your web framework to do this. Convenient for user, no additional server load.

  2. You could do it in business layer (eg java-ejb). Foolproof because all applications using the same ejb will use the same validation. Eventually not nice because you need to throw back error at user after evaluation. Needs roundtrip to and from server.

  3. You could rely (partially) on the DB to fail if someone enter (via persistence layer) a non-digit value or a value with more than 4 digits. Ugly.

Resume: You would do it (redundant) in 1. and 2. (Make it nice for the user and make it consistent for all applications). (Plus the length of DB col would be 4 )

Now the QUESTION: How you document this validation ? Text document or an UML diagram ? In a way you have business logic in up to 3 locations. In complex systems this close to impossible to track and understand.

Real life scenario for above sample: You need to change from 4 to 5 digits. Without documentation you need to hunt for the locations where changes might be required.

Whats you experience ? Any tips or tools for this ?

cheers
Sven

4

2 回答 2

1

在我的一个项目中,我能够使用正则表达式进行所有验证。幸运的是,我的数据库 (PostgreSQL) 支持正则表达式约束。通过在数据库模式级别定义正则表达式,我能够轻松地在整个应用程序中使用正则表达式验证。然后由应用程序逻辑继承,然后由客户端 javascript 验证引擎继承。

因为,我和我的同事都精通 SQL,所以它对我们来说是自我记录的。快速检查数据库的表定义会告诉您验证规则。如果我们需要生成正式的文档,那么将信息从数据库元数据中提取出来是微不足道的。

我知道我在这里的经历有点独特,但我想强调正则表达式是一种相对自记录的可移植解决方案。

于 2009-07-23T07:38:24.217 回答
1

诀窍是坚持DRY(不要重复自己)原则。

有几种不同的方法可以达到这个目标:

  1. 在 DB(Elijah 的方法)中定义传播到业务和 UI 层的约束
  2. 在业务层 (Java) 中定义约束并使用 GWT 在 UI 中运行相同的代码
  3. 等等,我相信还有很多其他方法可以达到相同的结果。

在不同的地方复制约束,然后“记录”它(添加另一个重复!)是低效率的秘诀!

于 2009-07-23T11:00:02.690 回答