Background:
Whilst writing my own MVC application I had a requirement to interact with a database. I decided to use generics with entity framework Repositories like so:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
DatabaseContext<TEntity> _dbContext;
Database _database;
public IQueryable<TEntity> All
{
get
{
return (IQueryable<TEntity>) _dbContext.Items;
}
}
public Repository()
{
_dbContext = new DatabaseContext<TEntity>();
_database = _dbContext.Database;
}
}
As you can see I also use a generic DbContext.
My Two Part Question
I took a while to sit back and look at the solution before proceeding with it. I realise that this forces me to create multiple DB context objects.
i) Is this bad practice?
ii) Will it have a noticeable impact on the efficiency of my solution?