告诉我我的实体验证有什么愚蠢的地方(以及如何改进它)
我有一个实现接口 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议查看 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.
请记住,如果您从另一个实体派生一个实体,例如。来自 Person 的客户,您需要一个 foeach 来获取基类违规:
这不太好。
就我个人而言,我也会寻找现有的东西,因为验证是一个普遍问题,并且已经有很多解决方案。
Keep in mind, if you derive an entity from another, eg. Customer from Person, you need a foeach to get the base classes violations:
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.
我正在使用 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 overrideFormatErrorMessage
so that I can return the errors collectively.