简体   繁体   English

当Ninject用作依赖项解析器时,如何在asp.net mvc3 App中处理DbContext(或对象)

[英]How to Dispose DbContext(or object) in asp.net mvc3 App when Ninject is used as dependency resolver

For this Demo I have created a fake Database+repository as below 对于这个Demo,我创建了一个虚假的Database +存储库,如下所示

Fake Db + Repository 假Db +存储库

 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 Ninject Part

I installed ninject from NUGet and added below code for resolving repositories 我从NUGet安装了ninject并在下面添加了用于解析存储库的代码

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 . Ninject没有调用DemoRepository.Dispose ,我添加了一个断点,即使我当前的代码抛出错误但是Ninject没有调用DemoRepository.Dispose

Can any body suggest me how dispose the object. 任何人都能建议我如何处置这个物体。

如果要处理,请确保您的存储库绑定到Ninject中的请求范围:

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

You don't need to Dispose() of you DbContext , since it already manages all connections properly itself. 您不需要Dispose() DbContext ,因为它已经正确管理所有连接。 Here's a quote from ASP.NET MVC Tip #34 – Dispose of Your DataContext (or Don't) : 这是ASP.NET MVC提示#34 - 处理您的DataContext(或不要)的引用:

The most important consequence of calling the DataContext.Dispose() method is that any open connections associated with the DataContext get closed. 调用DataContext.Dispose()方法的最重要结果是与DataContext关联的任何打开的连接都将关闭。 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. 它不重要的原因是DataContext类已经管理了它的连接。 By default, the DataContext class opens and closes a connection automatically. 默认情况下,DataContext类会自动打开和关闭连接。

... ...

Most often, when you call the Dispose() method on the DataContext object, any database connection associated with the DataContext object is already closed. 通常,当您在DataContext对象上调用Dispose()方法时,与DataContext对象关联的任何数据库连接都已关闭。 The DataContext object has closed the database connection right after executing the database query. DataContext对象在执行数据库查询后立即关闭了数据库连接。 So, the Dispose() method really doesn't have anything to do. 所以,Dispose()方法实际上没有任何事情可做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM