Unity 2.1 – May 2011, Aug 2012
Unity is a lightweight, extensible dependency injection container with support for instance and type interception.
Microsoft Enterprise Library 5.0 Hands-on Labs contain the labs for Unity 2.1, including the interception mechanisms.
On This Page
Feedback and Support
Authors and Contributors
Unity is a lightweight, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages:
- Simplified object creation, especially for hierarchical object structures and dependencies
- Abstraction of requirements; this allows developers to specify dependencies at run time or in configuration and simplify management of crosscutting concerns
- Increased flexibility by deferring component configuration to the container
- Service location capability, which allows clients to store or cache the container
- Instance and type interception
Unity is a general-purpose container for use in any type of Microsoft® .NET Framework-based application. It provides all of the features commonly found in dependency injection mechanisms, including methods to register type mappings and object instances, resolve objects, manage object lifetimes, and inject dependent objects into the parameters of constructors and methods and as the value of properties of objects it resolves.
In addition, Unity is extensible. You can write container extensions that change the behavior of the container, or add new capabilities. For example, the interception feature provided by Unity, which you can use to capture calls to objects and add additional functionality and policies to the target objects, is implemented as a container extension.
The following sections of this guidance describe what Unity can do, when you should choose Unity, and the ways that you can use it in your applications:
- What Does Unity Do? This topic provides a brief overview that will help you to understand what Unity can do, and explains some of the concepts and features it incorporates. It also provides a simple example of the way that you can write code to use Unity.
- When Should I Use Unity? This topic will help you to decide if Unity is suitable for your requirements. It explains the benefits of using Unity, and any alternative techniques you may consider. It also provides details of any limitations of Unity that may affect your decision to use it.
- About This Release of Unity. This topic contains information about the changes in this release, the target audience and system requirements, migration and side-by-side execution, and links to other Microsoft patterns & practices resources.
- Configuring Unity. This topic describes how you can populate a Unity container with the type registrations, mappings, extensions, and other information required by your application.
- Using Unity in Applications. This topic explains how to use Unity in your own applications. It explains how to add Unity to your application, how to resolve objects, and how to take advantage of the many other capabilities of Unity.
- Design of Unity. This topic explains the decisions that went into designing Unity and the rationale behind those decisions.
- Extending and Modifying Unity. This topic explains how to extend Unity and how to modify the source code.
- Deployment and Operations. This topic explains how to deploy and update the Unity assemblies and use the instrumentation exposed by Unity.
A working knowledge of .NET programming is required.
The following are the system requirements for using Unity:
- Operating system: Microsoft Windows® 7 Professional, Enterprise or Ultimate; Windows Server 2003 R2; Windows Server 2008 with Service Pack 2; Windows Server 2008 R2; Windows Vista with Service Pack 2; or Windows XP with Service Pack 3.
- Microsoft .NET Framework 3.5 with Service Pack 1 or Microsoft .NET Framework 4.0.
For a rich development environment, the following are recommended:
- Microsoft Visual Studio® 2008 Development System with Service Pack 1 (any edition) or Microsoft Visual Studio 2010 Development System (any edition).
To run the unit tests, the following are also required:
- Microsoft Visual Studio 2008 Professional, Visual Studio 2008 Team Edition, Visual Studio 2010 Premium, Visual Studio 2010 Professional, or Visual Studio 2010 Ultimate edition.
- Moq v3.1 assemblies.
For the Data Access Application Block, the following is also required:
- A database server running a database that is supported by a .NET Framework 3.5 with Service Pack 1 or .NET Framework 4.0 data provider. This includes SQL Server® 2000 or later, SQL Server 2005 Compact Edition, and Oracle 9i or later. The database server can also run a database that is supported by the .NET Framework 3.5 with Service Pack 1 or the .NET Framework 4.0 data providers for OLE DB or ODBC.
Out of the box, Unity binary assemblies are targeted at .NET Framework 3.5 SP1. Unity solution and project files are provided in Visual Studio 2008 and Visual Studio 2010 formats. For information on building Unity from source and targeting various versions of the .NET framework, see Building Enterprise Library from the Source Code.
Unity was designed to achieve the following goals:
- To promote the principles of modular design through aggressive decoupling of components, business objects, and services
- To raise awareness of the need to maximize testability when designing applications
- To provide a fast and lightweight dependency injection container mechanism for creating new object instances and managing existing object instances
- To expose a compact and intuitive API for developers to use with the container
- To support a wide range of code languages, with method overrides that accept generic parameters where the language supports these
- To implement attribute-driven injection for constructors, property setters, and methods of target objects
- To provide extensibility through custom and third-party container extensions
- To provide the performance required in enterprise-level line-of-business (LOB) applications
The Unity 2.1 release contains the following main changes to the previous release (Unity 1.2 – October 2008):
- Streamlined configuration schema
- A simplified API for static factories and interception
- The capability to add interface implementation through interception
- Additional types of lifetime manager
- Deferred resolution (automatic factories)
- Bug fixes
- A reduction of the number of assemblies
For the full list of changes, please refer to the Release Notes.
For an introduction to dependency injection, see the article Inversion of Control Containers and the Dependency Injection pattern by Martin Fowler.
For information about the key features of Unity, see Unity QuickStarts. This topic demonstrates some of the key features of Unity and is a good starting point for working with Unity.
You can also use the Hands-on Labs to practice Unity capabilities in various contexts. Unity Hands-On Labs are a part of the Microsoft Enterprise Library 5.0 Hands-On Labs set and are available as a separate download.
Unity, like many patterns & practices deliverables, is associated with a community site. On this community site, you can post questions, provide feedback, or connect with other users to share ideas. Community members can also help Microsoft plan and test future releases of Unity, and download additional content such as extensions and training material.
Feedback and Support
Questions? Comments? Suggestions? To provide feedback about Unity, or to get help with any problems, visit the Unity Community site. The message board on the community site is the preferred feedback and support channel because it allows you to share your ideas, questions, and solutions with the entire community. Unity is a guidance offering, designed to be reused, customized, and extended. Code-based guidance is shipped "as is" and without warranties. Customers can obtain support through Microsoft Premier Support Services for a fee, but the code is considered by Microsoft support staff as user-written.
- Microsoft Unity Container Home Page
- Microsoft Enterprise Library Home Page
- Microsoft Enterprise Library 5.0