Reputation: 7395
Another MVC framework I've used - Symfony2 - uses a dependency injector to carefully configure and manage dependencies.
Does ASP.NET MVC have anything similar which would let me configure a MongoDB connection and then pass it down to my controller logic where it can be used?
Upvotes: 0
Views: 419
Reputation: 6019
There are many different dependency injection libraries you can use with Asp.net MVC, here's a (non comprehensive) list:
If you go looking for them in Nuget, typically there will be one package for the container itself, and a different package that adds the Asp.Net MVC plugins.
Take a look at them, figure out which flavor you like, then configure your mongo db connection to be per-request, add the connection to your controller constructor, and away you go. If you pick a lib and update your question, I'm sure someone will answer with specific code etc.
Upvotes: 1
Reputation: 1436
This is generally done in the web.config file
<appSettings>
<add key="MONGOHQ_URL" value="mongodb://localhost/YourDBName"/>
</appSettings>
The value can be referenced like this...
public class ConfigEnvironment
{
public static string GetConnectionString()
{
return ConfigurationManager.AppSettings.Get("MONGOHQ_URL") ??
"mongodb://localhost";
}
}
Use this to create your database
MongoDatabase.Create(ConfigEnvironment.GetConnectionString())
Upvotes: 1