Unit testing 在EF4.1代码中,UnitOfWork和GenericRepository模式首先是冗余的吗?
不知道是否需要使用Genericrepository模式和UnitOfWork来模拟存储库。我正在使用MOQ。由于我注意到EF 4.1已经设置了IDBSet,它现在是多余的了吗 我还没有弄明白如何编写usic IDBSet的通用代码。如果你有一个实现IDBSet的示例,你能给我演示一下吗Unit testing 在EF4.1代码中,UnitOfWork和GenericRepository模式首先是冗余的吗?,unit-testing,mocking,repository-pattern,entity-framework-4.1,unit-of-work,Unit Testing,Mocking,Repository Pattern,Entity Framework 4.1,Unit Of Work,不知道是否需要使用Genericrepository模式和UnitOfWork来模拟存储库。我正在使用MOQ。由于我注意到EF 4.1已经设置了IDBSet,它现在是多余的了吗 我还没有弄明白如何编写usic IDBSet的通用代码。如果你有一个实现IDBSet的示例,你能给我演示一下吗 有什么建议吗?这是许多已经讨论过的主题的重复,所以我同意其中一些很难找到,因为它们嵌套在其他问题中 我希望这能给你一些答案。如果没有,请在此处或在新问题中询问更多信息 公共类MockDbSet:I
有什么建议吗?这是许多已经讨论过的主题的重复,所以我同意其中一些很难找到,因为它们嵌套在其他问题中
公共类MockDbSet:IDbSet其中T:class,new()
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; }
}
}
{
私人名单实体;
公共MockDbSet(列出实体)
{
_实体=实体;
}
公共虚拟T添加(T实体)
{
_实体。添加(实体);
返回实体;
}
公共虚拟T连接(T实体)
{
_实体。添加(实体);
返回实体;
}
公共TDerivedEntity Create(),其中TDerivedEntity:class,T
{
将新的T()作为TDerivedEntity返回;
}
公共虚拟T创建()
{
返回新的T();
}
公共虚拟T查找(参数对象[]键值)
{
抛出新的NotImplementedException();
}
public System.Collections.ObjectModel.ObservableCollection Local
{
得到
{
返回新的ObservableCollection(_实体);
}
}
公共虚拟T删除(T实体)
{
_实体。移除(实体);
返回实体;
}
公共IEnumerator GetEnumerator()
{
返回_entities.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回_entities.GetEnumerator();
}
公共类型ElementType
{
获取{return\u entities.AsQueryable().ElementType;}
}
public System.Linq.Expressions.Expression表达式
{
获取{return\u entities.AsQueryable().Expression;}
}
公共IQueryProvider提供程序
{
获取{return\u entities.AsQueryable().Provider;}
}
}
此外,我想补充的是,实体框架上的通用存储库和工作单元是多余的,请查看此链接非常感谢这些链接。我现在将查看它们。您是否有或看到过将moq用作codeFirst模拟框架的示例?是否有包括db在内的端到端示例可供下载。我确实看到了第一个问题,但在您的回答中,我不确定是否需要。谢谢您的时间