How to remove references to Entity Framework in other layers

c# dependency-injection entity-framework entity-framework-6

Question

I have an application with 3 layers:

  • UI
  • Domain Services
  • Data

The Data layer uses EF 6 (code first) to connect to the database.

I've create an interface which my DbContext implements:

public interface IMyDbContext
{
    DbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
    void Dispose();
}

public class MyDbContext : DbContext, IMyDbContext
{
    //...
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Module> Modules { get; set; }
    public DbSet<User> Users { get; set; }
    //...
}

I want to remove the references to EF from the Domain Services layer, that's why I inject this Interface into the Domain Services layer using Dependency Injection.

However the DbSet class is described in the EF binaries, so this is not going to work.

I'd like to use this abstraction in stead of the actual EF implementation, but I'm stuck. I've tried using IQueryable in stead of DbSet, but this didn't work.

I don't want to use (Generic) Repositories, but I want to re-use the DbSet and DbContext functionality of EF in my domain logic.

1
1
3/30/2015 7:32:45 AM

Popular Answer

What you are looking for is Unit of Work pattern. It will help you to prevent leaking data logic to the domain layer. Here is a nice tutorial on that.

3
3/25/2015 7:52:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow