title: Using View-Models with Repository pattern
body: <p>I'm using <a href="http://blogs.msdn.com/b/marblogging/archive/2011/05/23/domain-drive-design-n-layered-net-4-0-architecture-guide.aspx?Redirected=true" rel="noreferrer">Domain driven N-layered application architecture</a> with <code>EF code first</code> in my recent project, I defined my <code>Repository</code> contracts, In <code>Domain</code> layer.
  A basic contract to make other <code>Repositories</code> less verbose:</p>
  
  <pre><code>public interface IRepository&lt;TEntity, in TKey&gt; where TEntity : class
  {
     TEntity GetById(TKey id);
     void Create(TEntity entity);
     void Update(TEntity entity);
     void Delete(TEntity entity);
  }
  </code></pre>
  
  <p>And specialized <code>Repositories</code> per each <code>Aggregation root</code>, e.g:</p>
  
  <pre><code>public interface IOrderRepository : IRepository&lt;Order, int&gt;
  {
      IEnumerable&lt;Order&gt; FindAllOrders();
      IEnumerable&lt;Order&gt; Find(string text);
      //other methods that return Order aggregation root
  }
  </code></pre>
  
  <p>As you see, all of these methods depend on <code>Domain entities</code>. 
  But in some cases, an application's <code>UI</code>, needs some data that isn't <code>Entity</code>, that data may made from two or more enteritis's data(<code>View-Model</code>s), in these cases, I define the <code>View-Model</code>s in <code>Application layer</code>, because they are closely depend on an <code>Application's</code> needs and not to the <code>Domain</code>.</p>
  
  <p>So, I think I have 2 way's to show data as <code>View-Models</code> in the <code>UI</code>:</p>
  
  <ol>
  <li>Leave the specialized <code>Repository</code> depends on <code>Entities</code> only, and map the results of <code>Repositories</code>'s method to <code>View-Models</code> when I want to show to user(in <code>Application Layer</code> usually).</li>
  <li>Add some methods to my specialized <code>Repositories</code> that return their results as <code>View-Models</code> directly, and use these returned values, in <code>Application Layer</code> and then <code>UI</code>(these specialized <code>Repositories</code>'s contracts that I call them <code>Readonly Repository Contract</code>s, put in <code>Application Layer</code> unlike the other <code>Repositories</code>'e contract that put in <code>Domain</code>).
  <img src="https://i.stack.imgur.com/ZLxvi.jpg" alt="enter image description here"></li>
  </ol>
  
  <p>Suppose, my <code>UI</code> needs a <code>View-Model</code> with 3 or 4 properties(from 3 or 4 <strong>big</strong> <code>Entities</code>).
  It's data could be generate with simple projection, but in case 1, because my methods could not access to <code>View-Models</code>, I have to <strong>fetch all the fields of all 3 or 4 tables</strong> with  sometimes, huge joins, and then map the results to <code>View-Models</code>.
  But, in case 2, I could simply use projection and fill the <code>View-Model</code>s directly.</p>
  
  <p>So, I think in performance point of view, the case 2 is better than case 1. but I read that <code>Repository</code> should depend on <code>Entities</code> and not <code>View-Models</code> in design point of view.</p>
  
  <p>Is there any better way that does not cause the <code>Domain</code> Layer depend on the <code>Application layer</code>, and also doesn't hit the performance? or is it acceptable that for reading queries, my <code>Repositories</code> depend on <code>View-Models</code>?(case2)</p>