Is there a .NET IoC that prefers convention over configuration?

后端 未结 6 903
天涯浪人
天涯浪人 2021-02-04 17:03

Simply put, if you had to use a IoC/DI container, thats easy to deploy (not a big fan of using config/xml file for everything), stable, good documentation and supports .net, whi

6条回答
  •  醉梦人生
    2021-02-04 17:27

    Perhaps you can use DI without a container?

    Here is an example that uses constructor injection:

    public static void Main() {
      ILogger logger = new FileLogger();
      ISession session = new ConcreteSession();
      IRepository repository = new MyDataRepositoryImpl(session, logger);
    
      IApplication app = new MyApplication();
      app.AddModule(new DataSelector(repository));
      app.AddModule(new Editor(repository, new MyEditorFactory(session)));
      app.AddModule(new LdapAuthenticator(session, logger));
      // ...
      app.Run();
    }
    

    If you do it this way, your executable module will have dependencies to everything, so you should limit its responsibility to just plugging things together. You can keep the bulk of your application's code clean in other modules.

    Often you don't need advanced frameworks to manage your dependencies with injection.

提交回复
热议问题