Repository pattern should I use ObjectSet<T> vs IDbSet<T> for base class?

I'm trying to create a base repository class based off the MyFinance example that uses Entity Framework Code first. I'd like to change it to use just Entity Framework. The sample uses IDbSet, but I have no idea what to change it to for just vanilla Entity Framework. Maybe ObjectSet<T>? When I use ObjectSet<T> I lose some of the methods such as GetById so maybe that isn't 100% right. Any help would be greatly appreciated.

Sample repository base using Entity Framework code first

public abstract class RepositoryBase<T> where T : class
{
private MyFinanceContext dataContext;
private readonly IDbSet<T> dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
    DatabaseFactory = databaseFactory;
    dbset = DataContext.Set<T>();
}

protected IDatabaseFactory DatabaseFactory
{
    get; private set;
}

protected MyFinanceContext DataContext
{
    get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
}
public virtual void Add(T entity)
{
    dbset.Add(entity);           
}
public virtual void Update(T entity)
{
    dbset.Attach(entity);
    dataContext.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
    dbset.Remove(entity);           
}
public virtual void Delete(Expression<Func<T, bool>> where)
{
    IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
    foreach (T obj in objects)
        dbset.Remove(obj);
} 
public virtual T GetById(long id)
{
    return dbset.Find(id);
}
public virtual T GetById(string id)
{
    return dbset.Find(id);
}
public virtual IEnumerable<T> GetAll()
{
    return dbset.ToList();
}
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
    return dbset.Where(where).ToList();
}
public T Get(Expression<Func<T, bool>> where)
{
    return dbset.Where(where).FirstOrDefault<T>();
} 

}

What I think I should change it to for EntityFramework

public abstract class BaseRepository<T> where T : class
{
    private MyDBContainer _dataContext;
    private readonly ObjectSet<T> dbset;

    protected BaseRepository(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        dbset = DataContext.CreateObjectSet<T>();
    }

    protected IDatabaseFactory DatabaseFactory
    {
        get;
        private set;
    }

    protected MyDBContainer DataContext
    {
        get { return _dataContext ?? (_dataContext = DatabaseFactory.DataContext); }
    }
    public virtual void Add(T entity)
    {
        dbset.AddObject(entity);
    }
    //public virtual void Update(T entity)
    //{
    //    dbset.Attach(entity);
    //    _dataContext.Entry(entity).State = EntityState.Modified;
    //}
    public virtual void Delete(T entity)
    {
        dbset.DeleteObject(entity);
    }
    public virtual void Delete(Expression<Func<T, bool>> where)
    {
        IEnumerable<T> objects = dbset.Where<T>(where).AsEnumerable();
        foreach (T obj in objects)
            dbset.DeleteObject(obj);
    }
    //public virtual T GetById(long id)
    //{
    //    return dbset.(id);
    //}
    //public virtual T GetById(string id)
    //{
    //    return dbset.Find(id);
    //}
    public virtual IEnumerable<T> GetAll()
    {
        return dbset.ToList();
    }
    public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).ToList();
    }
    public T Get(Expression<Func<T, bool>> where)
    {
        return dbset.Where(where).FirstOrDefault<T>();
    }

}

Answers


ObjectSet<T> is the precursor to DbSet<T> and it's associated interface IDbSet<T>. However there isn't as I'm sure you've discovered an IObjectSet<T>, but you are correct that you should use ObjectSet<T> in place of DbSet<T>.


Need Your Help

Why is this linq query with anonymous type faster than anything else I try?

c# sql performance linq

I have this query, it selects A LOT of user records from a table. This code block takes 16 seconds from my local/debug machine (more like 5 in production). Anything I do to make this more efficient

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.