Entity Framework code first ctp 5 using an IDbSet in RepositoryBase class
I want to find out if it makes sense to use the Entity Framework code first ctp 5 IDbSet in a Repository base class .
I am using the Repository Pattern(and would like to follow the persistence ignorance method) for my implementations and I would like to use fake data for testing(by using a fake in memory IDBset implementation).
I feel that adding a dependency to the System.Data.Entity.IDbSet would tie my implementation of the repository base class to the IDBset and if there are any future changes to it that might break the code. What is the best way of implementaing a repository base class without the Idbset dependency?
I am following this post :EF CTP4 Tips & Tricks: Testing With Fake DbContext to 开发者_运维技巧implement fakes.
//_dbset impliments the IDBset
//Database inherits from DbContext
protected RepositoryBase(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_dbset = _unitOfWork.Database.Set<T>();
}
public virtual void Add(T entity)
{
_dbset.Add(entity);
}
public virtual void Delete(T entity)
{
_dbset.Remove(entity);
}
public virtual T GetById(long id)
{
return _dbset.Find(id);
}
public virtual IEnumerable<T> All()
{
return _dbset.ToList();
}
It is a point of repository to wrap dependencies on mapping layer. So in your case you will have dependency on EF CTP5 and IDbSet
- that is correct. That is a correct approach. If you want to test your code you will for example:
- Mock whole repository when unit testing upper layer
- Use concrete repository for integration tests
精彩评论