Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage autofac container setup

One of my team members decided to use autofac on one of our services and because we wanted to try it out we stuck with it.

Now some time has passed and the container setup method has grown! It so big that we are having problems with it.

Splitting it up did not bring the results we looked for. Maybe we are just using it wrong.

So my question is: How can we manage the container setup? Can we dump into XML or are there any other best practices?

like image 734
Dejan Avatar asked Oct 19 '09 13:10

Dejan


People also ask

What is container in Autofac?

Autofac is an IoC container for Microsoft . NET 4.5, Silverlight 5, Windows Store apps, and Windows Phone 8 apps. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular . NET classes as components.

How do I get Autofac containers?

From Visual Studio, you can get it via NuGet. The package name is Autofac. Alternatively, the NuGet package can be downloaded from the GitHub repository (https://github.com/autofac/Autofac/releases).


1 Answers

There are many ways to manage container setup with autofac.

One of the most common ways is to use a Module and register it with the builder. You can break up multiple groups of registration this way:

public class DALModule : Module
{
   protected override void Load(ContainerBuilder builder)
   {
      builder.Register<SomeDataSomething>().As<IDataSomething>();  
      builder.Register<SomeOtherSomething( c => SomeOtherSomething.Create());
      //and so on
   }
}

Then register these broken up modules with the builder either via code or XML. (a simple call to builder.RegisterModule( new DALModule()) would do it here). See the wiki page on Structuring with Modules.

Or, you can use only XML files (or use XML and modules together). See the wiki page on XML config for this.

like image 71
Philip Rieck Avatar answered Oct 03 '22 04:10

Philip Rieck