title: Rules Validation on Data or Domain layer?
body: <p>I've been reading Pro ASP.NET MVC Framework, Steven Sanderson, and in chapter 11 it discusses data validation.</p>
  
  <p>On page 390 we see the section <em>Moving Validation Logic into Your Model Layer</em>. In this section we see, in page 392, some code showing how to implement validation.</p>
  
  <p>The code implements a <code>GetRuleViolations()</code> method and the <code>Save()</code> method uses it to throw a <code>RuleException</code> if something was not alright.</p>
  
  <p>It looks to me, however, that there's <strong>no distinction between the Domain Layer and a Data Access Layer</strong>, here's the code:</p>
  
  <pre><code>public void Save() {
      var errors = GetRuleViolations();
      if (errors.Count &gt; 0)
          throw new RuleException(errors);
  
      // Todo: Now actually save to the database or whatever
  }
  private NameValueCollection GetRuleViolations() {
      // validations...
  }
  </code></pre>
  
  <p>In a project I'm working, I have a <strong>Domain layer</strong>, as persistence-ignorant as possible, and a <strong>Data Access layer</strong>, implementing data access through NHibernate, and implementing the repositories which interfaces were defined in the Domain layer.</p>
  
  <p>If I implement the validation rules as the author proposes here, on the "<code>Save()</code>" method, they would go on my Data Access layer, although, at least I think, they should reside on the domain model!</p>
  
  <p><strong>So, my question is</strong>: when creating a <strong>layered application</strong>, with a <strong>Domain layer</strong> implementing the domain entities and exposing interfaces to repositories (persistence ignorant), a <strong>Data Access layer</strong> implementing the repositories from the domain layer and implementing all the data access code, <strong><em>where should the validation rules reside</em></strong>?</p>
  
  <p>My primary (or at least <em>first</em>) interface will be an ASP.NET MVC application, if that might change anything.</p>
  
  <p>Thanks.</p>