I am trying to get to grips with Ninject and can't seem to find any articles on here that help solve my issue. I have created a simple n-tier solution that contains Web, Business Logic and Data Access Layers. In the DAL I have created a model for my database (simple two table DB) and generic repositories (IRepository
and ItemRepository
) that look as follows.
public interface IRepository<T> where T : class
{
IQueryable<T> GetAll();
}
The implementation of this Interface looks like this.
public class ItemRepository : IRepository<Item>
{
public IQueryable<Item> GetAll()
{
IQueryable<Item> result;
using (GenericsEntities DB = new GenericsEntities()) {
result = DB.Set<Item>();
}
return result;
}
}
In my BLL I have created a DataModule
, an Item
Object and a class (DoWork
) to use these. These look as follows...
public class DataModule : NinjectModule
{
public override void Load()
{
Bind(typeof(IRepository<>)).To<ItemRepository>();
}
}
The Item object
public class Item
{
DAL.IRepository<DAL.Item> _repository;
[Inject]
public Item(DAL.IRepository<DAL.Item> repository) {
_repository = repository;
}
public List<DAL.Item> GetItems(){
List<DAL.Item> result = new List<DAL.Item>();
result = _repository.GetAll().ToList();
return result;
}
}
The DoWork Class
public DoWork()
{
var DataKernel = new StandardKernel(new DataModule());
var ItemRepository = DataKernel.Get<IRepository<DAL.Item>>();
Item thisItem = new Item(ItemRepository);
List<DAL.Item> myList = thisItem.GetItems();
}
The problem I have is that when I use this code from the Web Project I get a "DbContext is disposed" runtime error. I have tried to keep things simple just to get to grips with the framework but don't understand how to get the DbContext
scope correct. I have looked at other articles on here but there are all specific to certain scenarios and I want to get the basics right.
Can anyone help or point me in the right direction?