验证设计模式
我正在为我们的一个部门编写一个数据验证实用程序,它有以下要求。 - 动态添加新的业务实体 - 动态地向实体添加新的验证。 - 用于显示业务实体列表及其验证的 UI - 用户可以选择开始对所有或选定的业务实体进行验证。 - 如果任何验证失败,UI 将显示验证错误消息。 - 即使任何验证失败,系统也应继续进行下一次验证,从而验证所有配置的验证。
在搜索互联网后,我发现以下两种有前景的设计模式可以满足我的业务需求,一种是装饰器模式,另一种是命令链(又名责任链)。现在我的问题是哪个更好?有人有更好的主意吗?
谢谢
I am wrting a data validation utility for one of our department which has following requirement.
- Dynamically adding new business entity
- Dynamically adding new validations to an entity.
- An UI to display list of business entity and their validaiton
- User will have option to start the validation on all or selcted business entity validaiton.
- UI will display a validation error message if any validation fails.
- System should proceed to the next validation even if any of the validation fails thus all configured validaiton are validated.
After searching internet I found following 2 promissing design pattern which satisfy my business requirement one id Decorator pattern and another is Chain of Command (aka Chain of Responsibilty). Now my question is which is better? Anyone got any better idea?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要的是 规范模式。所以你会做这样的事情:
这种模式的好处是每个规则都可以轻松地单独测试,并且你可以选择何时应用验证规则(而不是某些框架将这个决定强加给你)。
I think what you want is the Specification Pattern. So you would do something like this:
The nice thing about this pattern is that each rule is easily testable in isolation and you get to choose when to apply the validation rules (as opposed to some frameworks which impose this decision on you).
我也在使用规范模式。这是它的基本实现。
通过此实现,我只需在构造函数中传递一个谓词,如下所示:
我的业务对象中有多个 bool 方法,而不是多个类(我的域中的每个条件一个)。
I'm using the Specification Pattern too. This is a basic implementation of it.
With this implementation, I just pass a predicate in the constructor, like this:
Instead of several classes (one per each condition in my domain), I have several bool methods in my business objects.