title: Implement WCF Data Service using the Repository Pattern
body: <p>We are using the repository pattern in our ASP.NET MVC 3 application. This means that, although we use EF 4.1 Code First to access the data in the backend, all MVC controllers do that via a generic repository class rather than directly over the DbContext subclass.</p>
  
  <p>Simplified code snippet:</p>
  
  <pre><code>public class MyEntityContext : DbContext, IMyEntityContext
  {
      public IDbSet MyEntities { get; set; }
      ...
  }
  
  public class MyEntityRepository : IMyEntityRepository
  {
      private IMyEntityContext _context;
  
      public IQueryable&lt;MyEntity&gt; MyEntities
      {
          return _context.MyEntities;
      }
      ...
  }
  
  public class MyEntityController : Controller
  {
      private MyEntityRepository _repository;
      ...
  }
  </code></pre>
  
  <p>We use interfaces and dependency injection for every dependency. It works fine. Looks nice, doesn't it? But now for the caveat:</p>
  
  <p>We also provide a WCF Data Service (CTP supporting Code First) to access the entities. We want to use the repository in that service, too. But this seems tricky. When using the <code>MyEntityContext</code> directly, the service looks like this:</p>
  
  <pre><code>public class MyEntityService : DataService&lt;MyEntityContext&gt;
  {
      public static void InitializeService(DataServiceConfiguration config)
      {
          config.SetEntitySetAccessRule("MyEntities", EntitySetRights.All);
      }
  }
  </code></pre>
  
  <p>But when I try to replace the <code>MyEntityContext</code> by the repository, there are two issues:</p>
  
  <ol>
  <li>The type specified for the generic <code>DataService&lt;..&gt;</code> needs to be a class with a default constructor, which breaks the pretty design-by-contract and dependency injection design.</li>
  <li>It even seems that the type provided has to be a <code>DbContext</code> class: I tried and used the <code>MyEntityRepository</code> instead, but failed (see details).</li>
  </ol>
  
  <p>I seem lost... Can anyone bring me back on the proper track?</p>
  
  <hr>
  
  <p>Details:</p>
  
  <p>My first go was:</p>
  
  <pre><code>public class MyEntityService : DataService&lt;MyEntityRepository&gt;
  {
      ...
  </code></pre>
  
  <p>However, when calling the service, it fails with the following error message:</p>
  
  <blockquote>
    <p>The server encountered an error processing the request. The exception message is 'On data context type 'MyEntityRepository', there is a top IQueryable property 'MyEntities' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.'.</p>
  </blockquote>
  
  <p>I tried the following steps to fix this, but did not get rid of this error message:</p>
  
  <ul>
  <li>Adding a <code>[DataServiceKey("MyEntityId")]</code> to MyEntity, where <em>MyEntityId</em> is the correct key property of the entity.</li>
  <li>Replacing the type of <code>Repository.MyEntities</code> by <code>IDbSet</code> instead of <code>IQueryable</code>.</li>
  </ul>
  
  <hr>
  
  <p>BTW: The following posts are <strong><em>not</em></strong> duplicates:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3312071/wcf-repository-service-pattern-with-entity-framework">WCF Repository Service pattern with entity framework</a></li>
  <li><a href="https://stackoverflow.com/questions/5164683/service-layer-repository-pattern">Service Layer/Repository Pattern</a></li>
  <li><a href="http://Best%20way%20to%20implement%20Repository%20Pattern?" rel="nofollow noreferrer">Best way to implement Repository Pattern?</a></li>
  <li><a href="https://stackoverflow.com/questions/704216/webservices-with-repository-pattern-in-c-and-wcf">webservices with repository pattern in c# and WCF?</a></li>
  <li><a href="https://stackoverflow.com/questions/5996529/wcf-service-design-pattern">WCF Service design pattern</a></li>
  </ul>