autofac - correct use in multi module application
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
Fabian
Updated on June 05, 2022Comments
-
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 about 12 yearsNo, 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 about 12 yearsAnd 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 about 12 yearsA lot of overhead? The container is built once in an applications lifetime.
-
Fabian about 12 yearshm..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 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 about 12 yearsSingleton = 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 over 5 yearsUsually 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.