Abstract Entity Framework

c# entity-framework entity-framework-6

Question

In order to separate Entity Framework from the rest of my program, I want to provide an abstraction layer. However, I'm running into a couple issues with Entity Framework.

I basically divided my program into numerous projects like this (I don't show you all the interface layers I've made too):

  1. Domain
    • Contains an abstraction of my data storage object called my domain object.
  2. DAL
    • establishes a connection between my business layer and my data store. has two different kinds of elements:
      • Private ones include my EDMX, database object, and a few other produced objects that provide me with ToDomain and ToEntity methods.
      • Public ones include my Data Access Object, which offers CRUD operations.
  3. Business
    • Contains my application's logic. only comprehends the DAL's and the Domain Layer's open components.
  4. Presentation
    • presents the user with the domain objects. understands only the business layer.

As I previously stated, I wish to abstract my data storage objects (in my scenarioDatabase In order to keep my business layer in the dark about how my DAL implementation works, I need a solution that also works for other storage types (for example, file or WCF storage).

An example of my DAL work is given below:

public abstract class GenericDao<TEntity, TDomain, TDbContext> : IGenericDao<TDomain>
    where TDbContext : DbContext, new()
    where TEntity : class
    where TDomain : class
{
    protected TDbContext _context;
    protected DbSet<TEntity> _dbSet;

    public GenericDao(TDbContext dbContext)
    {
        this._context = dbContext;
        this._dbSet = dbContext.Set<TEntity>();
    }

    public TDomain Create()
    {
        return this.ToDomain(this._dbSet.Create());
    } 

    public IList<TDomain> GetAll()
    {
        return this._dbSet.ToList().Select(entity => this.ToDomain(entity)).ToList();
    }

    public void Update(TDomain domain)
    {
        var entity = this.ToEntity(domain);

        var entry = this._context.Entry(entity);

        entry.State = EntityState.Modified;
    }

    public void Remove(TDomain domain)
    {
        _dbSet.Remove(this.ToEntity(domain));
    }

    protected abstract TDomain ToDomain(TEntity entity);

    protected abstract TEntity ToEntity(TDomain domain);
}

Reading my code will probably show you what's wrong with it: whenever I try to remove or edit an entity, I'm not doing it with an entity that is connected to Entity Framework. Attempting to connect my entity to thedbContext because an entity with the same id already exists in the context, it fails.

I've already considered a number of solutions, but none of them appeal to me.

Maybe I'm approaching this the incorrect way. I'm not really clear on the Repository and DAO pattern (I read anything and the very opposite about that difference on the internet).

1
1
8/16/2015 7:35:44 PM

Accepted Answer

ZZZ_tmp
1
8/16/2015 3:20:57 PM

Popular Answer

ZZZ_tmp


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