title: Wrapping DbSet<TEntity> with a custom DbSet/IDbSet?
body: <p>First off, I think this is somewhat ridiculous to do but the other members of my team insist upon it and I can't come up with a good argument against it other than "I think it's dumb"... </p>
  
  <p>What we're trying to do is create a completely abstract data layer and then have various implementations of that data layer. Simple enough, right? Enter Entity Framework 4.1... </p>
  
  <p>Our end goal here is that the programmers (I do my best to stay only on the data layer) never want to have to be exposed to the concrete classes. They only ever want to have to use interfaces in their code, aside from obviously needing to instantiate the factory.</p>
  
  <p>I want to achieve something like the following:</p>
  
  <p>First we have our "Common" library of all of the interfaces, we'll call it "Common.Data":</p>
  
  <pre><code>public interface IEntity
  {
      int ID { get; set; }
  }
  
  public interface IUser : IEntity
  {
      int AccountID { get; set; }
      string Username { get; set; }
      string EmailAddress { get; set; }
      IAccount Account { get; set; }
  }
  
  public interface IAccount : IEntity
  {
      string FirstName { get; set; }
      string LastName { get; set; }
      DbSet&lt;IUser&gt; Users { get; set; } // OR IDbSet&lt;IUser&gt; OR [IDbSet implementation]?
  }
  
  public interface IEntityFactory
  {
      DbSet&lt;IUser&gt; Users { get; }
      DbSet&lt;IAccount&gt; Accounts { get; }
  }
  </code></pre>
  
  <p>From that we then have an implementation library, we'll call it "Something.Data.Imp":</p>
  
  <pre><code>internal class User : IUser
  {
      public int ID { get; set; }
      public string Username { get; set; }
      public string EmailAddress { get; set; }
      public IAccount Account { get; set; }
  
      public class Configuration : EntityTypeConfiguration&lt;User&gt;
      {
          public Configuration() : base()
          {
               ...
          }
      }
  }
  
  internal class Account : IAccount
  {
      public int ID { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public DbSet&lt;IUser&gt; Users { get; set; } // OR IDbSet&lt;IUser&gt; OR [IDbSet implementation]?
  
      public class Configuration : EntityTypeConfiguration&lt;Account&gt;
      {
          public Configuration() : base()
          {
               ...
          }
      }
  }
  </code></pre>
  
  <p>Factory:</p>
  
  <pre><code>public class ImplEntityFactory : IEntityFactory
  {
      private ImplEntityFactory(string connectionString) 
      {
          this.dataContext = new MyEfDbContext(connectionString);
      }
      private MyEfDbContext dataContext;
  
      public static ImplEntityFactory Instance(string connectionString)
      {
          if(ImplEntityFactory._instance == null)
              ImplEntityFactory._instance = new ImplEntityFactory(connectionString);
  
          return ImplEntityFactory._instance;
      }
      private static ImplEntityFactory _instance;
  
      public DbSet&lt;IUser&gt; Users // OR IDbSet&lt;IUser&gt; OR [IDbSet implementation]?
      { 
          get { return dataContext.Users; }
      }
  
      public DbSet&lt;IAccount&gt; Accounts // OR IDbSet&lt;IUser&gt; OR [IDbSet implementation]?
      {
          get { return dataContext.Accounts; }
      }
  }
  </code></pre>
  
  <p>Context:</p>
  
  <pre><code>public class MyEfDataContext : DbContext
  {
      public MyEfDataContext(string connectionString)
          : base(connectionString)
      {
          Database.SetInitializer&lt;MyEfDataContext&gt;(null);
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Configurations.Add(new User.Configuration());
          modelBuilder.Configurations.Add(new Account.Configuration());
          base.OnModelCreating(modelBuilder);
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Account&gt; Accounts { get; set; }
  }
  </code></pre>
  
  <p>Then the front-end programmers would be using it such as:</p>
  
  <pre><code>public class UsingIt
  {
      public static void Main(string[] args)
      {
          IEntityFactory factory = new ImplEntityFactory("SQLConnectionString");
          IUser user = factory.Users.Find(5);
          IAccount usersAccount = user.Account;
  
          IAccount account = factory.Accounts.Find(3);
          Console.Write(account.Users.Count());
      }
  }
  </code></pre>
  
  <p>So that's pretty much it... I'm hoping someone on here might be able to either point me in the right direction or help me out with a good argument that I can fire back at the development team. I've looked at some other articles on this site about EF not being able to work with interfaces and <a href="https://stackoverflow.com/questions/6227461/entity-framework-implementing-idbset">one reply</a> saying that you can't implement <code>IDbSet</code> (which I find kind of curious, why would they provide it if you couldn't implement it?) but so far to no avail.</p>
  
  <p>Thanks in advance for any help!
  J</p>