Generated by GPT-5-mini| Autofac | |
|---|---|
| Name | Autofac |
| Operating system | Cross-platform |
| Programming language | C# |
| License | MIT License |
Autofac Autofac is an inversion of control (IoC) container and dependency injection (DI) framework for the .NET Framework and .NET Core. It facilitates decoupling of components in applications developed with C#, Visual Basic .NET, F#, ASP.NET Core, and related Microsoft ecosystems by managing object lifetimes and resolving service dependencies. Autofac is used across enterprise projects, cloud services, desktop solutions, and open source libraries to simplify composition and testing.
Autofac provides a runtime composition model that supports constructor injection, property injection, and method injection for components in .NET Framework, .NET Core, Mono, and Xamarin-based applications. It targets scenarios involving ASP.NET, Azure, Docker containers, and microservice architectures exemplified by patterns used in Kubernetes deployments. The project interacts with tooling from Microsoft Visual Studio, JetBrains Rider, and CI/CD systems like Jenkins, Azure DevOps, and GitHub Actions to enable automated builds and dependency graph validation.
Autofac implements lifetime scopes, instance registration, and service resolution strategies compatible with frameworks such as ASP.NET Core MVC, ASP.NET Web API, and SignalR. It supports module composition analogous to patterns in NServiceBus and MassTransit by enabling registration modules similar to those used in Enterprise Service Bus designs. Advanced features include interception compatible with Castle Windsor idioms, open generic support common to Entity Framework, and metadata-driven resolution used alongside AutoMapper. Registration sources and keyed services enable scenarios found in Windows Presentation Foundation and Universal Windows Platform projects.
Core components include a component registry, lifetime scope manager, and resolver pipeline reflecting inversion patterns articulated in literature from Martin Fowler and Eric Evans. The architecture delineates responsibilities between container builders, resolved instances, and disposal patterns influenced by IDisposable semantics in Microsoft .NET. Components interact with logging subsystems like Serilog, NLog, and log4net to trace composition; they interoperate with testing frameworks such as xUnit.net, NUnit, and MSTest for unit and integration tests. Module patterns mirror approaches in Prism (software), while configuration integration supports Microsoft.Extensions.Configuration and policy patterns seen in Polly.
Typical usage starts by creating a container builder, registering services and implementations, and resolving service graphs at application startup used in ASP.NET Core host building. Developers often combine Autofac registrations with middleware pipelines in Owin and Katana hosts or use factory patterns similar to those in Factory Method implementations found in Gang of Four design examples. Example scenarios include dependency resolution for data access layers built on Entity Framework Core, message handlers in RabbitMQ integrations, and command handlers in CQRS architectures. Tests frequently register mocks from libraries like Moq or NSubstitute to substitute concrete implementations during resolution.
Autofac integrates with web frameworks such as ASP.NET Core MVC, NancyFX, and GraphQL servers implemented with Hot Chocolate or GraphQL.NET. Extensions exist for service buses including NServiceBus and MassTransit, and for container orchestration commonly used with Docker Compose and Kubernetes Helm. Community packages facilitate integration with ORMs like Dapper and NHibernate, authentication stacks like IdentityServer4, and caching layers such as Redis via StackExchange.Redis. Third-party adaptors allow interoperability with other IoC containers including Simple Injector, StructureMap, and Autofac.Extras.DynamicProxy bridges to Castle DynamicProxy.
Autofac emphasizes flexibility which can trade off against raw resolution speed compared with static DI approaches used by the built-in Microsoft.Extensions.DependencyInjection container. Benchmarks often compare Autofac to alternatives like Simple Injector and StructureMap under workloads typical for ASP.NET Core request pipelines, showing differing startup and resolution characteristics. Limitations include potential complexity in large registration graphs, increased memory retention with long-lived lifetimes, and the need for careful disposal patterns aligned with IDisposable and scope lifetimes. Profiling with tools such as dotTrace, dotMemory, and PerfView helps identify bottlenecks in high-throughput services.
Autofac originated within the .NET community as an open source project responding to dependency injection patterns popularized by developers influenced by Martin Fowler and Robert C. Martin. Over time the project evolved alongside major platform milestones such as the release of .NET Core and the consolidation of ASP.NET Core, adapting to dependency injection abstractions introduced by Microsoft and contributions from maintainers active in repositories hosted on GitHub. The project’s ecosystem expanded through community contributions tied to conferences like NDC Conferences, Build (Microsoft conference), and Microsoft Ignite, and through collaborations with library authors from projects like AutoMapper, MediatR, and Serilog.
Category:Dependency injection containers