编程语言
首页 > 编程语言> > c# – 存储库模式我应该使用ObjectSet vs IDbSet作为基类吗?

c# – 存储库模式我应该使用ObjectSet vs IDbSet作为基类吗?

作者:互联网

我正在尝试基于首先使用实体​​框架代码的MyFinance示例创建基本存储库类.我想将其更改为仅使用Entity Framework.该示例使用IDbSet,但我不知道如何将其更改为仅仅为vanilla Entity Framework.也许ObjectSet< T>?当我使用ObjectSet< T>时我失去了一些像GetById这样的方法,所以也许这不是100%正确的.任何帮助将不胜感激.

首先使用Entity Framework代码示例存储库库

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>();
} 

}

我认为我应该将其更改为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>();
    }

}

解决方法:

对象集&LT T&GT是DbSet T的前体.并且它是相关的接口IDbSet< T>.但是没有,因为我确定你已经发现了IObjectSet< T>,但你应该使用ObjectSet< T>代替DbSet< T>.

标签:c,entity-framework,entity-framework-4,repository-pattern
来源: https://codeday.me/bug/20190709/1417485.html