适当的储存库的生命周期作用域瓦特/ Ninject在MVC [英] Appropriate Repository LifeCycle Scope w/ Ninject in MVC

查看:151
本文介绍了适当的储存库的生命周期作用域瓦特/ Ninject在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个MVC 3应用程序中使用实体框架4 Ninject时,什么是一个存储库和EF背景下适当生命周期作用域?

我一直在使用InTransientScope的默认,但质疑是否应该InRequestScope。

 公共类myController的:控制器
 {
   私人只读IMyRepo _repo;
   公共myController的(IMyRepo回购)
   {
     _repo =回购;
   }   公众的ActionResult指数()
   {
     变种结果= _repo.GetStuff();
     返回查看(结果);
   }
 }

Ninject模块:

 公共类MyServices:NinjectModule
  {
    公共overrride无效负载()
    {
      绑定< IMyRepo>&。为了LT; MyRepo>();
      绑定< MyContext> .ToSelf();
    }
  }

MyRepo:

 公共类MyRepo:IMyRepo
{
  私人只读MyContext _context;
  公共MyRepo(MyContext上下文)
  {
    _context =背景;
  }
  公共IEnumerable的GetStuff()
  {
    返回_context.Entity; //查询的东西
  }}


解决方案

您存储库可以是短暂的范围,但是,我会结合请求范围的上下文。这样,所有的存储库实例将共享相同的上下文。这样,您就可以收获一个ORM的缓存和事务的好处。

它的工作原理目前在code的方式是一个新的上下文中创建你请求一个任意时间。所以,如果你的控制器首先使用资料库,然后调用反过来使用存储库的另一个模块。每个这些仓库将具有上下文的不同实例。所以,实际上你现在使用ORM仅仅作为一个连接管理器和SQL生成器。

这也可以产生意想不到的后果。想象一下,一个code这样的:

 公众的ActionResult MyAction(INT ID)
{
    变种实体= _repository.Get&所述;实体>(ID);
    entity.Prop =处理;
    _module.DoStuff(ID);
}

如果该DoStuff方法,最终调用 _repository.Get<实体>(ID); 再次,你会有不同步的你的实体的2个不同的副本。

What is the appropriate LifeCycle Scope for a repository and the EF context when using Entity Framework 4 with Ninject in an MVC 3 application?

I've been using the default of InTransientScope, but questioning whether it should be InRequestScope.

 public class MyController: Controller
 {
   private readonly IMyRepo _repo;
   public MyController(IMyRepo repo)
   {
     _repo = repo;
   }

   public ActionResult Index()
   {
     var results = _repo.GetStuff();
     return View(results);
   }
 }

Ninject Module:

  public class MyServices : NinjectModule
  {
    public overrride void Load()
    {
      Bind<IMyRepo>.To<MyRepo>();
      Bind<MyContext>.ToSelf();
    }
  }

MyRepo:

public class MyRepo: IMyRepo
{
  private readonly MyContext _context;
  public MyRepo(MyContext context)
  {
    _context = context;
  }
  public IEnumerable GetStuff()
  {
    return _context.Entity;//query stuff
  }

}

解决方案

Your repository can be transient scope, however, I would bind the context in request scope. This way all of your repository instances will share the same context. This way you can reap the caching and transactional benefits of an ORM.

The way it works currently in your code is that a new context is created any time you request one. So if your controller first uses a repository and then calls another module that in turn uses a repository. Each of those repositories will have a different instance of the context. So in effect you are now using your ORM simply as a connection manager and SQL generator.

This can also have unintended consequences. Imagine a code like the following:

public ActionResult MyAction(int id)
{
    var entity = _repository.Get<Entity>(id);
    entity.Prop = "Processing";
    _module.DoStuff(id);
}

If the DoStuff method, eventually calls _repository.Get<Entity>(id); again, you will have 2 different copies of your entity that are out of sync.

这篇关于适当的储存库的生命周期作用域瓦特/ Ninject在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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