使用Ninject ADO.NET的DbContext [英] DbContext with Ninject ADO.NET

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

问题描述

我正在做一个已经完成80%的大项目(尽管需要实现某些功能).但是最近我们发现该项目不允许并发请求(我的意思是多个用户向同一个存储库请求).有时我们会得到空引用&有时执行无法打开可用的连接,连接状态为关闭"等. 我们的源代码在世界范围内受到严格限制.这是一些代码.让我知道是否存在任何架构问题,因为架构师离开了公司.它使用的是ninject 3.0.我已经将InRequestScope()用于所有经理的存储库,但是没有运气

I am working on a big project that 80% completed (Some features need to be implemented though).But recently we discovered that the project doesn't allow concurrent requests (I mean multiple users request to same repository). Sometime we get null referece & sometimes "Executed can not open available connection , connection state is closed" etc. Our source code is strongly restricted outside of the world. Here is some code.Let me know if there is any architectural problem, as architectural guys left company. It's using ninject 3.0. I already used InRequestScope() for all manager's repositories but no luck

更新:我在这里没有使用任何ORM,而是尝试通过DbContext类中的数据适配器连接SqlServer.

Update: I am not using any ORM here, I am trying to connect SqlServer through data adapter in my DbContext class

public class DbContext
{
  //execute query , nonquery etc using adapter & datatable
  //Example
  var dt=new DataTable();
  _adapter=new _dbfactory.CreateAdapter();
  _adapter.Fill(dt);
  return dt;
}
//MyController
 public class MyController
    {
       private readonly IMyManager_iMyManager;
       public MyController(IMyManager iMyManager){_iMyManager=iMyManager}

       public ActionResult Save()
       {
          _iMyManager.Save()
       }
   }
// My Manager
  public class MyManager:IMyManager
    {
      private readonly  IMyRepository _iMyRepository;
      DbContext _dbContext=new    
                DbContext("someParameter","connectionstring");

     public MyManager
       (
       IMyRepository iMyRepository, DbContext dbContext
       )                    
       {      
        _iMyRepository=iMyRepository;
        _dbContext=dbContext;
       }

  Public DataTable GetDataTable()
  {
    try
    {
      _dbContext.Open();
      _iMyRepository.GetDataTable()
    } 
    catch(Exception ex){}
    finally{_dbContext.Close()}
   }
 }

//这是存储库

Public class MyRepository:IMyRepository
    {
      public _dbContext;
      public MyRepository(DbContext dbContext)
      {
       _dbContext=dbContext;
      }

      public DataTable GetDataTable()
      { return _dbContext.ExecuteQuery()}
    }

最后,这是我们的ninject绑定

Finally Here is our ninject binding

public class NinjectDependencyResolver()
{
   var context=new DbContext("someparameter","connectionStrin");
   kernel.Bind<IMyManager>().To<MyManager>().WithConstructorArgument("_dbContext",context);
   kernel.Bind<IMyRepository >().To<MyRepository >().WithConstructorArgument("_dbContext",context);
}

当我在编辑器中编写所有内容时,我的代码中可能会有一些错字

there can have some typo in my code as I wrote everything in so editor

推荐答案

我认为您在Ninject Dependency Resolver中做的太复杂了.

I think you did this too complicated in Ninject Dependency Resolver.

您不应使用新关键字创建DbContext.相反,您应该让Ninject在请求范围内或在线程范围内解析DbContext.

You shouldn't create DbContext with a new keyword. Instead you should make Ninject to be resolving DbContext in request scope or in thread scope.

要注册DbContext,您可以这样操作:

To register DbContext you can do it like this:

kernel.Bind<DbContext>().To<MyDbContext>().WithConstructorArgument("someArgument", "someValue").InRequestScope();
kernel.Bind<IMyManager>().To<MyManager>().InRequestScope();
kernel.Bind<IMyRepository>().To<MyRepository>().InRequestScope();

您无需将DbContext的构造函数参数精确化,因为DbContext仅在Ninject中注册一次.

You don't need to precise the constructor argument to DbContext as DbContext is only once registered in the Ninject.

您还可以将DbContext注册到DbContextProvider类,然后可以在其中添加一些特定的逻辑来解析对象.

You can also register DbContext to a DbContextProvider class and there you can add some specific logic to resolve object.

示例:

kernel.Bind<DbContext>().ToProvider<MyDbContextProvider>().InRequestScope();

internal class MyDbContextProvider : Ninject.Activation.IProvider
{
    public object Create(IContext context)
    {
        return new MyDbContext("connectionStringArgument";
    }

    public Type Type { get { return typeof (MyDbContext); } }
}

我希望这会有所帮助.

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

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