2

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?

4

1 に答える 1

2

You are getting "DbContext is disposed" because you are disposing it before you leave the GetAll method on your ItemRepository and the query is not executed yet. The query is executed inside the GetItems method when the ToList() is called - at that time your data context is already dispose because of that using closure. If you want to return Items as IQueryable you will have to let the data context alive until you are done with your query.

I would suggest to bind your GenericsEntities in request scope (ninject will dispose it for you at the and of request) for web application or in some custom scope if it is desktop app and inject into your repository.

Registration

Bind<GenericEntities>().ToSelf().InRequestScope();

Repository

public class ItemRepository : IRepository<Item>
{
    private readonly GenericEntities DB;

    public ItemRepository(GenericEntities db)
    {
         this.DB = db;                            
    } 

    public IQueryable<Item> GetAll()
    {
        return DB.Set<Item>();
    }   
}
于 2013-01-04T16:38:16.250 に答える