Gau*_*aui 27 asp.net-mvc-4 .net-4.5 entity-framework-5
我正在使用ASP.NET MVC 4和Entity Framework 5.我有模型类和实体映射来将现有表映射到那些模型类.这一切都很好,工作得很好.
现在我想嘲笑这个.我创建了Unit Of Work,它接受DataContext并使用Generic Repository.在那之后,我构建了服务,以便能够立即从许多存储库获取数据,并且只需要有一个DataContext实例.这也很有效.
现在问题:我想用模拟数据测试服务.当我创建Unit Of Work实例时,我希望能够插入一个被模拟的DataContext而不是真正的DataContext.
我试图创建一个IContext接口,让真实的和模拟的DataContext实现,但遇到了DbSet的问题.我试图使用IDbSet并创建一个FakeDbSet但没有成功.我还在互联网上读到用IDbSet模拟上下文并使用FakeDbSet是一种糟糕的方法.
你知道最好的方法是什么?我现在拥有的是我想保留的行为,但是我真的希望能够在DataContext中模拟Model类中的数据.
我知道实体框架已经附带了工作单元行为,并且您不需要在其上添加额外的行为.但是我想把它包装在另一个跟踪所有存储库的类中(称为UnitOfWork类).
编辑:我写了两篇文章,用LINQ和Entity Framework解释我的解决方案.
http://gaui.is/how-to-mock-the-datacontext-linq/
http://gaui.is/how-to-mock-the-datacontext-entity-framework/
这是我的代码:
IRepository.cs
public interface IRepository<T> where T : class
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
T GetById(long Id);
IEnumerable<T> All();
IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
}
Run Code Online (Sandbox Code Playgroud)
IUnitOfWork.cs
public interface IUnitOfWork : IDisposable
{
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
void Save();
}
Run Code Online (Sandbox Code Playgroud)
Repository.cs
public class Repository<T> : IRepository<T> where T : class
{
private readonly IDbContext _context;
private readonly IDbSet<T> _dbset;
public Repository(IDbContext context)
{
_context = context;
_dbset = context.Set<T>();
}
public virtual void Add(T entity)
{
_dbset.Add(entity);
}
public virtual void Delete(T entity)
{
var entry = _context.Entry(entity);
entry.State = System.Data.EntityState.Deleted;
}
public virtual void Update(T entity)
{
var entry = _context.Entry(entity);
_dbset.Attach(entity);
entry.State = System.Data.EntityState.Modified;
}
public virtual T GetById(long id)
{
return _dbset.Find(id);
}
public virtual IEnumerable<T> All()
{
return _dbset;
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return _dbset.Where(predicate);
}
}
Run Code Online (Sandbox Code Playgroud)
UnitOfWork.cs
public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
{
private readonly IDbContext _ctx;
private Dictionary<Type, object> _repositories;
private bool _disposed;
public UnitOfWork()
{
_ctx = new TContext();
_repositories = new Dictionary<Type, object>();
_disposed = false;
}
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
if (_repositories.Keys.Contains(typeof(TEntity)))
return _repositories[typeof(TEntity)] as IRepository<TEntity>;
var repository = new Repository<TEntity>(_ctx);
_repositories.Add(typeof(TEntity), repository);
return repository;
}
public void Save()
{
_ctx.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
_ctx.Dispose();
}
this._disposed = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
ExampleService.cs
public class ExampleService
{
private IRepository<Example> m_repo;
public ExampleService(IUnitOfWork uow)
{
m_repo = uow.GetRepository<Example>();
}
public void Add(Example Example)
{
m_repo.Add(Example);
}
public IEnumerable<Example> getAll()
{
return m_repo.All();
}
}
Run Code Online (Sandbox Code Playgroud)
ExampleController.cs
public IEnumerable<Example> GetAll()
{
// Create Unit Of Work object
IUnitOfWork uow = new UnitOfWork<AppDataContext>();
// Create Service with Unit Of Work attached to the DataContext
ExampleService service = new ExampleService(uow);
return service.getAll();
}
Run Code Online (Sandbox Code Playgroud)
hai*_*770 10
你的ExampleService班级期待IUnitOfWork,这意味着你只需要另一个IUnitOfWorkMock,它的GetRepository()方法将返回一个IRepository模拟.
例如(不是真的是Mock而是In-Memory存根):
public InMemoryRepository<T> : IRepository<T> where T : class
{
........
}
public InMemoryUnitOfWork : IUnitOfWork
{
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
return new InMemoryRepository<TEntity>();
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
public IEnumerable<Example> GetAll()
{
// Create Unit Of Work object
IUnitOfWork uow = new InMemoryUnitOfWork();
// Create Service with Unit Of Work
ExampleService service = new ExampleService(uow);
return service.getAll();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27856 次 |
| 最近记录: |