title: Building Validation Service Layer
body: <p>I'm looking for a tutorial on how to build validation service layer. I want the layer to be present inside my 'domain' assembly.  At the moment I've got my domain model and (maybe not the best) generic repository implementation. </p>
  
  <p>My repository implementation is next:</p>
  
  <pre><code>public sealed class Repository&lt;T&gt; : Interface.IRepository&lt;T&gt; where T : Entity&lt;T&gt;
  {
      private ISessionFactory sessionFactory;        
  
      public Repository(ISessionFactory sessionFactory)
      {                      
          this.sessionFactory = sessionFactory;
      }
  
      public T Get(Guid id)
      {
          using(var session = this.sessionFactory.OpenSession())
          {
              return session.Get&lt;T&gt;(id);
          }
      }
      public IQueryable&lt;T&gt; Get(Expression&lt;Func&lt;T, Boolean&gt;&gt; predicate)
      {
          using(var session = this.sessionFactory.OpenSession())
          {
              return session.Query&lt;T&gt;().Where(predicate);
          }
      }
      public IQueryable&lt;T&gt; All()
      {
          using(var session = this.sessionFactory.OpenSession())
          {
              return session.Query&lt;T&gt;();
          }
      }
      public void Add(T entity)
      {
          // execute validation here?
  
          using(var session = this.sessionFactory.OpenSession())
          using(var transaction = session.BeginTransaction())
          {
              session.Save(entity);
              transaction.Commit();
          }
      }
      public void Remove(T entity)
      {
          using(var session = this.sessionFactory.OpenSession())
          using(var transaction = session.BeginTransaction())
          {
              session.Delete(entity);
              transaction.Commit();
          }
      }
      public void Update(T entity)
      {
          // make changes &amp;
          // execute validation here?
  
          using(var session = this.sessionFactory.OpenSession())
          using(var transaction = session.BeginTransaction())
          {                
              session.Update(entity);
              transaction.Commit();
          }
      }
  } 
  </code></pre>
  
  <p>I wanted to use <code>FluentValidation</code> to validate entities. As far as I understand within scope of repository there are only two places where validation should be made: when adding and updating an entity.</p>
  
  <p>First I thought to put <code>IValidator&lt;T&gt;</code> as a parameter to base entity class:</p>
  
  <pre><code>public abstract class Entity&lt;T&gt; where T : Entity&lt;T&gt;
  {
      // ...        
      public virtual String ValidationMessage
      {
          get;
          private set;
      }
  
      public virtual Boolean Validate(IValidator&lt;T&gt; validator)
      {
          try
          {
              validator.ValidateAndThrow(this as T);
              return true;
          }
          catch(ValidationException ex)
          {
              this.ValidationMessage = ex.Message;
              return false;
          }
      } 
      // ...      
  } 
  </code></pre>
  
  <p>But it doesn't seem to be right. </p>
  
  <p>How do I make this right from design point of view? Any advice or tutorial is appreciated.</p>
  
  <p>Thanks!</p>