Note that there are some explanatory texts on larger screens.

plurals
  1. POTell me what is stupid about my Entity Validation (and how to improve it)
    primarykey
    data
    text
    <p>I have an IEntity interface that implements an interface, IValidatable</p> <pre><code>public interface IValidatable { bool IsValid { get; } bool IsValidForPersistence { get; } // Rules applied at UI time (please enter your name, etc) IEnumerable&lt;RuleViolation&gt; GetRuleViolations(); // Rules to be applied only at persistence time IEnumerable&lt;RuleViolation&gt; GetPersistenceRuleViolations(); } public interface IEntity : IValidatable { int ID { get; set; } } </code></pre> <p>and for convenience sake I've implemented my Entity class like:</p> <pre><code> 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&lt;RuleViolation&gt; GetRuleViolations() { return new List&lt;RuleViolation&gt;(); } public virtual IEnumerable&lt;RuleViolation&gt; GetPersistenceRuleViolations() { return new List&lt;RuleViolation&gt;(); } } </code></pre> <p>By default entities are valid, until GetRuleViolations() or GetPersistenceRuleViolations() are overridden.</p> <pre><code> public partial class Company { public override IEnumerable&lt;RuleViolation&gt; GetRuleViolations() { if (String.IsNullOrEmpty(CompanyName)) yield return new RuleViolation("CompanyName", "Name is required."); } public override IEnumerable&lt;RuleViolation&gt; GetPersistenceRuleViolations() { // Include standard rules too foreach (RuleViolation rule in RuleViolations) { yield return rule; } // Check some data based on a referenced entity "Bid" if (!Active &amp;&amp; Bid.Active) yield return new RuleViolation("Active", "When Active is set to false, the Bid must also be inactive."); } } </code></pre> <p>I know this is a bit naive for validation, so besides any typos, what can be improved?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload