8

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!

Fabian
  • 435
  • 2
  • 5
  • 11
  • 2
    The best way to use Autofac (or any other DI container) is to reference it as rarely as necessary. [This answer](http://stackoverflow.com/a/1410738/644812) might be helpful. – default.kramer Mar 26 '12 at 21:27
  • thank you, that is what I am thinking too. I'm just not sure what is the best practice to use Autofac... – Fabian Mar 27 '12 at 06:21

2 Answers2

8

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

Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
jgauffin
  • 99,844
  • 45
  • 235
  • 372
-2

Separate module for container is not only a better option, it is the only right option. AFAIK IoC container instances must be singletons.

At least I've used Unity this way - you create publicly available static instance of container, initialize it on application startup and then access it from all your modules.

vlad2135
  • 1,556
  • 12
  • 14
  • 4
    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. – jgauffin Mar 26 '12 at 14:58
  • 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. – vlad2135 Mar 26 '12 at 20:37
  • 2
    A lot of overhead? The container is built once in an applications lifetime. – jgauffin Mar 26 '12 at 20:57
  • 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... – Fabian Mar 27 '12 at 06:27
  • @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. – vlad2135 Mar 27 '12 at 09:25
  • 3
    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... – jgauffin Mar 27 '12 at 09:48
  • 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](http://blog.ploeh.dk/2010/02/03/ServiceLocatorisanAnti-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. – Dennis Kuypers Oct 20 '18 at 15:56