代码改变世界

Unity

2010-08-20 17:14  W2R  阅读(344)  评论(0)    收藏  举报

What is Unity?

  Unity is a lightweight, extensible dependency injection container that supports interception, constructor injection, property injection, and method call injection. You can use Unity in a variety of different ways to help decouple the components of your applications, to maximize coherence in components, and to simplify design, implementation, testing, and administration of these applications.

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 add policies to objects, is implemented as a container extension.

 

Design Goals

  Unity was designed to achieve the following goals:

  • Promote the principles of modular design through aggressive decoupling.
  • Raise awareness of the need to maximize testability when designing applications.
  • Provide a fast and lightweight dependency injection container mechanism for creating new object instances and managing existing object instances.
  • Expose a compact and intuitive API for developers to work with the container.
  • Support a wide range of code languages, with method overrides that accept generic parameters where the language supports these.
  • Implement attribute-driven injection for constructors, property setters, and methods of target objects.
  • Provide extensibility through custom and third-party container extensions.
  • Provide the performance required in enterprise-level line of business (LOB) applications.

What does Unity do?

 

By using dependency injection frameworks and inversion of control mechanisms, you can generate and assemble instances of custom classes and objects that can contain dependent object instances and settings. The following sections explain the ways that you can use Unity, and the features it provides:

  • The Types of Objects Unity Can Create
  • Registering Existing Types and Object Instances
  • Managing the Lifetime of Objects
  • Specifying Values for Injection
  • Populating and Injecting Arrays, Including Generic Arrays
  • Intercepting Calls to Objects

When should I use Unity?

  Dependency injection provides opportunities to simplify code, abstract dependencies between objects, and automatically generate dependent object instances. In general, you should use Unity when:

  • You wish to build your application according to sound object oriented principles (following the five principles of class design, or SOLID), but doing so would result in large amounts of difficult-to-maintain code to connect objects together.
  • Your objects and classes may have dependencies on other objects or classes.
  • Your dependencies are complex or require abstraction.
  • You want to take advantage of constructor, method, or property call injection features.
  • You want to manage the lifetime of object instances.
  • You want to be able to configure and change dependencies at run time.
  • You want to intercept calls to methods or properties to generate a policy chain or pipeline containing handlers that implement crosscutting tasks.
  • You want to be able to cache or persist the dependencies across post backs in a Web application.

Scenarios for Interception

  Unity interception addresses the following scenarios:

  • Adding responsibilities to individual objects and not the entire class and avoiding a static solution, much as in a decorator pattern. In a manner similar to the way a decorator forwards requests to the object and enables you to perform additional actions before or after forwarding the request, interception intercepts the call to the target object and dynamically adds behaviors to individual objects without affecting any other objects. This can be useful in managing crosscutting concerns that access common features such as logging or validation.
  • To augment or modify the behavior from existing classes that you cannot modify, provided that they are interceptable by the available interception mechanisms.
  • Enabling the developer and administrator to configure the behavior of objects in an application through configuration when used in conjunction with a dependency injection (DI) container, by adding or removing behaviors that execute common tasks or add custom features.
  • Enabling the developer and administrator to capture calls to objects and add or remove behaviors that execute common tasks or add custom features at run time, but in this case independent of a DI container.
  • Minimizing the work required and the code that the developer must write to perform common tasks within an application, such as logging, validation, authorization, and instrumentation.
  • Reducing development time and cost, and minimizing bugs in complex applications that use common and shared tasks and services.