Unity Application Block 1.0 series (2): constructor Injection)

Source: Internet
Author: User

When to use constructor Injection


    • The dependent objects can also be automatically instantiated when the parent object is instantiated.
    • It is easy to achieveCodeView the items that each class depends on
    • The constructor of the parent object does not need many constructor associated with each other.
    • The constructor of the parent object does not need many parameters.
    • By not using attributes and methodsProgramTo encapsulate the field value so that it cannot be seen
    • Modify the code of the dependent object to control which objects can be injected without modifying the parent object or application.


Preparations

Public   Class Song
{
Public   String Singer
{
Get
{
Return   " Westlife " ;
}
}

Public string name
{< br> Get
{< br> return " my love " ;
}< BR >}

Public class owner
{< br> Public string name
{< br> Get
{< br> return " inrie " ;
}< BR >}

Public IntAge
{
Get
{
Return 24;
}
}
}

Public InterfaceIPlayer
{
Void play ();
}

 

Start

The number of class constructors is divided into the following two situations:
1. Single Constructor
2. Multiple Constructors

1. Single constructor:

Unity performs automatic dependency injection for a single constructor.

Let's look at an example:

Public   Class Mp3player: iPlayer
{
Public Song msong;

PublicMp3player (Song, owner)
{
This. Msong=Song;
}

Public   Void Play ()
{
Console. writeline ( String . Format ( " Now playing [{0}] singing by ({1 }) " , This . Msong. Name, This . Msong. Singer ));
}
}


You can obtain the mp3player object instance in the following way.

Iunitycontainer container =   New Unitycontainer ();
Container. registertype < IPlayer, mp3player > ();

IPlayer player=Container. Resolve<IPlayer>();
Player. Play ();

Output:


In the preceding example, there is only one constructor in the mp3player class. The constructor depends on two classes: Song and owner. When executing the resolve method, the Unity container automatically assembles the dependent class instances and injects them into the constructor.

When there is only one constructor in the class, the Unity container automatically uses this constructor as the default constructor during assembly.

2. Multiple constructors:

For multiple constructors, unity must use the [injectionconstructor] feature to implement dependency injection.

There are several principles:
A. the constructor with the [injectionconstructor] label is the constructor for dependency injection.

Public   Class Mp3player: iPlayer
{
Public Song msong;

[injectionconstructor]
Public mp3player (owner)
{< br>
}

PublicMp3player (Song, owner)
{
This. Msong=Song;
}

Public   Void Play ()
{
Console. writeline ( String . Format ( " Now playing [{0}] singing by ({1 }) " , This . Msong. Name, This . Msong. Singer ));
}
}

In this exampleThe mp3player (owner) constructor is labeled with the [injectionconstructor] label. Therefore, the constructor is used as the constructor for dependency injection.

B. If no constructor is attached with [injectionconstructor], use the constructor with the most parameters as the constrtor for dependency injection.

Public   Class Mp3player: iPlayer
{
Public Song msong;

PublicMp3player (owner)
{

}

PublicMp3player (Song, owner)
{
This. Msong=Song;
}

Public   Void Play ()
{
Console. writeline ( String . Format ( " Now playing [{0}] singing by ({1 }) " , This . Msong. Name, This . Msong. Singer ));
}
}

In this example, because there are multiple constructors and no [injectionconstructor] labels attached to any constructor, the most parameter constructor is used, that is, mp3player (Song, as the constructor for dependency injection.

NOTE: If multiple constructors belong to the "Most Parameters" constructor, an error occurs.

Inject to an existing object instance

Constructor injection is not performed when you use the resolve method to obtain an existing object instance, because the creation of this object is not affected by the Unity container. Not even using the buildup method. Property injection can be used as a replacement method.

For more information about the buildup method, see:
Unity Application Block 1.0 series (5): Use buildup to allow object instances to support dependency injection.

For details about property injection, refer:
Unity Application Block 1.0 series (3): property/setter Injection)

Conclusion

Specifically mentioned in the Unity help document: "If you are not sure which injection method to use, we recommend using constructor.Injection. It is more in line with almost all general needs ."

In addition, to use constructor injection, pay special attention not to circular references. Otherwise, application errors may occur,Here is a specific description of circular references.ArticleIntroduction.

By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie

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.