title: How can I refactor my database access code outside my MVC project but keep my viewmodels inside?
body: <p>I have an asp.net-mvc website with the following folders:</p>
  
  <ul>
  <li>Controllers</li>
  <li>Scripts</li>
  <li>Views</li>
  <li>ViewModels</li>
  <li>Models</li>
  <li>DomainModel</li>
  </ul>
  
  <p>I now want to access a lot of this business logic and database access code and data in another .net app (a windows console app so not web at all) so i am refactoring <strong>to remove as much stuff as possible outside of the MVC project</strong> and into other projects in the solution so that code could be shared with this other solutions.  </p>
  
  <p>I have 2 main issues;</p>
  
  <ol>
  <li><p>My main issue is that I am struggling to find a place to put the code that generates the ViewModel because a lot of this code I would like to reuse in my console app as the console app sends email which requires the same data that is in the view. </p></li>
  <li><p>Another main issue is that i am struggling to see how i can move my database access code out of the MVC project while still having the ViewModels inside when many of my functions that instantiate my viewmodels start with a bunch of database access code.</p></li>
  </ol>
  
  <p>Here is a bit of the detail and my process so far:</p>
  
  <h2>Step 1 - Move DomainModel into another project - success</h2>
  
  <p>So moving the DomainModel project was simple (as that was a lot of raw objects with some business logic on top - nothing web about it).</p>
  
  <h2>Step 2 - Thin out controllers - success</h2>
  
  <p>I have thinned out as much of my controllers as possible and moved any business logic or complicated data access logic into the Models folder.  When i tried to move the models folder outside the MVC project a few things broke:</p>
  
  <h2>Step 3 - Attempt to move Models folder outside MVC Project - struggle</h2>
  
  <p>In thinning out the controllers, I have a number of different controller actions that go to a model class and return my ViewModel that i pass back into the view. Something like this (in my controller class):</p>
  
  <pre><code> public ActionResult ApplicationDetail(int id)
   {
        AppDetailViewModel applicationViewModel = Model.GenerateAppDetailViewModel(id);
        return View(applicationViewModel);
   }
  </code></pre>
  
  <p>So files in my Model folder are dependency on the ViewModel classes.  I do want to centralize the GenerateAppDetailViewModel() function as that is used in multiple different controllers.  Also, in my console app (which sends out email, i often want to get all the data that happens to be on some view so my code "wants" to leverage the viewmodel as well .. if i move it out of the MVC project then I can reuse but i think have the dependency issue (clearly i don't need SelectListItem in my console app but in other cases where they are just container objects of different data needed to generate a view I do want to reuse)</p>
  
  <p>or  another thing that broke was the dependency on:</p>
  
  <pre><code>System.Web.Mvc
  </code></pre>
  
  <p>because I have a lot of code that:</p>
  
  <ol>
  <li>queries a table in a database</li>
  <li>Converts that into a collection of objects (i am using nhibernate)</li>
  <li>Convert that into either some DTO object (which is sitting in ViewModels folder) or a List of SelectListItem objects (to be used to populate dropdowns in the view) which is part of System.web.mvc.</li>
  </ol>
  
  <p>I wanted to look for suggestions on the best way to break out this dependency so i can move as much code out of the MVC project as possible for reuse. </p>
  
  <p>The issue is that if i try to suck my ViewModel code into the Model folder and into another project then again i get stuck because the ViewModel classes have a lot of dependency on </p>
  
  <p>System.Web.Mvc </p>
  
  <p>due to things like SelectListItem.</p>
  
  <p>Should i have 2 view models folders (one in the MVC project that has specific system.web.mvc references and another one that sits in a different project?).  It seems like the dependency on SelectListItem is what keeps causing the contention</p>
  
  <p>In most examples that i have seen ViewModels do have a dependency on System.Web.Mvc <a href="http://www.em64t.net/2011/03/organizing-view-models-in-asp-net-mvc-part-3-of-3/" rel="nofollow noreferrer">such as this tutorial</a></p>
  
  <p>I have seen these questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2149855/asp-net-mvc-selectlist-refactoring-question">Asp.Net MVC SelectList Refactoring Question?</a></li>
  <li><a href="https://stackoverflow.com/questions/3746777/where-should-selectlist-logic-sit-in-asp-net-mvc-view-model-or-controller">Where should selectlist logic sit in ASP.NET MVC, view, model or controller?</a></li>
  </ul>
  
  <p>which are sort of related but not sure they answer my specific overall refactoring question stated.</p>