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

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

问题描述

我正在为我们的一个部门编写一个数据验证实用程序,它具有以下要求。
- 动态添加新的业务实体
- 动态地向实体添加新的验证。
- 显示业务实体及其有效性列表的用户界面
- 用户可以选择启动对所有或被选中的业务实体验证的验证。
- 如果任何验证失败,UI将显示验证错误消息。
- 系统应该进行下一次验证,即使任何验证失败,因此所有配置的有效性都被验证。



搜索互联网后,我发现以下2个推荐设计满足我的业务需求的模式一个装饰模式,另一个是命令链(又称为责任链)。现在我的问题是哪个更好?任何人有更好的想法?



谢谢

解决方案

我想您要的是规格模式。所以你会这样做:

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

这个模式的好处是每个规则是可以孤立地进行测试,您可以选择何时应用验证规则(而不是将某些框架强加于您)。


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

解决方案

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天全站免登陆