使用存储库模型与 MVC3 和 Ninject 进行每个请求的 RavenDB 会话
我正在寻找一些关于正确机制的建议,用于以真正的会话公关请求行为将 RavenDB IDocumentSession 放入我的存储库。
这是一个全新的 MVC3 应用程序,我已经使用 NuGet 获得了 Ninject / Ninject.MVC3。 RavenDB 在外部服务器上运行(即非嵌入式)。
我已经设置了 Ninject 模块来返回正确的存储库,以及每个请求的会话。
然而 - MVC3 真的会为每个操作方法实例化控制器吗?在这种情况下,我可以允许 MVC3/Ninject 注入我的存储库和它们需要的会话,没问题。
但是,如果在多个请求之间重用控制器,则这可能不起作用,因为挂在先前请求上的存储库现在可能会使用旧的并被丢弃的会话。
我研究了几种方法来做到这一点 - 以上是基本方法。我也尝试过做一些类似 ActionFilterAttribute 的事情,它在每个请求开始时从 IoC 容器获取一个新会话 - 但在这种情况下,我应该把它放在哪里?
我的存储库是否应该有一个每次实际从容器获取当前会话的 Session 属性?这将增加存储库实现和 IoC 容器之间的耦合,但我认为应该可以工作。
执行此操作的正确方法是什么?酷孩子们是如何做到的?任何帮助将不胜感激!
I am looking for some advice on the correct mechanism to use for getting a RavenDB IDocumentSession into my repositories in a true session-pr-request behaviour.
This is a greenfield MVC3 application, and I've gotten Ninject / Ninject.MVC3 using NuGet. RavenDB is running on an external server (i.e. non-embedded).
I've set up the Ninject module to return the right repositories, and also a session per request for them.
However - is it true that MVC3 will instantiate the controller for each action method? In that case, I can just allow MVC3/Ninject to inject my repositories and the sessions they need, no problem.
However, if a controller is reused across several requests, this might not work, as the repository hanging around from a previous request might now employ a session that is old and discarded.
I have looked at a few ways to do this - the above is the basic one. I have also tried to do something like an ActionFilterAttribute that gets a new session from the IoC container at the start of each request - but in that case, where should I put it?
Should my repository have a Session property it uses that actually gets the current session from the container each time? This would add coupling between the repository implementation and the IoC container, but otherwise should work I guess.
What is the proper way to do this? How are the cool kids doing it? Any help would be highly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非您正在对控制器工厂做一些非常有趣的事情,否则每个控制器实例将用于单个请求。
控制器不是线程安全的,通常不会在单个请求之后继续存在。
Unless you are doing something really funny with your controller factory, each controller instance will be used for a single requests.
Controllers are not thread safe and will not usually survive beyond a single request.
我写了一篇关于使用 Ninject 的
InRequestScope
的综合博客文章,以便每个请求注入一次IDocumentSession
。 Ninject 非常擅长为您管理范围。I've written a comprehensive blog post about using Ninject's
InRequestScope
so thatIDocumentSession
is injected once per request. Ninject is great at managing scope for you.http://www.dalsoft.co.uk/blog/index.php/2012/04/12/mvc-get-ravendb-up-and-running-in-5-minutes-using-ninject/
我认为你应该避免继续使用控制器。
有时它可能会有所帮助:
如果会话状态被禁用,我们不应该再尝试使用控制器上的会话属性,因为它将为空。关闭会话状态并使用会话属性会给您带来可怕的“对象引用未设置到对象实例”错误。
I think that you should avoid going on with Controllers.
It may help sometime:
If session state is disabled we should no longer try to use the Session Property on the Controller as it will be null. Turning off session state and using the Session Property will give you the dreaded “object reference not set to an instance of object” error.