问题描述
什么是管理类MongoServer生命周期中的最佳做法?我应该创建一个,在每个请求结束时关闭它,或者它应该被保留作为使用像StructureMap应用的整个生命单身?
What is the best practice for managing the MongoServer class life cycle? Should I create one and close it at the end of each request or should it be kept as a singleton for the entire life of the app using something like StructureMap?
任何帮助是AP preciate。
Any help is appreciate.
推荐答案
在the官方文档更说明 MongoServer
, MongoDatabase
和 MongoCollection
都是线程安全的,并且你应该创建一个单独的 MongoServer
为每次连接数据库。
In the official documentation it is stated that MongoServer
, MongoDatabase
, and MongoCollection
are thread safe, and that you're supposed to create one single MongoServer
for each database that you connect to.
因此, MongoServer
, MongoDatabase
和 MongoCollection
可以安全地被构造成单件。 MongoServer
甚至会帮助返回的后续调用相同 MongoDatabase
实例强制执行这一点, MongoDatabase
会做同样的事情 MongoCollection
秒。
Thus, MongoServer
, MongoDatabase
, and MongoCollection
can safely be configured to be singletons. MongoServer
will even help enforcing this by returning the same MongoDatabase
instance for successive calls, and MongoDatabase
will do the same thing for MongoCollection
s.
即。你的 MongoServer
实例可以安全地被配置为拥有一个单身的生活方式,你的IoC容器,你还不如设立注射 MongoDatabase
,甚至 MongoCollection
为好。
I.e. your MongoServer
instance can safely be configured to have a singleton lifestyle in your IoC container, and you might as well set up injection for MongoDatabase
and maybe even MongoCollection
as well.
我使用这种策略与温莎自己 - 你可以看到我的 MongoInstaller
此处的 - 它让我的班,只是继续前进,这样做:
I'm using this strategy with Windsor myself - you can see my MongoInstaller
here: https://gist.github.com/2427676 - it allows my classes to just go ahead and do this:
public class SomeClass
{
public SomeClass(MongoCollection<Person> people)
{ ... }
}
为了有一个集合注入,美观大方,并准备使用。
in order to have a collection injected, nice and ready to use.
这篇关于什么是管理在ASP.Net MVC的MongoDB连接的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!