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?
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With