验证设计模式 [英] Validation Design Pattern

查看:294
本文介绍了验证设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我wrting对于具有下列要求我们部门的一个数据验证工具。
- 动态增加新的业务实体
- 动态添加新验证的实体。
- 一个UI显示业务实体及其validaiton
名单 - 用户将拥有选项来启动上的所有验证或selcted业务实体validaiton。
- 如果任何验证失败的用户界面将显示验证错误消息。
- 系统应着手即使任何验证从而发生故障,所有配置validaiton被验证的下一个验证

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.

搜索互联网后,我发现以下2有希望的设计模式满足我的业务需求的一个识别码Decorator模式,另一种是指挥链(责任限制又名链)。现在的问题是,这是更好?任何人有什么好主意吗?

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?

感谢

推荐答案

我想你想要的是规范模式。所以,你会做这样的事情:

I think what you want is the Specification Pattern. So you would do something like this:

public void StartDateNotInPastSpecification : ISpecification<ISomeBusinessObject>
{
  public bool IsSatisfiedBy(ISomeBusinessObject myBusinessObject)
  {
    return myBusinessObject.StartDate >= DateTime.Now;
  }
}



有关这种模式的好处是,每个规则孤立容易测试和你选择何时应用验证规则(相对于一些框架而强加给你这个决定)。

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).

这篇关于验证设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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