How to remove references to Entity Framework in other layers

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

Question

I have a program with three layers:

  • UI
  • Registrar Services
  • Data

The Data layer connects to the database using EF 6 (code first).

My DbContext implements an interface that I created:

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 use Dependency Injection to inject this Interface into the Domain Services layer since I want to get rid of the references to EF there.

This won't work since the EF binaries contain a description of the DbSet class.

I want to replace the actual EF implementation with this abstraction, but I'm at a loss. Instead of DbSet, I tried using IQueryable, but it didn't work.

I want to reuse the DbSet and DbContext features of EF in my domain logic instead of using (Generic) Repositories.

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

Popular Answer

The object you seek isUnit of Work pattern. It will assist you in avoiding data logic leakage to the domain layer. An excellent tutorial on that is Here.

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