4
我正在嘗試創建一個基於MyFinance
示例的基礎知識庫類,該示例首先使用實體框架代碼。我想改變它只使用實體框架。該示例使用IDbSet
,但我不知道要將其更改爲僅適用於vanilla Entity Framework。也許ObjectSet<T>
?當我使用ObjectSet<T>
時,我丟失了一些方法,如GetById
,所以也許這不是100%正確。任何幫助將不勝感激。對於基類,我應該使用ObjectSet <T>還是IDbSet <T>存儲庫模式?
使用實體框架代碼第一個樣本庫的基礎
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>();
}
}
作爲您正在做的事情的一個便箋,我強烈建議您不要將這些存儲庫傳遞給您的服務層。我會將它們的所有用法都包含在PeopleService等可以使用1到多個存儲庫的東西中。我也不會從BaseRepository繼承,這個類可以作爲一流的公民居住。然後,您可以使用StructureMap等IOC框架將它們連接在一起。我寫了一篇關於用NH做這個相同概念的博客:http://dotnetchris.wordpress.com/2010/03/15/creating-a-common-generic-and-extensible-nhiberate-repository-version-2/ –
你的更新和GetById呢?你的Get似乎有這個邏輯。更新怎麼樣? – Mukus