4

For this Demo I have created a fake Database+repository as below

Fake Db + Repository

 public interface IDemoRepository
    {
        string[] GetUsers();
    }

    public class DemoRepository : IDemoRepository, IDisposable
    {

        public string[] GetUsers()
        {
            string[] Users = { "Robert","Linda","Jack"};
            return Users;
        }

        public void Dispose()
        {
            //do nothing     
            throw new Exception("Disposed is called");       
        }
    }

My Controller looks this

 public class TestController:Controller
    {
        protected IDemoRepository _repository;

        public BaseController(IDemoRepository repository)
        {
            _repository = repository;
        }

        public ActionResult()
       {  
             var users = _repository.GetUsers();
             Return View(users);
       }
    }

Ninject Part

I installed ninject from NUGet and added below code for resolving repositories

kernel.Bind<IDemoRepository>().To<DemoRepository>()

Ninject is not calling DemoRepository.Dispose, i added a break point even my current code is throwing error but Ninject is not calling DemoRepository.Dispose.

Can any body suggest me how dispose the object.

Rusi Nova
  • 2,617
  • 6
  • 32
  • 48

2 Answers2

5

Make sure that your repository is bound to the request scope in Ninject if you want it to be disposed:

kernel.Bind<IDemoRepository>().To<DemoRepository>().InRequestScope();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    @RusiNova, NInject doesn't call Dispose by default. It will automatically ensure that instances implementing IDisposable are properly disposed only if you limit their scope. You may take a look at the following answer: http://stackoverflow.com/questions/2964673/ninject-and-datacontext-disposal – Darin Dimitrov Oct 19 '11 at 16:52
1

You don't need to Dispose() of you DbContext, since it already manages all connections properly itself. Here's a quote from ASP.NET MVC Tip #34 – Dispose of Your DataContext (or Don’t):

The most important consequence of calling the DataContext.Dispose() method is that any open connections associated with the DataContext get closed. This might seem really important, but it’s not. The reason that it is not important is that the DataContext class already manages its connections. By default, the DataContext class opens and closes a connection automatically.

...

Most often, when you call the Dispose() method on the DataContext object, any database connection associated with the DataContext object is already closed. The DataContext object has closed the database connection right after executing the database query. So, the Dispose() method really doesn't have anything to do.

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • 4
    I wouldn't rely on that. It's merely an implementation detail which might change in some future version. For me if the author of a class decided to implement IDisposable it meant that he wanted the .Dispose method of this class to be called (unless the author of the class didn't really know what he was doing which I don't think is the case with the authors of the EF). – Darin Dimitrov Oct 19 '11 at 16:46