何时使用Ninject和MongoDB使用Singleton vs Transient vs Request

时间:2022-03-08 04:17:02

I'm not quite sure when I should use SingletonScope() vs TransientScope() vs RequestScope() when I do my binding in my global.cs file.

当我在global.cs文件中进行绑定时,我不太确定何时应该使用SingletonScope()vs TransientScope()vs RequestScope()。

I have for example my call to MongoSession (using NoRM and the mvcStarter project http://mvcstarter.codeplex.com/) which is set to SingletonScope but I created a repository that use this MongoSession object to make calls to Mongo easier, e.g., I have a NewsRepository which uses MongoSession to fetch my News items from the data. As an example I have a call that fetches News items that has DisplayOnHome set to true and get the latest by CreationDate. Should such a repository be SingletonScope or would RequestScope would be more appropriate?

我有一个例如我对MongoSession的调用(使用NoRM和mvcStarter项目http://mvcstarter.codeplex.com/),它设置为SingletonScope但是我创建了一个使用这个MongoSession对象的存储库来简化对Mongo的调用,例如,我有一个NewsRepository,它使用MongoSession从数据中获取我的新闻项。作为一个例子,我有一个调用,它获取DisplayOnHome设置为true的新闻项目,并通过CreationDate获取最新项目。这样的存储库应该是SingletonScope还是RequestScope会更合适?

When should I use each of it and why?

我什么时候应该使用它们?为什么?

3 个解决方案

#1


21  

In general in a web app, you want state to be request scope as much as possible.

通常在Web应用程序中,您希望状态尽可能地是请求范围。

Only in the case of very low level optimisations are you ever likely to run into a case where its appropriate to create singleton objects (and the chances even then are that you'll pull such caching / sharing logic out into another class which gets pulled in as a dependency on your other [request scope] objects and make that singleton scope). Remember that a singleton in the context of a web app means multiple threads using the same objects. This is rarely good news.

只有在非常低级别优化的情况下,你才有可能遇到适合创建单例对象的情况(甚至可能会将这种缓存/共享逻辑拉到另一个被拉入的类中作为对您的其他[请求范围]对象的依赖,并使该单例范围)。请记住,Web应用程序上下文中的单例表示使用相同对象的多个线程。这几乎不是好消息。

On the same basis, transient scope is the most straightforward default (and that's why Ninject 2 makes it so) - request scope should only come into the equation when something needs to be shared for performance reasons, etc. (or because that's simply the context of the sharing [as mentioned in the other answer]).

在相同的基础上,瞬态范围是最直接的默认(这就是Ninject 2所做的那样) - 请求范围只应出于性能原因需要共享某些事物等等时(或者因为这仅仅是上下文)分享[如另一个答案所述])。

#2


3  

I guess the answer would depend on whether your MongoSession represents a unit of work or not. Most database related classes that I've worked with (mostly in the context of ORM, such as NHibernate or EF4) revolve around a context, entities, and tracked state that represent a unit of work. A unit of work should never be kept around longer than the length of time required to perform the given unit of work, after which the unit should be committed or rolled back. That would mean you should use RequestScope.

我想答案取决于你的MongoSession是否代表一个工作单元。我使用过的大多数与数据库相关的类(主要是在ORM的上下文中,如NHibernate或EF4)都围绕着代表工作单元的上下文,实体和跟踪状态。工作单元的工作时间不应超过执行给定工作单元所需的时间,此后应将单元承诺或回滚。这意味着你应该使用RequestScope。

If your MongoSession is not a unit of work, you could keep it around for the lifetime of an MVC session, in which case SessionScope would then be appropriate.

如果您的MongoSession不是一个工作单元,您可以在MVC会话的生命周期内保留它,在这种情况下,SessionScope将是合适的。

#3


0  

From deleted question as requested by @shankbond above

从@shankbond上面要求删除的问题


The Disposal is not necessarily performed synchronously on your main request thread as one might assume.

处理不一定在您的主请求线程上同步执行,如人们可能认为的那样。

You probably want to stash a Block and then Dispose() it at an appropriate phase in your request (how are you going to handle exceptions?)

你可能想要隐藏一个Block然后在你的请求中的适当阶段Dispose()它(你将如何处理异常?)

Have a look in the Ninject Tests for more examples (seriously, go look - they're short and clear and I didnt regret it when I listened the 3rd time I was told to!)

看看Ninject Tests中的更多例子(严肃地说,看看 - 它们很短而且清晰,当我第三次听到它时,我并没有后悔!)

See http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/

见http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/

#1


21  

In general in a web app, you want state to be request scope as much as possible.

通常在Web应用程序中,您希望状态尽可能地是请求范围。

Only in the case of very low level optimisations are you ever likely to run into a case where its appropriate to create singleton objects (and the chances even then are that you'll pull such caching / sharing logic out into another class which gets pulled in as a dependency on your other [request scope] objects and make that singleton scope). Remember that a singleton in the context of a web app means multiple threads using the same objects. This is rarely good news.

只有在非常低级别优化的情况下,你才有可能遇到适合创建单例对象的情况(甚至可能会将这种缓存/共享逻辑拉到另一个被拉入的类中作为对您的其他[请求范围]对象的依赖,并使该单例范围)。请记住,Web应用程序上下文中的单例表示使用相同对象的多个线程。这几乎不是好消息。

On the same basis, transient scope is the most straightforward default (and that's why Ninject 2 makes it so) - request scope should only come into the equation when something needs to be shared for performance reasons, etc. (or because that's simply the context of the sharing [as mentioned in the other answer]).

在相同的基础上,瞬态范围是最直接的默认(这就是Ninject 2所做的那样) - 请求范围只应出于性能原因需要共享某些事物等等时(或者因为这仅仅是上下文)分享[如另一个答案所述])。

#2


3  

I guess the answer would depend on whether your MongoSession represents a unit of work or not. Most database related classes that I've worked with (mostly in the context of ORM, such as NHibernate or EF4) revolve around a context, entities, and tracked state that represent a unit of work. A unit of work should never be kept around longer than the length of time required to perform the given unit of work, after which the unit should be committed or rolled back. That would mean you should use RequestScope.

我想答案取决于你的MongoSession是否代表一个工作单元。我使用过的大多数与数据库相关的类(主要是在ORM的上下文中,如NHibernate或EF4)都围绕着代表工作单元的上下文,实体和跟踪状态。工作单元的工作时间不应超过执行给定工作单元所需的时间,此后应将单元承诺或回滚。这意味着你应该使用RequestScope。

If your MongoSession is not a unit of work, you could keep it around for the lifetime of an MVC session, in which case SessionScope would then be appropriate.

如果您的MongoSession不是一个工作单元,您可以在MVC会话的生命周期内保留它,在这种情况下,SessionScope将是合适的。

#3


0  

From deleted question as requested by @shankbond above

从@shankbond上面要求删除的问题


The Disposal is not necessarily performed synchronously on your main request thread as one might assume.

处理不一定在您的主请求线程上同步执行,如人们可能认为的那样。

You probably want to stash a Block and then Dispose() it at an appropriate phase in your request (how are you going to handle exceptions?)

你可能想要隐藏一个Block然后在你的请求中的适当阶段Dispose()它(你将如何处理异常?)

Have a look in the Ninject Tests for more examples (seriously, go look - they're short and clear and I didnt regret it when I listened the 3rd time I was told to!)

看看Ninject Tests中的更多例子(严肃地说,看看 - 它们很短而且清晰,当我第三次听到它时,我并没有后悔!)

See http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/

见http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/