autofac - correct use in multi module application

11,302

Each project need to have a dependency to the autofac core package if you would like to use autofac modules.

Use autofac modules as described here: http://autofac.readthedocs.io/en/latest/configuration/modules.html

Update

I describe why you should use modules here: http://www.codeproject.com/Articles/386164/Get-injected-into-the-world-of-inverted-dependenci

Share:
11,302
Fabian
Author by

Fabian

Updated on June 05, 2022

Comments

  • Fabian
    Fabian almost 2 years

    I'm working on an application that consists of many modules, with some having dependencies on other modules. I now decided to use Autofac to solve circular dependencies and improve the architecture in general.

    To configure autofac I use the xml method (http://code.google.com/p/autofac/wiki/XmlConfiguration).

    Now I am not sure on how to implement Autofac. Do I need to have a reference to autofac in each module in my application? Meaning that i have to register all components each time I want to solve a dependency...

    ContainerBuilder builder = new ContainerBuilder();
    builder.RegisterModule(new ConfigurationSettingsReader("autofac", configPath));
    IContainer container = builder.Build();
    IWhatever w = container.Resolve<IWhatever>();
    

    Is this the way to do it?

    Or is it better to Wrap Autofac in a separate Module ? With this approach I would have to register the modules only once (when the application starts) and could just use the wrapped Autofac to resolve dependencies...

    IWhatever w = container.Resolve<IWhatever>();
    

    I hope someone can tell me the best way to use Autofac.

    thanks!

  • jgauffin
    jgauffin about 12 years
    No, no and no. Having a singleton (of anything) are rarely a good idea. There are in most cases a better solution. Same thing here.
  • vlad2135
    vlad2135 about 12 years
    And which is it, you haven't provided one? You can not use code provided in the question in every module because this will create a lot of overhead. I don't know for sure, but it is possible that calling builder.Build instantiates all objects in the container - very bad idea.
  • jgauffin
    jgauffin about 12 years
    A lot of overhead? The container is built once in an applications lifetime.
  • Fabian
    Fabian about 12 years
    hm..now I'm not sure what to think ^^' I'm just not sure what the best practice is in this case. Referencing Autofac in each module sure creates a bit of overhead. To get the container I need to register all componentes each time as mentioned in my post...
  • vlad2135
    vlad2135 about 12 years
    @jgauffin what's the difference between "container is built once in .. lifetime" and singleton? ;) You contradict youself. As for the question - I've used Unity IoC in the way I've described (singleton in separate class in separate assembly) - there was no problems at all.
  • jgauffin
    jgauffin about 12 years
    Singleton = you follow a pattern which give you access to it everywhere at anytime. Building a container once = you gain access to it everywhere you use a container module (limited scope) during one invocation (limited time). that's the difference...
  • Dennis Kuypers
    Dennis Kuypers over 5 years
    Usually if you have a singleton that resolves your dependencies then you're probably using it as a Service Locator. This is considered an anti pattern. The point of using AutoFac is to decouple implementations. One can probably say that a single instance (over a singleton) makes it harder to get it wrong, because it forces you to think about your dependencies of your container.