Skip to content

linven/Autofac

 
 

Repository files navigation

Autofac

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.

NuGet Packages

You can get Autofac by grabbing the latest NuGet packages or using our NuGet script builder to get exactly what you need. A few older versions remain for download here.

Getting Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.

Getting Started

Our Getting Started tutorial walks you through integrating Autofac with a simple application and gives you some starting points for learning more.

Adding Components

Components are registered with a ContainerBuilder:

var builder = new ContainerBuilder();

Autofac can use a Linq expression, a .NET 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.

Highlights

Autofac keeps out of your way and places as few constraints on your design as possible.

Simple Extension Points: Activation events like OnActivating(e => e.Instance.Start()) can achieve a lot of customization 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.

Flexible Module System: Strike a balance between the deployment-time benefits of XML configuration and the clarity of C# code with Autofac modules.

Status

Autofac moved to GitHub on the 22nd January, 2013. The process of cleaning up the issues list and wiki content is ongoing. You may stumble across some invalid links while we sort out problems from the migration. The code and NuGet packages all remain in a consistent state.

You can get the latest releases from NuGet. Release notes are available on the wiki.

If you're feeling bold, you can get continuous integration builds from MyGet.

There is a growing number of integrations that make using Autofac with your application a snap. Support for several popular frameworks is also available through the "Extras" packages.

Autofac is licensed under the MIT license, so you can comfortably use it in commercial applications (we still love contributions though).

Contributing

Refer to the Readme for Autofac Developers for setting up, building Autofac and generating the related documentation. We also have a contributors guide to help you get started.

About

An addictive .NET IoC container

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.1%
  • Other 0.9%