Autofac is an IoC container for Microsoft .NET. 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.
Autofac 2 is here! Read about the new features.
Adding Components¶
Components are registered with a ContainerBuilder:
var builder = new ContainerBuilder();
Autofac can use a Linq expression, a Type, or a pre-built instance as a component:
builder.Register(c => new TaskController(c.Resolve<ITaskRepository>())); builder.RegisterType<TaskController>(); builder.RegisterInstance(new TaskController());
Or, Autofac can find and register the component types in an assembly:
builder.RegisterAssemblyTypes(controllerAssembly);
Calling Build() creates a container:
var container = builder.Build();
To retrieve a component instance from a container, a service is requested. By default, components provide their concrete type as a service:
var taskController = container.Resolve<TaskController>();
To specify that the component’s service is an interface, the As() method is used at registration time:
builder.RegisterType<TaskController>().As<IController>(); // enabling var taskController = container.Resolve<IController>();
Expressing Dependencies¶
When Autofac instantiates a component, it satisfies the component's dependencies by finding and instantiating other components.
Components express their dependencies to Autofac as constructor parameters:
public class TaskController : IController
{
public TaskController(ITaskRepository tasks) { ... }
}In this case Autofac will look for another component that provides the ITaskRepository service and call the constructor of TaskController with that component as a parameter.
If there is more than one constructor on a component type, Autofac will use the constructor with the most resolvable parameters.
public TaskController(ITaskRepository tasks) public TaskController(ITaskRepository tasks, ILog log)
Default parameter values can be used to express optional dependencies (properties can be used instead if you prefer):
public TaskController(ITaskRepository tasks, ILog log = null)
Circular references can be constructed by declaring one of the parameters to be of type Lazy<T>.
public TaskController(Lazy<ITaskRepository> tasks)
Autofac understands an advanced vocabulary of “relationship types” like Lazy<T>, Func<T>, IEnumerable<T> and others, to vary the relationship between a component and its dependencies.
Flexible, Low-Friction Design¶
Autofac keeps out of your way and places as few constraints on your design as possible.
Zero Intrusion: Components don't need to reference Autofac.
Simple Extension Points: Activation events like OnActivating(e => e.Instance.Start()) can achieve a lot of customisation in very little code.
Robust Resource Management: Autofac takes on the burden of tracking disposable components to ensure that resources are released when they should be.
Multiple Services per Component: Fine-grained interfaces are great for controlling dependencies. Autofac allows one component to provide multiple services.
Flexible Module System: Strike a balance between the deployment-time benefits of XML configuration and the clarity of C# code with Autofac modules.
Status¶
Version 2.2 is the default supported build. Deployment requires a single assembly weighing in at about 100KB.
There is a growing number of integrations that make using Autofac with your application a snap. Support for several popular frameworks is available in the AutofacContrib download package.
Our friendly and open community will help you to get the best from the project.
Autofac is licensed under the MIT license, so you can comfortably use it in commercial applications (we still love contributions though.)