Use Unity (i): Unity Application Block Container Introduction

Source: Internet
Author: User
Tags exception handling

Starting today, I will be working with you to learn the use of this control reversal (inversion, IoC) and Dependency injection (Dependency injection, DI) containers. As for IOC and DI, you can refer to the "IOC container and Dependency injection model" by Martin Fowler.

Describe

Unity Application Block (Unity) is a lightweight, extensible, dependency injection container that supports constructors, properties, and method invocation injection. It provides developers with the following benefits:

simplifies the creation of objects, especially the hierarchical object structure and dependencies.

Allows developers to specify dependent requirements abstractions at run time or in configuration, and simplifies management of crosscutting concerns.

The service locator feature allows the client code to save or cache the container. This is particularly useful in asp.net Web applications where developers can persist containers to asp.net sessions or application.

Common scenes

In addition to addressing cross-cutting concerns such as logging, authentication, authorization, caching, and exception handling components, modern business systems typically consist of custom business objects and components that perform special or generic tasks in the application.

The key to successfully building such an application is to get decoupled or extremely loosely coupled designs. Loosely coupled applications are more flexible and easier to maintain. At the same time testing during development, can simulate the object of the pile (lightweight simulation of the implementation), which enhances the real dependency. For example, database connections, network connections, ERP connections, and rich user interface components.

Dependency injection is a major technique for building loosely coupled applications. It provides a way to handle dependencies between objects. For example, an object that processes user information may depend on other objects that access the data store, verify the information, and check whether the user is authorized to perform the update. Dependency injection technology ensures that the user class correctly initializes and assembles all of these objects, especially where dependency is abstract.

There are many benefits to using a container, but it changes the way the application is designed, especially for component-based development, and friends can use it selectively.

The most basic code

The following code shows the most basic usage of the Unity container.

IUnityContainer myContainer = new UnityContainer();
myContainer.Register<IMyObject, MyRealObject>();
IMyObject myRealObjectInstance = myContainer.Get<IMyObject>();

In the above code, the container's Register method is used first to register a type mapping of a Imyobject interface to a Myrealobject concrete class (note: The mapping can also be specified by a configuration file) and then obtained by the Get method Imyobject The specific object instance of the default mapping for the interface. Here, the Imyobject interface is mapped as a key, and the Get method uses it to find instances of the registered concrete class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.