title: Asp.NET MVC - DataAnnotations and ModelState.IsValid too invasive into the domain model?
body: <p>I'm learning ASP.NET MVC from the book <a href="https://rads.stackoverflow.com/amzn/click/com/1430242361" rel="noreferrer" rel="nofollow noreferrer">Pro ASP.NET MVC 4</a> (which I love so far, by the way).</p>
  
  <p>I'm still in the beginning chapters and it's showing me the <code>System.ComponentModel.DataAnnotations</code> namespace attributes, how to sprinkle my model class with these annotations, and then how to use them to check if the model is valid (<code>ModelState.IsValid</code> in the <code>Controller</code>).</p>
  
  <p>For example:</p>
  
  <pre><code>public class GuestResponse
  {
      [Required(ErrorMessage = "Please enter your name"]
      public string Name { get; set; }
  }
  
  ...
  
  public ViewResult RsvpForm(GuestResponse guestResponse)
  {
      if(ModelState.IsValid)
      {
          return View("Thanks", guestResponse);
  
      }
  
  }
  </code></pre>
  
  <p>There are a couple things about this that make me uneasy.</p>
  
  <ol>
  <li>Why do I want a bunch of attributes littered throughout my domain model? I like my domain model pure and free from any stuff that is implementation specific, and any real world model would be too complex to just use declarative validation like this.</li>
  <li>Aren't the <code>ErrorMessage</code> parameters of the validation attributes somewhat <code>View</code> related? Doesn't something like that belong in the <code>UI</code> layer? For example...what if due to space constraints I want the mobile version to instead of saying "Please enter your name" say "Name required"? But here it is in my <em>model</em>!</li>
  <li>Why do I want to use <code>ModelState.IsValid</code> to determine the status of the model? Shouldn't the model tell me? I understand that <code>ModelState</code> is making use of the <code>DataAnnotations</code> attributes that <em>are</em> in my model, but this seems like it would only work for very simple models. A more complex model might not even have a valid/invalid state, it might just have various stages and states. I'm sort of rambling here, but I don't like the idea of declaratively saying what makes my model valid or invalid.</li>
  </ol>
  
  <p>Any advice, reassurance, or validation of these thoughts would be appreciated.</p>