Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autofac - correct use in multi module application

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!

like image 686
Fabian Avatar asked Mar 26 '12 14:03

Fabian


People also ask

Why should I use Autofac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

What is module in Autofac?

A module can do many things : it can register new components and/or subscribe to Autofac events. In this case, the LoggingModule doesn't register ILog . It intercepts the preparation of other components using the Preparing event and add a new Parameter that will create the ILog if needed.


1 Answers

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

like image 74
jgauffin Avatar answered Nov 14 '22 03:11

jgauffin