如何使用Ninject时处理的DbContext [英] How to handle DBContext when using Ninject

查看:115
本文介绍了如何使用Ninject时处理的DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Ninject和OpenAccess的首次。请帮我下。这里是我的项目是什么样子...

I am trying to use Ninject and OpenAccess for the first time. Please help me with the following. Here is what my project looks like...

public class ContentController : Controller
{
    private ContentService contentSvc;

    public ContentController(ContentService contentSvc)
    {
        this.contentSvc = contentSvc;
    }
}

下面的类是在我的web应用程序的文件夹下。

The following class is under a folder in my web app.

public class ContentService
{
    private IContentRepository contentRepository;

    public ContentService(IContentRepository contentRepository)
    {
        this.contentRepository = contentRepository;
    }

    public void InsertContent(Content content)
    {
         contentRepository.InsertContent(content);
    }
}

下面的资料库属于一个单独的程序。

The following repository belongs to a separate assembly.

public class ContentRepository : IContentRepository
{
    DBContext db;
    public ContentRepository(DBContext _db)
    {
        db = _db;
    }

    public void InsertContent(Content content)
    {
             db.Add(content);
    }
}   

下面是Ninject绑定的样子。

Here is what Ninject binding look like..

kernel.Bind<ContentService>().To<ContentService>().InRequestScope();
kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope().WithConstructorArgument("_db", new DBContext());

如果我在一个时间内获取一个网页一切正常。我使用的是简单的工具的Xenu同时获取多个页面。这是当我通过一次读取多个页面,获得与错误的DbContext

Everything works fine if I fetch one page at a time. I am using a simple tool 'XENU' to fetch multiple pages simultaneously. This is when I get errors with DBContext by fetching multiple pages at a time.

我不知道是否Ninject在每个请求dosposing中的DbContext?我得到不同的错误,例如, 对象引用未设置到对象的实例。'或'ExecuteReader需要一个开放和可用的连接。连接的当前状态是开放的。

I am not sure if Ninject is dosposing the DBContext in each REQUEST?? I get different errors, e.g. 'Object reference not set to an instance of an object.', OR 'ExecuteReader requires an open and available Connection. The connection's current state is open.'

我有一个contentService的文件夹下,我的MVC Web应用程序。 ContentRepository是一个单独的程序。我会被contentService的添加业务逻辑和只能使用CRUD操作ContentRepository。另外,请让我知道如果这个结构是正确的,或者有更好的方法来创建服务和信息库。

I have ContentService under a folder in my MVC web app. ContentRepository is a separate assembly. I will be adding business logic in ContentService and use 'ContentRepository' only for CRUD operations. Also, please let me know if this architecture is okay or is there a better way to create services and repositories.

推荐答案

下面是我会怎么做你的Ninject绑定,

Here's how I would do your Ninject bindings,

kernel.Bind<DBContext>().ToSelf().InRequestScope();
kernel.Bind<ContentService>().ToSelf().InRequestScope();
kernel.Bind<IContentRepository>().To<ContentRepository>().InRequestScope();

这个模式应该在这个例子做工精细以上EF和Ninject。

This pattern should work fine in the example above with EF and Ninject.

这篇关于如何使用Ninject时处理的DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆