验证设计模式

发布于 2024-10-06 06:55:49 字数 279 浏览 2 评论 0原文

我正在为我们的一个部门编写一个数据验证实用程序,它有以下要求。 - 动态添加新的业务实体 - 动态地向实体添加新的验证。 - 用于显示业务实体列表及其验证的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-10-13 06:55:49

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

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

这种模式的好处是每个规则都可以轻松地单独测试,并且你可以选择何时应用验证规则(而不是某些框架将这个决定强加给你)。

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

烧了回忆取暖 2024-10-13 06:55:49

我也在使用规范模式。这是它的基本实现。

public class Specification<T, E> : ISpecification<T, E>
{
    private Predicate<T> predicate;

    public Specification(Predicate<T> predicate)
    {
        this.predicate = predicate;
    }

    public bool IsSatisfiedBy(T candidate)
    {
        return this.predicate.Invoke(candidate);
    }
}

通过此实现,我只需在构造函数中传递一个谓词,如下所示:

var specification = new Specification<SomeDomainClass>(x => x.SomeDomainBoolMethod());

我的业务对象中有多个 bool 方法,而不是多个类(我的域中的每个条件一个)。

I'm using the Specification Pattern too. This is a basic implementation of it.

public class Specification<T, E> : ISpecification<T, E>
{
    private Predicate<T> predicate;

    public Specification(Predicate<T> predicate)
    {
        this.predicate = predicate;
    }

    public bool IsSatisfiedBy(T candidate)
    {
        return this.predicate.Invoke(candidate);
    }
}

With this implementation, I just pass a predicate in the constructor, like this:

var specification = new Specification<SomeDomainClass>(x => x.SomeDomainBoolMethod());

Instead of several classes (one per each condition in my domain), I have several bool methods in my business objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文