title: Using Service layer classes in ViewModels. A Design Flaw?
body: <p>I am wondering if I have a design flaw in my solution. Here's what I have:  </p>
  
  <ul>
  <li><strong><code>Entities</code></strong> => pure poco. refs: none.</li>
  <li><strong><code>Data</code></strong> => data access. refs: <code>Entities</code>.</li>
  <li><strong><code>Service</code></strong> => business logic. refs: <code>Entities</code> and <code>Data</code>
  <ul>
  <li>Manager Classes.</li>
  <li>ViewModels.</li>
  </ul></li>
  <li><strong><code>WebApp</code></strong> => UI. Refs: <code>Entities</code> and <code>Service</code></li>
  </ul>
  
  <p><code>WebApp</code> ASP.NET MVC project as UI, <code>Entities</code> project with no reference for holding pure POCO. <code>Data</code> to access database and <code>Service</code> for Business Logic (where my Manager classes are).  </p>
  
  <p>Basically, I have defined a <code>Manager</code> class for every entity. For exmaple, I have a <code>Message</code> entity with relation to a list of <code>Recipient</code> entity. I have a <code>MessageManager</code> and <code>RecipientManager</code> class, responsible for both CRUD operations using Data layer and logical results (e.g. <code>public List&lt;Message&gt; GetAllMessagesWithPermissionForUser(User user, Permission permission)</code>)</p>
  
  <p>For my MVC project I have defined some ViewModel classes in Service layer to generate specific videmodels for my views. Since the viewmodels are using Manager classes, I have defined them in my Service Class. For example I have a <code>MessageOperationVM</code> viewmodel which has a <code>PermittedBoxesToSend</code> property. This property uses my <code>BoxManager</code> class to get all boxes permitted for the specified message:  </p>
  
  <pre><code>// Initialized by Catsle Windsor.
  public BoxManager BoxManager {get; set;}
  
  public List&lt;Box&gt; PermittedBoxesToSend 
  {
      if(this._premittedBoxesToSend != null)
      {
          this._permittedBoxesToSend = BoxManager.GetPermittedBoxesToSend(this.Message);
      }
  }
  </code></pre>
  
  <ul>
  <li><p>I am not sure if using manager classes in Viewmodels are a good design. Although I've defined them as constructor/property setters to get populated with DI. Should I populate my viewmodels' properties in my controllers instead of defining properties and get rid of manager classes in my ViewModels?  </p>
  
  <pre><code>public ActionResult ShowNewMessageDialog()
  { 
      var messageVM = new MessageOperationVM() { new Message() };
      messageVM = this.BoxManager.GetPermittedBoxesToSend();
  }
  </code></pre></li>
  <li><p>using a manager class for each entity seems to make maintenance hard. (Although they all derive from a <code>BaseManager</code> class which shares the operations they have in common)  </p></li>
  <li>Is there any reconsideration worth mentioning in the above design?  </li>
  </ul>
  
  <p>Thank you.</p>
  
  <p><strong>Update</strong>:<br>
  based on eulerfx's answer:<br>
  The problem I have with your answer is: To construct a ViewModel, I have to call some service layer's methods. So I cannot construct my ViewModel just based on my poco entity. Do you suggest that I should construct those parts in controller as well? :  </p>
  
  <pre><code>public ActionResult ShowNewMessageDialog()
  {
      var message = this.messageRepository.GetMessage();
      var messageVM = new MessageViewModel(message);
      messageVM.CustomProperty = this.messageManager.CallSomeMethod(message);
      return View(messageVM);
  }
  </code></pre>