layout | title | css | |
---|---|---|---|
home |
Home |
|
Autofac is an addictive Inversion of Control container for .NET Core, ASP.NET Core, .NET 4.5.1+, Universal Windows apps, and more.
Build up containers with lambdas, types, or pre-built instances of components. You can also scan assemblies for registrations.
var builder = new ContainerBuilder();// Register individual components builder.RegisterInstance(new TaskRepository()) .As<ITaskRepository>(); builder.RegisterType<TaskController>(); builder.Register(c => new LogManager(DateTime.Now)) .As<ILogger>();
// Scan an assembly for components builder.RegisterAssemblyTypes(myAssembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces();
var container = builder.Build();
Let Autofac inject your constructor parameters for you. It can also handle property and method injection.
public class TaskController { private ITaskRepository _repository; private ILogger _logger; // Autofac will automatically find the registered // values and pass them in for you. public TaskController( ITaskRepository repository, ILogger logger) { this._repository = repository; this._logger = logger; } }
</div>
</div>
</div>
<div class="marketing">
<div class="row-fluid">
<div class="span6">
<h2>Flexible Module System</h2>
<p>Strike a balance between the deployment-time benefits of <a href="https://autofac.readthedocs.io/en/latest/configuration/xml.html">XML configuration</a> and the power of code with <a href="https://autofac.readthedocs.io/en/latest/configuration/modules.html">Autofac modules</a>.</p>
// Specify complex registrations in code public class CarTransportModule : Module { public bool ObeySpeedLimit { get; set; } protected override void Load(ContainerBuilder builder) { builder.RegisterType<Car>().As<IVehicle>(); if (ObeySpeedLimit) builder.RegisterType<SaneDriver>().As<IDriver>(); else builder.RegisterType<CrazyDriver>().As<IDriver>(); } }
<!-- Change deployment-time behavior with XML --> <autofac> <module type="CarTransportModule"> <properties> <property name="ObeySpeedLimit" value="true" /> </properties> </module> </autofac>
</div>
<div class="span6">
<h2>Simple Extension Points</h2>
<p>Autofac provides <a href="https://github.com/autofac/Autofac/wiki/Lifetime-Events">activation events</a> to let you know when components are being activated or released, allowing for a lot of customization with little code.</p>
var builder = new ContainerBuilder(); // Once a listener has been fully constructed and is // ready to be used, automatically start listening. builder.RegisterType<Listener>() .As<IListener>() .OnActivated(e => e.Instance.StartListening()); // When a processor is being constructed but before // it's ready to be used, call an initialization method. builder.RegisterType<Processor>() .OnActivating(e => e.Instance.Initialize()); var container = builder.Build();
</div>
</div>
</div>
<div class="marketing">
<h1>Dive In</h1>
<p class="marketing-byline">Want to download Autofac or learn more? Here's how.</p>
<div class="row-fluid">
<div class="span4">
<img class="marketing-img" src="img/icon_arrow.png" alt="Download" width="128" height="128" />
<h2>Download</h2>
<p>The easiest way to get Autofac is through NuGet. <a href="https://www.nuget.org/packages?q=Owner%3A%22Autofac%22+Autofac*">Here are the Autofac packages in the NuGet Gallery</a>.</p>
</div>
<div class="span4">
<img class="marketing-img" src="img/icon_school.png" alt="Learn" width="128" height="128" />
<h2>Learn</h2>
<p>If you're new to Autofac, <a href="https://autofac.readthedocs.io/en/latest/getting-started/index.html">the Quick Start guide</a> is a good place to start. There's also <a href="https://autofac.readthedocs.io/">an official documentation site</a>, <a href="/apidoc/">API documentation</a>, and <a href="https://github.com/autofac/Autofac/wiki">lots of info on the Autofac wiki</a>. For questions, hit us up on <a href="https://stackoverflow.com/questions/tagged/autofac">StackOverflow</a>.</p>
</div>
<div class="span4">
<img class="marketing-img" src="img/icon_people.png" alt="Get Involved" width="128" height="128" />
<h2>Get Involved</h2>
<p>Found an issue? <a href="https://github.com/autofac/Autofac/issues">Let us know!</a> Want to help us improve Autofac? <a href="https://github.com/autofac/Autofac">Check out the source</a> and our <a href="https://autofac.readthedocs.io/en/latest/contributors.html">contributor's guide</a>, and drop us a line on <a href="https://groups.google.com/forum/#forum/autofac">the discussion forum</a>!</p>
</div>
</div>
</div>