Autofac - Proper Use in a Multi-Module Application

I am working on an application that consists of many modules, some of which have dependencies on other modules. Now I decided to use Autofac to solve circular dependencies and improve the architecture as a whole.

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

Now I'm not sure how to implement Autofac. Do I need to have a link to autofac in every module in my application? This means that I have to register all components every time I want to solve the dependency ...

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

Is this a way to do this?

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 can just use wrapped Autofac to resolve dependencies ...

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

I hope someone tells me the best way to use Autofac.

thanks!

+6
source share
2 answers

Each project must have a dependency on the core autofac package if you want to use autofac modules.

Use autofac modules as described here: http://code.google.com/p/autofac/wiki/StructuringWithModules

Update

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

+7
source

A separate module for the container is not only the best option, but also the only option. AFAIK IoC container instances must be single point.

At least I used Unity in this way - you create a public static instance of the container, initialize it when the application starts, and then access it from all your modules.

-1
source

Source: https://habr.com/ru/post/911678/


All Articles