C#MongoDb依赖注入&;控制反转

C#MongoDb依赖注入&;控制反转,c#,mongodb,dependency-injection,inversion-of-control,ioc-container,C#,Mongodb,Dependency Injection,Inversion Of Control,Ioc Container,我正在使用Mongo数据库来创建一个自定义CMS。我对MVC比较陌生,虽然对OOP语言并不陌生,但我确实掌握了依赖注入的概念,以及控制反转如何更好地超越前者(在大多数情况下) 我已经尝试了几天在互联网上寻找一些例子,这些例子可以帮助我创建自己的DIs或IoC容器,但都没有用。据我目前的理解,MVC(以及一般的C#)严重依赖于约定,因此我提出了这个问题 到目前为止,使用MongoDB大学MVC项目的一个示例,我有一个模型设置,可以帮助我连接mongo数据库: public class BlogCo

我正在使用Mongo数据库来创建一个自定义CMS。我对MVC比较陌生,虽然对OOP语言并不陌生,但我确实掌握了依赖注入的概念,以及控制反转如何更好地超越前者(在大多数情况下)

我已经尝试了几天在互联网上寻找一些例子,这些例子可以帮助我创建自己的DIs或IoC容器,但都没有用。据我目前的理解,MVC(以及一般的C#)严重依赖于约定,因此我提出了这个问题

到目前为止,使用MongoDB大学MVC项目的一个示例,我有一个模型设置,可以帮助我连接mongo数据库:

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 => _client;

    public IMongoCollection<Article> Articles => _database.GetCollection<Article>(POSTS_COLLECTION_NAME);

    public IMongoCollection<User> Users => _database.GetCollection<User>(USERS_COLLECTION_NAME);
}
我确实知道(而且我已经读过多次)Mongo使用连接池,而且它是线程安全的。我正在寻找更好的方法来实现这一点。无论是通过创建接口和使用依赖项注入,还是使用IoC容器,就像类注释所建议的那样

我读过关于通过Castle Windsor或Unity实现IoC的文章,但我想要的是一个示例。这样,也许我可以用它来创建自己的IoC容器


提前感谢您。

下面是一个使用ServiceStack.Funq的示例

您的global.asax应该如下所示

    protected void Application_Start()
    {
        Funq container = new Funq.Container();

        container.Register<IMyType>(c => new MyType());
        ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
    }

MyType
将由IoC容器自动连接。

有很多成熟的IoC容器具有广泛的MVC支持,因此无需实现自己的容器。您可以查看文档和基准测试,选择一些顶级产品,并在文档中查找MVC集成。通常包括示例。

依赖注入是控制反转的一种形式,它们不是相互竞争的概念-一个概念并不比另一个“更好”。Mark Seemann的书是一本很好的学习资源,其中包含了DIY依赖注入的示例,以及使用流行框架的示例。谢谢,为了更好地理解您需要ServiceStack.Mvc包。它依赖于包含ServiceStack.Funq的ServiceStack包。请注意,ServiceStack v3.9.71是最后一个非商业版本。ServiceStack v4是商用的,但使用时有一些限制。感谢您的回复。我必须尝试一下,看看我是否能更好地掌握国际奥委会集装箱,也许这些可以帮助我:1.2。
    protected void Application_Start()
    {
        Funq container = new Funq.Container();

        container.Register<IMyType>(c => new MyType());
        ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
    }
public class ExampleController : ServiceStackController
{
   public IMyType MyType {get;set;}

   public ActionResult Index(){
      return View();
   }
}