C# MongoDb Dependency Injection & Inversion of Control
<p>I am working with a Mongo Database for a custom CMS I am trying to create. I am relatively new to MVC, though not unfamiliar with OOP Languages, and I do grasp - vaguely I must admit - the concepts of Dependency Injection, and how Inversion of Control better tops the former off (in most cases).</p>

<p>I have tried looking for several days around the Internet of examples that could help facilitate me through creating my own DIs or IoC container, but to no avail yet. From my understanding so far, MVC (and C# in general) rely heavily on conventions, hence my question.</p>

<p>So far, using an example of a MongoDB University MVC Project, I have this Model setup that helps me connect with my mongo database:</p>

<pre><code>public class BlogContext
{
    private static readonly MongoCredential Credentials = MongoCredential.CreateCredential("admin", "root", "*****");
    private static readonly MongoServerAddress MongoServerAddress = new MongoServerAddress("localhost", 27017);
    private static readonly MongoClientSettings MongoClientSettings = new MongoClientSettings
    {
        Credentials = new[] { Credentials },
        Server = MongoServerAddress
    };
    public const string DATABASE_NAME = "testdb";
    public const string POSTS_COLLECTION_NAME = "cmstest";
    public const string USERS_COLLECTION_NAME = "users";

    // This is ok... Normally, they would be put into
    // an IoC container.
    private static readonly IMongoClient _client;
    private static readonly IMongoDatabase _database;

    static BlogContext()
    {
        _client = new MongoClient(MongoClientSettings);
        _database = _client.GetDatabase(DATABASE_NAME);
    }

    public IMongoClient Client =&gt; _client;

    public IMongoCollection&lt;Article&gt; Articles =&gt; _database.GetCollection&lt;Article&gt;(POSTS_COLLECTION_NAME);

    public IMongoCollection&lt;User&gt; Users =&gt; _database.GetCollection&lt;User&gt;(USERS_COLLECTION_NAME);
}
</code></pre>

<p>And <strong>for every controller action, or whenever I need to access my database, I initialize a BlogContext class</strong> and connect like so</p>

<pre><code>var blogContext = new BlogContext();
await blogContext.Articles.UpdateOneAsync(x =&gt; x.Id == id, update);
</code></pre>

<p><strong>I do know (and I've read several times) that Mongo uses a Connection Pool and it's thread-safe.</strong> I am looking for a better way to implement this. Be it by creating an Interface and using Dependency Injection, or by using an IoC Container, as even the class comment suggests.</p>

<p>I have read about implementations of IoC via Castle Windsor or Unity, but what I am looking for is an <strong>example</strong>. This way, perhaps I might be able to use this to create my own IoC Container.</p>

<p>Thank you in advance.</p>