asp.net-mvc-4 – 工作单元和通用存储库与实体框架5
|
我使用ASP.NET MVC 4与Entity Framework 5.我有模型类和实体映射,以将现有表映射到这些模型类。所有这一切都很好,工作伟大。 现在我想嘲笑这个。我创建了工作单元,它接受DataContext并使用通用存储库。在那之后,我构建服务,以便能够从多个存储库中获取数据,而只需要有一个DataContext的实例。这也工作伟大。 现在到问题:我想测试的服务,用模拟数据。当我创建Unit Of Work实例时,我想要能够插入一个被嘲笑的DataContext,而不是真正的DataContext。 我试图创建一个IContext接口,让真正的嘲笑DataContext实现,但遇到了DbSet的问题。我试图使用IDbSet和创建一个FakeDbSet,但没有成功。我也读在互联网上嘲笑上下文与IDbSet和使用FakeDbSet是一个坏的方法。 你有什么想法将是实现这一最好的方法吗?我现在是我想保留的行为,但真的想能够从DataContext中的Model类中模拟数据。 我知道实体框架已经附带了工作单位的行为,你不需要添加额外的行为。但我想包装在另一个类,跟踪所有的库(称为UnitOfWork类)。 编辑:我写了两篇文章解释我的解决方案与LINQ和实体框架。 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);
}
IUnitOfWork.cs public interface IUnitOfWork : IDisposable
{
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
void Save();
}
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);
}
}
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;
}
}
}
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();
}
}
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();
}
解决方法你的ExampleService类期待IUnitOfWork,这意味着你只需要另一个IUnitOfWork是一个Mock,它的GetRepository()方法将返回一个IRepository Mock。例如(不是真的一个Mock,但内存中的存根): public InMemoryRepository<T> : IRepository<T> where T : class
{
........
}
public InMemoryUnitOfWork : IUnitOfWork
{
public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
return new InMemoryRepository<TEntity>();
}
}
然后: 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();
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 按IN子句排序SQL Server结果
- asp.net-mvc-3 – ASP.NET MVC 3 – 验证问题
- asp.net-mvc – 将EF 4.1代码首先将ASP.NET MVC3部署到中等
- 如何为asp.net身份创建安全邮戳值(IUserSecurityStampStore
- asp.net-mvc – 如何自定义简单的成员资格提供者来处理我自
- asp.net-mvc-3 – 在MVC 3的AuthorizeAttribute中获取模型数
- 在ASP.NET 5中获取AuthenticationProperties
- asp.net – 如何重命名Visual Studio 2012中的IIS Express网
- 在ASP.NET中上传文件时运行病毒扫描
- asp.net-mvc – ASP .Net MVC中购物车的会话变量的替代方法
- asp.net – 如何从codebehind访问web.config的ht
- SCRIPT87:IE 9中无效的参数,ASP.NET C#
- ASP.NET 5中的全局异常处理
- ASP.NET Web窗体DropDownList具有SelectedValue,
- asp.net – 提供的URI方案’https’无效;预期’h
- asp.net – 用于验证的数据注释,至少一个必填字段
- asp.net-mvc – IIS Express会自动禁用我的项目的
- 缺少版本和工具:.NET Core,Core Tools,dotnet C
- asp.net-mvc – MiniProfiler与EF“模型第一”ed
- asp.net – global.asax Application_Error不触发
