Official MongoDB C # driver in ASP.NET MVC application with Ninject dependency injection

Does anyone have a code example that follows best practices for using the official MongoDB C # driver with Ninject in an ASP.NET MVC application?

My looks like this:

namespace WebApp { public class DataModule : NinjectModule { public override void Load() { var conventions = new ConventionProfile().SetElementNameConvention(new CamelCaseElementNameConvention()); BsonClassMap.RegisterConventions(conventions, x => true); var server = MongoServer.Create(connectionString); var database = server.GetDatabase("webapp"); Bind<MongoDatabase>().ToConstant(database); } } } 

Since this code is single, I have a bad feeling:

thanks

+4
source share
1 answer

MongoServer is great for single player. The same goes for MongoDatabase. Both of them are thread safe, and even if you create a new MongoServer and MongoDatabase each time, you will get the same instances, because they are cached under it until the connection string is exactly the same.

+8
source

All Articles