简体   繁体   中英

MongoDB Official C# Driver in ASP.NET MVC app with Ninject dependency injection

Does anyone have a code example that follows best practices of using MongoDB Official C# Driver with Ninject in ASP.NET MVC app?

Mine 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 singleton, I have a bad feeling about it :|

Thanks

MongoServer is just fine to keep as a singleton. The same is true of a MongoDatabase. They are both thread-safe and even if you create a new MongoServer and MongoDatabase every time, you will get back the same instances because they are cached underneath as long as the connection string is exactly the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM