title: entity framework programming against interface and repository pattern
body: <p>I am developing in ASP.NET MVC3 and EntityFramework. </p>
  
  <p>I want my model to follow an interface : </p>
  
  <pre><code>public class Account : IAccount
  {
      public string Id { get; set; }
  
      public DateTime Date { get; set; }
  
      public string Language { get; set; }
  }
  
  public interface IAccount
  {
      string Id { get; set; }
  
      DateTime Date { get; set; }
  
      string Language { get; set; }
  }
  </code></pre>
  
  <p>Here's my Context </p>
  
  <pre><code>public class EFContext : DbContext, IContext
  {
      public DbSet&lt;Account&gt; Accounts { get; set; }
  }
  </code></pre>
  
  <p>And here's the repository : </p>
  
  <pre><code>public interface IRepository&lt;T&gt; where T : class
  {
      IQueryable&lt;T&gt; All { get; }
      int Count { get; }
  
      bool Contains(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
  
      void Create(T item);
  
      void Update(T item);
  
      void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; predicate);
      void Delete(T item);
  }
  
  public class EFRepository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {
      private EFContext _context;
  
      public EFRepository(IUnitOfWork uow)
      {
          this._context = (EFContext)uow.Context;
      }
  
      protected DbSet&lt;T&gt; DbSet
      {
          get
          {
              return _context.Set&lt;T&gt;();
          }
      }
  
      public IQueryable&lt;T&gt; All
      {
          get
          {
              return DbSet.AsQueryable();
          }
      }
  
      public virtual int Count
      {
          get
          {
              return DbSet.Count();
          }
      }
  
      public bool Contains(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          return DbSet.Count(predicate) &gt; 0;
      }
  
      public virtual void Create(T item)
      {
          DbSet.Add(item);
      }
  
      public virtual void Update(T TObject)
      {
          var item = DbSet.Attach(TObject);
  
          _context.SetItemState(TObject, EntityState.Modified);
      }
  
      public virtual void Delete(Expression&lt;Func&lt;T, bool&gt;&gt; predicate)
      {
          var objects = DbSet.Where(predicate);
  
          foreach (var obj in objects)
          {
              DbSet.Remove(obj);
          }
      }
  
      public virtual void Delete(T TObject)
      {
          DbSet.Remove(TObject);
      }
  }
  </code></pre>
  
  <p>Now, I want to use <code>IRepository&lt;IAccount&gt;</code> but this will ask the context for <code>DbSet&lt;IAccount&gt;</code>. This leads to an error since the Context contains a <code>DbSet&lt;Account&gt;</code>.</p>
  
  <p>I then tried the solution proposed here for Linq2Sql : <a href="http://iridescence.no/post/Linq-to-Sql-Programming-Against-an-Interface-and-the-Repository-Pattern.aspx" rel="nofollow">http://iridescence.no/post/Linq-to-Sql-Programming-Against-an-Interface-and-the-Repository-Pattern.aspx</a></p>
  
  <p>So I added this function to my EFContext</p>
  
  <pre><code>public new DbSet&lt;T&gt; Set&lt;T&gt;() where T : class
  {
      var ciccio = TableMaps[typeof(T)];
      return (DbSet&lt;T&gt;)base.Set(ciccio).Cast&lt;T&gt;();
  }
  </code></pre>
  
  <p>But it doesn't work.</p>
  
  <p>Do anyone have a suggestion?
  Thx</p>