想知道是否需要使用Genericrepository模式和UnitOfWork來模擬知識庫。我正在使用MOQ.Is現在是多餘的,因爲我注意到EF 4.1有IDBSet。UnitOfWork和GenericRepository模式是否冗餘首先在EF 4.1代碼中?
我還沒有想出如何寫一些通用的IDBSet.If你有一個例子,你實現IDBSet可以顯示給我嗎?
有什麼建議嗎?
想知道是否需要使用Genericrepository模式和UnitOfWork來模擬知識庫。我正在使用MOQ.Is現在是多餘的,因爲我注意到EF 4.1有IDBSet。UnitOfWork和GenericRepository模式是否冗餘首先在EF 4.1代碼中?
我還沒有想出如何寫一些通用的IDBSet.If你有一個例子,你實現IDBSet可以顯示給我嗎?
有什麼建議嗎?
這是重複的對SO已經討論了許多話題,但我同意,因爲他們是嵌套在其他問題
我希望這會給你一些答案。如果不是,請不要猶豫,在這裏或在一個新的問題上尋求更多的信息。
public class MockDbSet<T> : IDbSet<T> where T : class, new()
{
private List<T> _entities;
public MockDbSet(List<T> entities)
{
_entities = entities;
}
public virtual T Add(T entity)
{
_entities.Add(entity);
return entity;
}
public virtual T Attach(T entity)
{
_entities.Add(entity);
return entity;
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
{
return new T() as TDerivedEntity;
}
public virtual T Create()
{
return new T();
}
public virtual T Find(params object[] keyValues)
{
throw new NotImplementedException();
}
public System.Collections.ObjectModel.ObservableCollection<T> Local
{
get
{
return new ObservableCollection<T>(_entities);
}
}
public virtual T Remove(T entity)
{
_entities.Remove(entity);
return entity;
}
public IEnumerator<T> GetEnumerator()
{
return _entities.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return _entities.GetEnumerator();
}
public Type ElementType
{
get { return _entities.AsQueryable().ElementType; }
}
public System.Linq.Expressions.Expression Expression
{
get { return _entities.AsQueryable().Expression; }
}
public IQueryProvider Provider
{
get { return _entities.AsQueryable().Provider; }
}
}
另外我想補充一點,通用存儲庫和工作單位的實體框架是多餘的,看看這個鏈接http://rob.conery.io/2014/03/04/repositories-and-unitofwork-are-not-a-good-idea/
非常感謝那些links.I將看看他們now.Do你有沒有看過moq被用作codeFirst的模擬框架的例子?是否有任何端到端的例子下載,包括db.I確實看到第一個問題,但在你的回答中,在某些時候我不確定是否需要。謝謝你的時間 – user9969 2011-04-23 12:48:29