当 Ninject 用作​​依赖解析器时,如何在 asp.net mvc3 应用程序中处置 DbContext(或对象)

发布于 2024-12-10 19:16:51 字数 1201 浏览 2 评论 0原文

对于这个演示,我创建了一个假数据库+存储库,如下所示

假数据库+存储库

 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");       
        }
    }

我的控制器看起来是这个

 public class TestController:Controller
    {
        protected IDemoRepository _repository;

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

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

Ninject部分

我从NUGet安装了ninject并添加了以下代码来解析存储库

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

Ninject没有调用DemoRepository.Dispose,我添加了一个断点,即使我当前的代码抛出错误,但 Ninject 没有调用 DemoRepository.Dispose。

任何人都可以建议我如何处置该物体吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欢烬 2024-12-17 19:16:51

如果您希望将其释放,请确保您的存储库已绑定到 Ninject 中的请求范围:

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

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();
温柔女人霸气范 2024-12-17 19:16:51

您不需要对 DbContext 进行 Dispose(),因为它本身已经正确管理所有连接。这是来自 ASP.NET MVC 技巧 #34 – 处置您的 DataContext(或不处置)

调用 DataContext.Dispose() 方法的最重要结果是与 DataContext 关联的任何打开的连接都会被关闭。这看起来似乎非常重要,但事实并非如此。它不重要的原因是 DataContext 类已经管理其连接。默认情况下,DataContext 类自动打开和关闭连接。

...

大多数情况下,当您对 DataContext 对象调用 Dispose() 方法时,与 DataContext 对象关联的任何数据库连接都已关闭。 DataContext 对象在执行数据库查询后立即关闭了数据库连接。因此,Dispose() 方法实际上没有任何作用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文