[Translation] What does unity do? -- What can unity do?

Source: Internet
Author: User

By using the dependency injection framework and reverse control, you can generate and assemble custom class instances and configurations that contain dependent objects. The following section describes how to use unity.

 

The types of objects unity can create-unity can create objects

You can use the unity container to create any instance with the public constructor object (of course, you can also create it through the new operation) instead of using container registration ing ). When you call the resolve method to create an instance type that is not registered by default, the Unity container will simply call this type of constructor and return results.

Unity exposes a method named registertype to register the ing type through the container. This method also supports calling the resolve method through containers to construct an instance of your specified type. The lifecycle of the created object is equivalent to the lifecycle of the method parameter you specify. If you do not specify a value for the lifecycle, the container will create a new instance every time the resolve method is called. For more information, see: registering types and type mappings -- type registration and ing.

 

Registering existing types and object instances-register an existing type and object instance

Unity exposes a method named registerinstance to register an existing instance through the container. When you call the resolve method, the container returns an existing instance in the lifecycle. If you do not specify a value for the lifecycle, the lifecycle of the instance is controlled by the container, which means it is actually a singleton instance. For more information, see create instance registrations -- create a registered instance.

 

Managing the lifetime of objects-manage object Lifecycle

Unity allows you to select a lifecycle when creating an object. By default, unity creates a new instance every time you call the resolve method and specify a type. However, you can use the lifecycle manager to specify different lifecycles for the created instance. For example, you can specify unity to keep a single instance (or, single-instance object)-if the instance does not exist, a new instance is created, and if it already exists, a reference object is directly returned. Of course there are other lifecycles that can be used. For example, you can use the lifecycle manager to maintain a weak reference of an object so that the creation process can destroy them, or use a lifecycle manager to maintain a separate instance for each thread.

You can specify a lifecycle manager when registering a type, type ing, or managing existing objects by configuring the designer, for details, see specifying types in the configuration file -- configure the specified type in the configuration file. In addition, you can use the runtimeCodeTo add the desired lifecycle management registration to the container. For details, see registering types and type mappings -- type registration and creating instance registrations -- create a registration instance.

 

Grouping types for injection into constructors, methods, and properties-configure constructor injection, method injection, and property injection.

Unity allows you to use techniques such as constructor injection, property input, and method call injection to generate and aggregate completed dependent objects and configured OBJECT instances. For more information, see Specifying values for injection -- special value injection and registering injected parameter and property values -- register the injection parameters and attribute values.

 

Example application code for Constructor injection-construct the injection instance code

The following is an example of constructor injection. If you use the Resolve Method in the Unity container to instantiate a constructor that has one or more dependent classes, the Unity container automatically creates a dependent object instance with the specified parameters in the constructor. For example, the following code contains a class named mermerservice dependent on a class named loggingservice.

C #:

 
// Http://www.cnblos.com/kyo-yo/public class customerservice {public customerservice (loggingservice myserviceinstance) {// work with the dependent instance myserviceinstance. writetolog ("somevalue ");}}

Visual Basic:

 
'Http: // www.cnblogs.com/kyo-yo/public class customerservice public sub new (byval myserviceinstance as loggingservice) 'work with the dependent instance myserviceinstance. writetolog ("somevalue") end subend class

At runtime, developers can create a customerservice class instance through the Resolve Method of the container, which will cause it to inject a loggingservice class instance within the customerservice class.

C #:

 
// Http://www.cnblos.com/kyo-yo/IUnityContainer ucontainer = new unitycontainer (); customerservice myinstance = ucontainer. Resolve <customerservice> ();

Visual Basic:

 
'Http: // www.cnblogs.com/kyo-yo/dim ucontainer as iunitycontainer = new unitycontainer () dim myinstance as customerservice = ucontainer. Resolve (of customerservice )()

 

Example application code for property (setter) injection-property (setter) injection instance code

In addition to constructor injection, as described earlier, unity also supports property and method call injection. The following code demonstrates property injection. A class named productservice has an attribute dependent on another class named supplierdata (not defined in the following code ). Unity does not automatically create instances as part of properties. You must specify how to configure containers. One method is to add a dependency attribute Declaration for the attribute. For details, see the following code:

C #:

 
// Http://www.cnblogs.com/kyo-yo/public class productservice {private supplierdata supplier; [dependency] public supplierdata supplierdetails {get {return supplier;} set {supplier = value ;}}}

Visual Basic:

'Http: // your class productservice private supplier as supplierdata <dependency ()> _ public property supplierdetails () as supplierdata get return supplier end get set (byval value as supplierdata) supplier = value end set end propertyend class

Now, if you create an instance of the productservice class, unity will automatically generate an instance of the supplierdata class for the supplierdetails attribute in the productservice class and set its value.

 

Populating and injecting arrays, including generic Arrays-fill and inject arrays, including generic Arrays

You can define arrays, including arrays and generic types, and unity will inject arrays into your class at runtime. You can specify the members of the array, or use unity to automatically fill in the matching type defined in the configuration to the array. You can then directly use arrays as the type fill, or set them to the constructor values, parameter members, and attributes of the methods. Arrays can be defined in the configuration file or by code at runtime.

For more information and examples, see the chapter on Arrays:

    • Inserting ing injected parameter and property values -- configure the injection parameters and attribute values
    • Registering injected parameter and property values -- register the injection parameters and attribute values
    • Registering generic parameters and types -- register generic parameters and types

 

Intercepting cballs to objects -- intercept object calls

The best type is specific and single responsibility in the object-oriented system. However, as the system runs, other problems are added. System detection, such as logs, event counters, parameter verification, and exception handling are common examples. These cross-cutting problems (AOP) usually needProgramAdd a lot of repeated code in the Code unless your design changes, such as you change your log framework, and then the log calls must be changed in many places in the code.

Interception technology allows you to add code before or after calling the target object. This call is an additional action that can be taken to intercept the request. When you manually execute this encoding process, you will know what is called the decoration mode. To write a decoration pattern, you need to know the type at the beginning of the design. At the same time, each different decoration type must match the type you wrote.

The Unity interception system automatically creates a decoration mode. It allows you to minimize the amount of code that can be reused to solve these cross-cutting issues (AOP). If so, pay attention to the types of issues that will be applied to the cross-cutting issue (AOP ).

 

Example application code-instance program code

In the following example, you can configure a container at runtime to intercept typetointercept by using an interceptor virtualmethodinterceptor, an interception behavior abehavior, and an additional interface.

C #:

 
// Http://www.cnblogs.com/kyo-yo/IUnityContainer Container = new unitycontainer (); container. addnewextension <interception> (); container. registertype <typetointercept> (new interceptor <virtualmethodinterceptor> (), new interceptionbehavior <abehavior> (), new additionalinterface <iotherinterface> ());

Visual Basic:

 
'Http: // www.cnblogs.com/kyo-yo/ dim container as iunitycontainer = new unitycontainer () container. addnewextension (of dataexception) () container. registertype (of typetointercept) (new interceptor (of virtualmethodinterceptor) (), new interceptionbehavior (of abehavior) (), new additionalinterface (of iotherinterface )())

 

You can create an instance by calling the following example:

C #:

 
// The http://www.cnblogs.com/kyo-yo/TypeToIntercept instance = container. Resolve <typetointercept> ();

Visual Basic:

 
'Http: // www.cnblogs.com/kyo-yo/dim instance as typetointercept = container. Resolve (of typetointercept )()

When an instance calls a method, the code in the abehavior class is executed before and after any instance object is called.

 

What does Untiy do?

 

PS: This article is translated from the official unity2.0 documentation, mainly to learn about unity2.0 and exercise your English skills at the same time. However, my English is really poor.ArticleThere must be many problems, so please note.

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.