Unity 1.0 Chinese document: 1-Unity Introduction

Source: Internet
Author: User
This is the first part of Unity 1.0 document translation. For more information, see http://forum.entlib.net.cn/showtopic-235.aspx.

Unity ApplicationProgramUnity is a lightweight and scalable dependency injection container that supports constructor, attribute, and method call injection. It has the following advantages:

    • Simplified object creation, especially hierarchical object structure and dependency, and simplified applicationCode.
    • Supports requirement abstraction, which allows developers to specify dependencies during runtime or configuration, and simplifies management of cross-concern.
    • Improve flexibility by assembling configurations to containers with latency.
    • The service positioning function allows the customer to save or cache the container code. This is particularly useful when developers can persistently send containers to ASP. NET sessions or ASP. NET web applications in applications.

This topic contains a series of useful chapters that provide information that helps determine whether the unity Application Block meets your needs. The chapters of this topic are as follows:

    • Common scenarios
    • Sample application code
    • Highlights of Unity application blocks
    • Determine when to use the unity Application Block

This document also contains the following topics:

    • Use the unity Application Block to develop applications. This topic explains how to use the unity Application Block in an application. It lists system requirements, explains how to configure application blocks to complete common tasks, and shows how to add application code to the desired application blocks. The block attributes are also listed.
    • Key scenarios. This topic demonstrates how to use application blocks to perform the most common operations.
    • The design of the Unity Application Block. This topic explains application block design considerations and other considerations.
    • Extends and modifies the unity Application Block. This topic explains how to scale application blocks and how to modifySource code.
    • Deployment and operations. This topic explains how to deploy and update the application block assembly and use the metrics exposed by the program block.
    • Unity quick start. This topic describes how to perform common operations in an application.

More information

For more information, see the following guide:

    • Unity community site on codeplex
    • IOC container and dependency injection mode
    • Design Patterns: dependency injection in msdn

Common scenarios

The Unity Application Block solves the problems faced by developers who are busy with component-based software engineering. In addition to the components that independently address cross-cutting concerns such as logs, authentication, authorization, caching, and exception handling, modern business systems, it also consists of custom business objects and components that complete special or general tasks in applications.

The key to successfully building such an application is to obtain a decoupled or extremely loosely coupled design. Loosely Coupled applications are more flexible and easy to maintain. It is also easy to test during development. It can simulate the pile of objects with strong substantive dependencies (implementation of lightweight simulation ). For example, database connection, network connection, ERP connection, and rich user interface components.

Lai injection is a major technology used to build loosely coupled applications. It provides methods to process dependencies between objects. For example, an object that processes user information may depend on accessing data storage, verifying information, and checking
Whether the user is authorized to execute updates to other objects. The dependency Injection Technology ensures that the user class correctly initializes and assembles all these objects, especially the dependency that can be abstracted.

The following design patterns define the simplified processing architecture and development methods:

    • Control reversal (IOC) ModeThis is a general mode, which describes the technology used to support the plug-in architecture of instances where objects can "find" other objects they need.
    • Dependency injection (DI)
      Mode, which is IOC
      A special case of pattern is an internal Interface Programming Technology Based on changing the behavior of objects without changing the class. Developers write class code implementing interfaces and inject dependencies using containers based on interfaces or object types.
      To the class. The technology used to inject object instances is interface injection, constructor injection, attribute (seter) injection, and method call injection.

The scenarios in this Guide include the following scenarios to demonstrate how to use the unity Application Block:

    • Create a unity container
    • Parse objects by type
    • Resolve objects by type and Registration Name
    • Parse all objects of a specific type
    • Use buildup to bind an object instead of creating it by the container
    • Annotating object used to construct function Injection
    • Annotating object used for Attribute (seter) Injection
    • Annotating object used for method call Injection

In addition, the Quick Start of Unity demonstrates many of these technologies, including a simple implementation of the MVC mode and an implementation of the event Broker Service as a custom container extension.

Sample application code

By using the dependency injection framework and control inversion mechanism, developers can generate and form custom classes and objects that can contain dependent object instances and settings. Unity
The Application Block supports this function, allows developers to use techniques such as container injection, constructor injection, property injection, and method call injection to generate and combine objects and all dependent objects and settings.

The Unity Application Block exposes two methods for registering types and ing with containers:

    • Registertype. This method
      Register a type with a container. When appropriate, the container will build an instance of the specified type. This can be achieved through the features of the class or calling resolve
      Method. The lifecycle of the constructed object is the same as that specified in the method parameter. If no lifecycle value is specified, a temporary lifecycle is registered for the type, which means
      Will create a new instance for each call to resolve.
    • Registerinstance. This method registers an existing instance of the specified type to the container and carries the specified lifecycle. The container returns an existing instance within the lifecycle. If no lifecycle is specified, the instance will have the lifecycle controlled by the container.

Type of configuration container Registration

As an example of reload of the registertype and resolve methods, the following code registers a ing for the interface imyservice. The specified container will return the customerservice class (implementing the imyservice Interface) instance.

C #
Iunitycontainer mycontainer = new unitycontainer ();
Mycontainer. registertype <imyservice, customerservice> ();
Imyservice myserviceinstance = mycontainer. Resolve <imyservice> ();

Visual Basic

Dim mycontainer as iunitycontainer = new unitycontainer ()
Mycontainer. registertype (of imyservice, customerservice )()
Dim myserviceinstance as imyservice = mycontainer. Resolve (of imyservice )()

Configure container registration for existing object instances

As an example of using registerinstance and resolve method to overload, the following code registers an existing instance of the loggingservice class that implements the imyservice interface and then obtains this instance:

C #
Iunitycontainer mycontainer = new unitycontainer ();
Loggingservice myexistingobject = new loggingservice ();
Mycontainer. registerinstance <imyservice> (myexistingobject );
Imyservice myserviceinstance = mycontainer. Resolve <imyservice> ();

Visual Basic

Dim mycontainer as iunitycontainer = new unitycontainer ()
Dim myexistingobject as new loggingservice ()
Mycontainer. registerinstance (of imyservice) (myexistingobject)
Dim myserviceinstance as imyservice = mycontainer. Resolve (of imyservice )()

Constructor Injection

As an example of constructor injection, if developers use the resolve
A constructor defines one or more dependencies on other classes.
The container automatically creates the object instance specified in the constructor parameters. For example, the following code demonstrates
Customerservice class.

C #
Public class customerservice
Public customerservice (loggingservice myserviceinstance)
// Work with the dependent instance
Myserviceinstance. writetolog ("somevalue ");

Visual Basic

Public class customerservice
Public sub new (myserviceinstance as loggingservice)
'Work with the dependent instance
Myserviceinstance. writetolog ("somevalue ")
End sub
End Class

At runtime, developers use the Resolve Method of the container to create an instance of the customerservice class, which causes the instance generation framework to inject an instance of a specific class of loggingservice into the customerservice class.

C #
Iunitycontainer ucontainer = new unitycontainer ();
Customerservice myinstance = ucontainer. Resolve <customerservice> ();

Visual Basic

Dim ucontainer as iunitycontainer = new unitycontainer ()
Dim myinstance as customerservice = ucontainer. Resolve (of customerservice )()

Property (configurator) Injection

In addition to the constructor injection described above, unity
The Application Block also supports property and method call injection. The following code demonstrates property injection. The productservice class exposes one reference to another named supplierdata.
Class (not defined in the following code. To force dependency object dependency injection, developers must use the dependency attribute declaration as in the following code:

C #
Public class productservice
Private supplierdata supplier;

Public supplierdata supplierdetails
Get {return supplier ;}
Set {supplier = value ;}

Visual Basic. net

Public class productservice
Private supplier as supplierdata

<Dependency ()> _
Public property supplierdetails () as supplierdata
Return supplier
End get
Set (byval value as supplierdata)
Supplier = Value
End set
End Property

End Class

Now, using the unity Application Block to create an instance of the productservice class will automatically generate an instance of the supplierdata class and set it to the value of the supplierdetails attribute of the productservice class.

Note: : For more details about how to use these and other unity Application Block features in an application, see key scenarios.

Highlights of Unity application blocks

The Unity application block contains the following features:

    • It provides a mechanism for building (or combining) object instances. It may contain other dependent object instances.
    • It exposes the registertype method that supports container configuration using ing and objects (including Singleton instances), and returns the resolve method for building object instances that can contain any dependent objects.
    • Control inversion (IOC) is provided by injecting pre-configured objects to classes built by the program block. Developers can specify the interface or class type (constructor injection) in the build function, or initialize the property injection and method call injection through the application attributes and method features.
    • Container hierarchy is supported. The container may have sub-containers that allow the query of object positioning to be passed from the sub-container to the parent container.
    • You can read configuration information from standard configuration systems such as XML files and use it to configure containers.
    • Object class definition is not required. You do not need to apply features to the class (except for using the property or method call injection), as well as unrestricted class declarations.
    • Supports custom container extensions that developers can implement. For example, allow other object building methods and container features, such as caching.

Determine when to use the unity Application Block

Dependency injection provides the opportunity to simplify code, abstract object dependencies, and automatically generate dependent object instances. However, processing may have a small impact on performance, and will increase complexity when only simplifying existing dependencies.

Generally, you can use the unity application block at the following time:

    • Objects and classes may depend on other objects and classes.
    • Dependencies are complex or abstract.
    • Use constructor, method, or attribute to call the injection feature.
    • The lifecycle of the object instance to be managed.
    • You need to configure and change dependencies at runtime.
    • The cache or persistent dependency must be exceeded in Web applications.

Do not use the unity Application Block when:

    • Objects and classes do not depend on other objects or classes.
    • The dependency is very simple and does not need to be abstracted.

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.