I'm trying to setup a base Repository class that can use the Entity Framework edmx model context. The problem I'm having is that I need to find an interface that the EF EDMX object context implements so I can pass to the constructor via dependency injections. I've got around this before by using a DataFactory that creates it and stores it in the HttpContext but that kills the ability to unit test. Any help would be appreciated. Thanks!
public abstract class BaseRepository<T> where T : EntityObject
{
private MyDataModelContext _dataContext;
private ObjectSet<T> dbset;
protected BaseRepository(IObjectContext dataContext)
{
_dataContext = dataContext;
dbset = _dataContext.CreateObjectSet<T>();
}
.....
I've always created a DataContextFactory that passes my own interface to the Context, and passed that to my repositories like so:
The context interface:
The context factory:
The context factory interface:
The repository:
Then when I want to test the repository, I pass in a fake
IMyDataContextFactory
that returns a fakeIMyDataContext
fromGetContext()
.In time I notice duplication in repositories, and can push certain methods into the base repository:
GetAll()
,Save()
,GetById()
sometimes if I have consistent primary keys, etc.