何时使用辛格尔顿VS瞬态VS使用Ninject和MongoDB请求 [英] When to use Singleton vs Transient vs Request using Ninject and MongoDB

查看:105
本文介绍了何时使用辛格尔顿VS瞬态VS使用Ninject和MongoDB请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太确定我何时应该使用SingletonScope()VS TransientScope()VS RequestScope()时,我做我的,我global.cs文件绑定。

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

我有我的例子调用MongoSession(使用规范和mvcStarter项目 HTTP://mvcstarter.$c$cplex.com / ),它被设置为SingletonScope,但我创建使用该MongoSession对象进行调用蒙戈更容易,例如一个仓库,我有一个使用MongoSession从数据提取我的新闻项NewsRepository。作为一个例子,我有取有DisplayOnHome设置为true,并得到通过CreationDate最新的新闻条目的电话。如果这样一个库是SingletonScope或将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?

当我应该使用每个它,为什么?

When should I use each of it and why?

推荐答案

在一般的Web应用程序,你想要的状态是请求范围内尽可能多地。

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.

在相同的基础上,短暂的范围是最直接的默认值(这就是为什么Ninject 2使它如此) - 请求范围应只来到这个公式有什么需要出于性能原因等方面进行共享时(或因为这是简单地[如其他答复中提到]共享的情况下)。

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]).

这篇关于何时使用辛格尔顿VS瞬态VS使用Ninject和MongoDB请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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