title: How to properly separate model validation from Controller into Service?
body: <p>I'm in the middle of re-factoring the project I'm working on. In my existing controllers I do use the repository pattern, but I was still performing a little too much scaffolding than I felt comfortable with. That and some of my controllers could have 10+ repositories passed in (via Ninject). So, I decided to introduce a service layer where my intention is to have one service per controller and each service will instead have the multiple repositories injected into it and do the work I need. This works great so far, but I'm running into a confusion of sorts: How do I move the model validation away from the controller and into the service layer?</p>
  
  <p>For example, take a look this <code>Edit</code> method on my <code>OfficesController</code>:</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;RedirectToRouteResult&gt; Edit(
      short id,
      FormCollection form,
      [Bind(Prefix = "Office.Coordinates", Include = "Latitude,Longitude")] Coordinate[] coordinates) {
      if (id &gt; 0) {
          Office office = await this.OfficesService.GetOfficeAsync(id);
  
          if ((office != null)
              &amp;&amp; base.TryUpdateModel(office, "Office", new string[2] {
                  "Name",
                  "RegionId"
              }, form)
              &amp;&amp; base.ModelState.IsValid) {
              this.OfficesService.UpdateOfficeAsync(office, coordinates);
          }
  
          return base.RedirectToAction("Edit", new {
              id = id
          });
      }
  
      return base.RedirectToAction("Default");
  }
  </code></pre>
  
  <p>The problem with it in comparison to the methods of the controller is that I still grab an <code>Office</code> object from the database, do the update, validate it, and then save it again. In this case the complexity increased rather than decrease. Before, I called the repository in the method, now I call the service which calls the repository to perform the same function. So far this increase in complexity has only show it self in my <code>Edit</code> methods, everywhere else the complexity decreased substantially, which is what I want.</p>
  
  <p>So, what would be a proper way move validation, and now that I think about it, the model updating logic out of the controller and into the service? Recommendations are appreciated!</p>
  
  <p>For reference, here's how my project is structured:</p>
  
  <ul>
  <li><strong>Data</strong>: Contains all of my model classes</li>
  <li><strong>Data.Google.Maps</strong>: Contains all classes I need to deserialize a specific Kml</li>
  <li><strong>Data.Models</strong>: Contains my DbContext, configurations, view models and partial view models</li>
  <li><strong>Data.Repositories</strong>: Contains all of my repositories that talk to the DbContext. Since EF is a pseudo repository on it's own, I'm leveraging my "repositories" as a more specific way of querying for data. For Example: <code>FindTechnicians()</code> or <code>FindActive()</code>, etc.</li>
  <li><strong>Data.Services</strong>: Contains all of the services I will use. The services will have one or more repository injected into them and perform all of logic I need done before I pass a completed view model back to the controller.</li>
  <li><strong>Identity</strong>: Contains my implementation of ASP.NET Identity.</li>
  <li><strong>Web.Private</strong>: Contains the actual MVC project.</li>
  </ul>