Skip to content
/ Autofac Public
forked from autofac/Autofac

An addictive .NET IoC container

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
License.txt
Notifications You must be signed in to change notification settings

mrvux/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

The easiest way to get Autofac into your application is to grab the NuGet packages. If you're not into NuGet, you can download Autofac from this site.

Getting Help

Need help with Autofac? We're ready to answer your questions on [Stack Overflow](http://stackoverflow.com/questions/tagged/autofac 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 has only just started. 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 this site or 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.

Our friendly and open community will help you to get the best from the project. You can also ask questions on Stack Overflow.

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

About

An addictive .NET IoC container

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
License.txt

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published