views:

150

answers:

1

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?

+5  A: 

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.

Philip Rieck