Generated by GPT-5-mini| Unity (dependency injection) | |
|---|---|
| Name | Unity (dependency injection) |
| Developer | Microsoft Patterns & Practices, Prism (software) |
| Released | 2006 |
| Programming language | C# |
| Operating system | Windows, Linux, macOS |
| Platform | .NET Framework, [.NET Core], [.NET Standard] |
| License | Apache License 2.0 |
Unity (dependency injection) is an open-source dependency injection container and inversion of control (IoC) framework for the [.NET] platform originally developed by Microsoft Patterns & Practices. It provides object lifetime management, dependency resolution, and interception to support modular design in applications built with Windows Presentation Foundation, ASP.NET, and Xamarin technologies. Unity emphasizes simplicity and extensibility for enterprise applications integrated with frameworks such as Entity Framework, NHibernate, and SignalR.
Unity implements dependency injection patterns to decouple components in applications using techniques popularized by Martin Fowler, Robert C. Martin, and the Gang of Four. It offers constructor, property, and method injection styles familiar to developers working with Visual Studio, ReSharper, and Roslyn-based tooling. Unity supports registration of types, named instances, lifetime managers, and interception via policies compatible with design practices from Domain-Driven Design, Command and Query Responsibility Segregation, and Model–View–ViewModel architectures used in Windows Phone and Universal Windows Platform projects.
Unity originated within Microsoft Patterns & Practices as part of efforts alongside projects such as Enterprise Library, Prism (software), and Microsoft Application Blocks during the mid-2000s. Contributors included engineers associated with Microsoft and the open-source community influenced by prior DI containers like StructureMap, Castle Windsor, and Autofac. Over successive releases Unity evolved to target the [.NET Core] and [.NET Standard] runtimes, adopting community-driven governance similar to projects hosted on GitHub and reflecting shifts seen in Mono and Xamarin ecosystems. Key milestones mirrored platform changes from .NET Framework 3.5 to .NET 5 and later consolidations in the .NET Foundation era.
Unity's architecture centers on a container abstraction that manages registrations and resolutions; this mirrors approaches in Inversion of Control literature and frameworks such as Spring Framework (for Java). Core concepts include: - Registrations: mapping between abstractions and concrete types comparable to patterns advocated by Erich Gamma and Richard Helm. - Lifetime managers: singleton, transient, and hierarchical scopes analogous to lifetime control in ASP.NET Core services and WCF behaviors. - Injection mechanisms: constructor, property, and method injection following idioms from Martin Fowler and Michael Feathers. - Interception: aspect-oriented features similar to techniques used in PostSharp and AspectJ for cross-cutting concerns like logging used by teams at Google, Amazon, and Facebook.
Unity exposes extension points for custom lifetime managers, container extensions, and policy injection models; these are architecturally comparable to extension patterns in MEF (Managed Extensibility Framework) and plugin systems used by Eclipse Foundation projects.
Unity includes features such as: - Type and instance registration with named mappings analogous to features in Autofac and Castle Windsor. - Lifetime management strategies used in enterprise products by IBM and Oracle. - Interception and policy injection to implement decorators and aspects seen in Aspect-Oriented Programming tooling. - Configuration via code and configuration files compatible with tools like NuGet and MSBuild. Extensions and integrations exist for frameworks including ASP.NET MVC, ASP.NET Web API, Prism (software), and ORMs like NHibernate and Entity Framework Core; community contributions also provide adapters for Serilog, NLog, and log4net.
Typical usage patterns show registering and resolving services within application startup code in Visual Studio or CI pipelines configured with Jenkins or Azure DevOps: - Registering a service type to an implementation and resolving it in a controller for ASP.NET MVC or in a view model for Prism (software). - Using lifetime managers to scope dependencies per-request in ASP.NET or per-session in SignalR hubs for real-time features used at Slack Technologies and Trello. - Applying interception to add logging frameworks like Serilog or telemetry systems such as Application Insights deployed by teams at Microsoft.
Code examples frequently appear in samples and documentation alongside projects such as Prism (software), example repositories on GitHub, and educational content created by authors like Mark Seemann and contributors to Stack Overflow.
Unity is often compared to other .NET DI containers: - Autofac: advanced module and registration scanning features used in Stack Overflow and GitHub projects. - Castle Windsor: mature interception and lifestyle management used in legacy Enterprise Library scenarios. - Microsoft.Extensions.DependencyInjection: the lightweight built-in container in ASP.NET Core recommended by Microsoft for modern web applications. - StructureMap and Ninject: alternative APIs and configuration models favored in community projects and academic examples from O'Reilly Media.
Trade-offs include Unity's extensibility versus the performance and minimalism of Microsoft.Extensions.DependencyInjection and the richer feature sets of Autofac for modular application composition.
Unity has seen adoption in enterprise applications built with WPF, Windows Forms, ASP.NET and cross-platform apps using Xamarin. Community support is provided through repositories on GitHub, discussions on Stack Overflow, and content from Microsoft developer networks such as MSDN and Channel 9. Educational resources and third-party tooling from publishers like Pluralsight, O'Reilly Media, and authors affiliated with Pearson Education supplement official documentation. The project's lifecycle and maintenance reflect contributions from corporate and independent developers, and interoperability with [.NET] ecosystem projects keeps it relevant in legacy modernization efforts.
Category:.NET libraries