My favorites | Sign in
Logo
                

Autofac is a fresh approach to IoC in .NET that utilises modern C#:

var builder = new ContainerBuilder();

builder.Register<Straight6TwinTurbo>().As<IEngine>().FactoryScoped();

builder.Register(c => new Skyline(c.Resolve<IEngine>(), Color.Black))
  .As<ICar>()
  .FactoryScoped();

using (var container = builder.Build())
{
  var car = container.Resolve<ICar>();
  car.DriveTo("Byron Bay");
}

Autofac was designed with modern .NET features and obsessive object-orientation in mind. It will change the way you approach dependency injection in .NET.

Flexible Component Instantiation

Autofac supports the reflection-based autowiring capabilities found in other .NET containers. Given a type (as in the StraightSixTwinTurbo example above,) Autofac will automatically select constructors and inject dependencies appropriately.

Autofac can also use expressions to create components, as in the example above. This makes the container fast, very flexible, and easy to integrate into existing applications.

var defaultLog = new ConsoleLog();
builder.Register(c => new Connection(){ Log = c.ResolveOptional<ILog>() ?? defaultLog });

There is no need to spend time looking up reflection- or XML-based syntax to achieve things that are simple to express in C#.

Predictable Resource Management

The dynamic nature of applications built with dependency injection means that determining which components should be disposed of and when is much harder. Autofac takes on this burden by tracking instantiations and dependencies within a specified scope, so that a single Dispose() call can clean up everything that should be cleaned up and nothing more.

var container = // ...
using (var inner = container.CreateInnerContainer())
{
  var controller = inner.Resolve<IController>();
  controller.Execute(); // use controller..
}

In the above example, not only will controller be disposed of by the using block (if it implements IDisposable,) but so will any any of its dependencies, and their dependencies, and so on.

Pragmatic Design...

...for the purist! The small things can make all the difference. Autofac keeps out of your way and places as few constraints on your design as possible.

Zero Intrusion: Components never need to reference Autofac. Expressions are so flexible that you can often register components built without dependency injection in mind at all.

Flexible Module System: Don't let your application become a tangle of XML and seemingly random configuration parameters - structure it with Autofac modules.

Autowiring: Even though you may use expressions to register many components, Autofac can choose constructors and build up instances for you if you need it.

XML Configuration Support: Overuse can be ugly, but when it comes to deployment time, XML configuration is very handy.

Multiple Services per Component: Many designers use fine-grained interfaces to control dependencies. Autofac allows one component to provide multiple services.

No Implementation Type Dependencies: After registration, Autofac needs no knowledge of implementation types so you can vary them however you like - even on a call-by-call basis.

Status

Version 1.4 is available for both .NET and Silverlight. Deployment requires a single assembly weighing in at 100KB.

There are 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.

Future plans are recorded in the roadmap and issue tracker.

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!)









Hosted by Google Code