We are starting to develop a small framework in our company, to share code between different applications. For data access we are using EF4. We have a custom DbContext class and a generic Repository:
public class RMDbContext : DbContext
{
// ....
}
public interface IRepository
{
IQueryable<T> All();
void Delete(T entity) where T : class;
void Add(T entity) where T : class;
void Update(T entity) where T : class;
int SaveChanges();
void RollbackChanges();
}
The problem here is how to implement the repository, using our custom DbContext class (RMDbContext). My co-worker thinks that the best way is to let RMDbContext implement the IRepository interface:
public class RMDbContext : DbContext, IRepository
{
// ....
}
To be honest I don't like this approach, because the context is tied to a specific contract (IRepository). IMO it's better to create a repository implementation that uses the RMDbContext, something like this:
public class Repository<T> : IRepository where T : RMDbContext, new()
{
protected readonly RMDbContext context;
public class Repository()
{
context = new T();
}
// ....
}
What do you think about these 2 approaches? Which one would you choose, and why?