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

后端 未结 6 911
天涯浪人
天涯浪人 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:25

    Try LinFu. It's one of the most easiest and flexible containers out there and requires the least amount of code, and it's the only container in that IOC container comparison to pass ALL of the tests. Enjoy :)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-04 17:35

    The latest version of StructureMap, 2.5.2, not only lets you configure your container without XML as Ryan said but it also has the ability to AutoWire things according to a convention, which seems to be exactly what you're looking for.

    ObjectFactory.Initialize(x => x.Scan(scanner =>
                                            {
                                               scanner.TheCallingAssembly();
                                               scanner.WithDefaultConventions();
                                            }));;
    

    From the docs on IAssemblyScanner.WithDefaultConvents():

    Adds the DefaultConventionScanner to the scanning operation. i.e., a concrete class named "Something" that implements "ISomething" will be automatically added to the PluginType "ISomething"

    I've not personally done much with it as I already had the existing configuration that used the Fluent Interface. But it looks promising.

    EDIT: Jeremy Miller just put up a post on how to create your OWN Conventions...

    0 讨论(0)
  • 2021-02-04 17:40

    Castle Windsor allows this through the Register calls. A simple scenario is, let's say you have a lot of Controllers which implement an IController interface:

    container.Register(AllTypes.FromAssembly(assemblyA).BasedOn(typeof(IController));
    

    You can simplify this further by creating an IService interface (no members) and adding it to the above. This way when you create a service, it's automatically registered.

    0 讨论(0)
  • 2021-02-04 17:47

    I think you'll will find this feature in most of the containers out there. Take a look at this blog post. It's slightly aged but it will give you an idea. By now the active containers probably have better support.

    0 讨论(0)
  • 2021-02-04 17:51

    I use Ninject and StructureMap -- both allow you to wire up your configuration without XML.

    Not to be shamelessly promoting my site but I wrote a tutorial on using Ninject available here.

    0 讨论(0)
提交回复
热议问题