title: Validate object based on external factors (ie. data store uniqueness)
body: <h2>Description</h2>
  
  <p>My solution has these projects:</p>
  
  <ul>
  <li><strong>DAL</strong> = Modified Entity Framework</li>
  <li><strong>DTO</strong> = Data Transfer objects that are able to validate themselves</li>
  <li><strong>BL</strong> = Business Layer Services</li>
  <li><strong>WEB</strong> = presentation Asp.net MVC application</li>
  </ul>
  
  <p>DAL, BL and WEB all reference DTO which is great.<br>
  The process usually executes this way:</p>
  
  <ol>
  <li>A web request is made to the WEB</li>
  <li>WEB gets DTOs posted
  
  <ul>
  <li>DTOs get automagically validated via custom ActionFilter</li>
  <li>validation errors are auto-collected</li>
  </ul></li>
  <li>(Validation is OK) WEB calls into BL providing DTOs</li>
  <li>BL calls into DAL by using DTOs (can either pass them through or just use them)</li>
  </ol>
  
  <h2>DTO Validation problem then...</h2>
  
  <p>My DTOs are able to validate themselves based on their own state (properties' values). But right now I'm presented with a problem when this is not the case. I need them to validate using BL (and consequently DAL).</p>
  
  <p><em>My real-life example</em>: User registers and WEB gets a User DTO that gets validated. The problematic part is <code>username</code> validation. Its uniqueness should be checked against data store.<br>
  <strong>How am I supposed to do this?</strong></p>
  
  <p>There's additional info that all DTOs implement an interface (ie. <code>User</code> DTO implements <code>IUser</code>) for IoC purposes and TDD. Both are part of the <em>DTO project</em>.</p>
  
  <h2>Impossible tries</h2>
  
  <ol>
  <li>I can't reference BL in DTO because I'll get circular reference.<br>
  <code>Compilation error</code></li>
  <li>I can't create an additional DTO.Val project that would reference partial DTO classes and implement their validation there (they'd reference BL + DTO).<br>
  <code>Partial classes can't span assemblies.</code></li>
  </ol>
  
  <h2>Possible tries</h2>
  
  <ol>
  <li>Create a special <code>ActionFilter</code> that would validate object against external conditions. This one would be created within <em>WEB project</em> thus seeing DTO and BL that would be used here.</li>
  <li>Put DTOs in BL and keep DTO interfaces as actual DTOs referenced by other projects and refactor all code to use interfaces instead of concrete classes.</li>
  <li>Don't handle external dependant validation and let external dependencies throw an exception - probably <strong>the worst</strong> solution to this issue</li>
  </ol>
  
  <h2>What would you suggest?</h2>