告诉我我的实体验证有什么愚蠢的地方(以及如何改进它)

发布于 2024-08-07 05:30:26 字数 2052 浏览 4 评论 0原文

我有一个实现接口 IValidatable 的 IEntity 接口

public interface IValidatable {
    bool IsValid { get; }
    bool IsValidForPersistence { get; }
    // Rules applied at UI time (please enter your name, etc)
    IEnumerable<RuleViolation> GetRuleViolations();
    // Rules to be applied only at persistence time
    IEnumerable<RuleViolation> GetPersistenceRuleViolations();
}

public interface IEntity : IValidatable {
    int ID { get; set; }
}

,为了方便起见,我实现了我的 Entity 类,如下所示:

 public abstract class Entity : IEntity {

        public virtual int ID { get; set; }

        public virtual bool IsValid {
            get { return RuleViolations().Count() == 0; }
        }

        public virtual bool IsValidForPersistence {
            get { return PersistenceRuleViolations().Count() == 0; }
        }

        public virtual IEnumerable<RuleViolation> GetRuleViolations() {
            return new List<RuleViolation>();
        }

        public virtual IEnumerable<RuleViolation> GetPersistenceRuleViolations() {
            return new List<RuleViolation>();
        }
    }

默认情况下,实体是有效的,直到覆盖 GetRuleViolations() 或 GetPersistenceRuleViolations() 。

  public partial class Company {

      public override IEnumerable<RuleViolation> GetRuleViolations() {
         if (String.IsNullOrEmpty(CompanyName))
                yield return new RuleViolation("CompanyName", "Name is required.");
     }

      public override IEnumerable<RuleViolation> GetPersistenceRuleViolations() {
         // Include standard rules too
         foreach (RuleViolation rule in RuleViolations) {
              yield return rule;
          }
        // Check some data based on a referenced entity "Bid"
        if (!Active && Bid.Active)
            yield return new RuleViolation("Active", 
               "When Active is set to false, the Bid must also be inactive.");
     }
  }

我知道这对于验证来说有点天真,所以除了任何拼写错误之外,还有什么可以改进的呢?

I have an IEntity interface that implements an interface, IValidatable

public interface IValidatable {
    bool IsValid { get; }
    bool IsValidForPersistence { get; }
    // Rules applied at UI time (please enter your name, etc)
    IEnumerable<RuleViolation> GetRuleViolations();
    // Rules to be applied only at persistence time
    IEnumerable<RuleViolation> GetPersistenceRuleViolations();
}

public interface IEntity : IValidatable {
    int ID { get; set; }
}

and for convenience sake I've implemented my Entity class like:

 public abstract class Entity : IEntity {

        public virtual int ID { get; set; }

        public virtual bool IsValid {
            get { return RuleViolations().Count() == 0; }
        }

        public virtual bool IsValidForPersistence {
            get { return PersistenceRuleViolations().Count() == 0; }
        }

        public virtual IEnumerable<RuleViolation> GetRuleViolations() {
            return new List<RuleViolation>();
        }

        public virtual IEnumerable<RuleViolation> GetPersistenceRuleViolations() {
            return new List<RuleViolation>();
        }
    }

By default entities are valid, until GetRuleViolations() or GetPersistenceRuleViolations() are overridden.

  public partial class Company {

      public override IEnumerable<RuleViolation> GetRuleViolations() {
         if (String.IsNullOrEmpty(CompanyName))
                yield return new RuleViolation("CompanyName", "Name is required.");
     }

      public override IEnumerable<RuleViolation> GetPersistenceRuleViolations() {
         // Include standard rules too
         foreach (RuleViolation rule in RuleViolations) {
              yield return rule;
          }
        // Check some data based on a referenced entity "Bid"
        if (!Active && Bid.Active)
            yield return new RuleViolation("Active", 
               "When Active is set to false, the Bid must also be inactive.");
     }
  }

I know this is a bit naive for validation, so besides any typos, what can be improved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

万劫不复 2024-08-14 05:30:26

我建议查看 NCommon 框架,它具有良好的业务规则和验证基础框架,或者 xVal 验证框架,它也支持来自同一规则集的客户端验证。

I'd recommend looking at either the NCommon framework which has a good business rule and validation base framework, or the xVal validation framework, which also supports client side validation from the same ruleset.

猫瑾少女 2024-08-14 05:30:26

请记住,如果您从另一个实体派生一个实体,例如。来自 Person 的客户,您需要一个 foeach 来获取基类违规:

  public override IEnumerable<RuleViolation> GetRuleViolations() 
  {
     // inherit base class valiations
     foreach (var violation in base.GetRuleViolations())
     {
         yield return violation;
     }

     // add own validations
     if (String.IsNullOrEmpty(CompanyName))
         yield return new RuleViolation("CompanyName", "Name is required.");
  }

这不太好。

就我个人而言,我也会寻找现有的东西,因为验证是一个普遍问题,并且已经有很多解决方案。

Keep in mind, if you derive an entity from another, eg. Customer from Person, you need a foeach to get the base classes violations:

  public override IEnumerable<RuleViolation> GetRuleViolations() 
  {
     // inherit base class valiations
     foreach (var violation in base.GetRuleViolations())
     {
         yield return violation;
     }

     // add own validations
     if (String.IsNullOrEmpty(CompanyName))
         yield return new RuleViolation("CompanyName", "Name is required.");
  }

Which is not so nice.

Personally, I would also look for something existing, because validation is a general problem and there are already many solutions for it.

墨落画卷 2024-08-14 05:30:26

我正在使用 ASP.NET MVC 并使用数据注释验证器执行类似的操作。然而,我继承了 ValidationAttribute 并重写了 FormatErrorMessage ,以便我可以集中返回错误。

I'm using ASP.NET MVC and doing something similar with Data Annotation Validators. I inherit however from ValidationAttribute and also override FormatErrorMessage so that I can return the errors collectively.

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