title: View and Domain model, where to perform calculation
body: <p>I just started using View &amp; Domain model design in my MVC web app but got the question where to perform calculations and other View related actions. I will try to give example below.</p>
  
  <p>My Domain Model (Linq2Sql)</p>
  
  <pre><code>public class Product
  {
    public int Id;
    public string Name;
  }
  </code></pre>
  
  <p>The View Model with new <code>UserCount</code> property which I would like to calculate.</p>
  
  <pre><code>public class ProductViewModel
  {
    public int Id;
    public string Name;
    public int UserCount;
  }
  </code></pre>
  
  <p>My controller action looks like</p>
  
  <pre><code>public ActionResult _SelectionClientSide_Products()
  {
    IQueryable&lt;Product&gt; products = _repository.GetProducts(true); 
    var  model = Mapper.Map&lt;IEnumerable&lt;Product&gt;, IEnumerable&lt;ProductViewModel&gt;&gt;(products);                                
    return View(model);
  }
  </code></pre>
  
  <p>I query for data using repository method and get <code>IQueryable&lt;Product&gt;</code> and map it to <code>ProductViewModel</code> list. But I also need to perform another query operation to count users for  every queried product and assign all values to ProductViewModel. What design I should follow to achieve this?</p>
  
  <p>The relationship between tables </p>
  
  <pre><code>Products -&gt; Orders - &gt; Users
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I have decided to remove AutoMapper because it gives more problems than benefits and created my own Builder which contain everything what I need. I make field assign and also add calculation. </p>
  
  <pre><code>    public ActionResult _SelectionClientSide_Products()
      {        
        Data = new ProductViewModelBuilder(_repository).Build();
        return View(Data);
      }
  
  namespace PC.Models
  {
              public class ProductViewModel
              {
                  public int Id { get; set; }
                  public string Name { get; set; }
                  public int UsersCount { get; set; }
              }
  
              public class ProductViewModelBuilder
               {
                   private readonly IDataAccess _repository;
  
                   public ProductViewModelBuilder(IDataAccess repository)
                   {
                       _repository = repository;
                   }
  
                   public IQueryable&lt;ProductViewModel&gt; Build()
                   {
                       return _repository.GetProducts().Select(p=&gt; new ProductViewModel
                                                                           {
                                                                               Id = p.Id, 
                                                                               Name = p.Name,
                                                                               UsersCount = _repository.CountUsers(p.Id)
                                                                           });         
                   }
  
              }
          }
  </code></pre>