When to use property (setter) 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 parent object has many constructor related to each other, which makes debugging and maintenance inconvenient.
The parent object contains many parameter constructors. In particular, similar parameter types can only be identified by parameter locations.
Ask the user (Program) It is easier to see which objects can be used, which cannot be implemented in constructor injection.
Modify the code of the dependent object to control which objects can be injected without modifying the parent object or application.
Preparations
//Song category
Public Class Song
{
//Artist
Public String Singer
{
Get
{
Return " Westlife " ;
}
}
/ /song name
Public string name
{< br> Get
{< br> return " my love " ;
}< BR >}
Public Abstract ClassPlayer
{
[Dependency]
PublicSong {Get;Set;}
Public Abstract StringName {Get;}
Public VoidPlay ()
{
Console. writeline (String. Format ("{0}: Now playing [{1}] singing by ({2 })",This. Name,This. Song. Name,This. Song. Singer ));
}
}
Public Class Mp3player: Player
{
Public Override String Name
{
Get
{
Return " MP3 player " ;
}
}
}
Public Class Cdplayer: Player
{
Public Override String Name
{
Get
{
Return " CD player " ;
}
}
}
Start
By attaching the [dependency] label to the class property, the Unity container automatically instantiates the object that the property depends on when obtaining the class object instance and injects it into the property.
Let's look at an example. The mp3player class has a song attribute, which is labeled as [dependency.
[Dependency]
Public Song { Get ; Set ;}
You can use the following method to obtain the mp3player object instance:
Iunitycontainer container = New Unitycontainer ();
Container. registertype < Player, mp3player > ();
Player player=Container. Resolve<Player>();
Player. Play ();
Look at the output:
Here, the [dependency] label is attached to the song attribute of the mp3player class to indicate that the unity container will automatically instantiate the song object when loading the object and inject it into the song attribute of mp3player.
The output shows that the container automatically loads the property object on which the [dependency] label is added when assembling the mp3player object.
You can also specify the name for the [dependency] feature. Let's look at another example:
This is a player store class. It attaches the [dependency] label to the popularplayer attribute and specifies the name as "mp3player ".
// Player store
Public Class Playerstore
{
// Most popular player types (mp3player and cdplayer)
[Dependency ("mp3player")]
Public Player popularplayer { Get ; Set ;}
}
Register Two Mappings (mp3player and cdplayer) for the player base class in the Unity container and specify the mapped name respectively. You can use the following method to obtain the playerstore object instance and output the most popular player name of the store:
Iunitycontainer container = New Unitycontainer ();
Container. registertype < Player, mp3player > ( " Mp3player " );
Container. registertype < Player, cdplayer > ( " Cdplayer " );
Playerstore player=Container. Resolve<Playerstore>();
Console. writeline (player. popularplayer. Name );
Output:
Since the player is mapped to mp3player and cdplayer, you can specify a name for the [dependency] attribute to match the specific player class.
Inject to an existing object instance
When you use the resolve method to obtain an existing object instance, the property injection is not performed because the creation of this object is not affected by the Unity container. You can use the buildup method to forcibly implement the property injection.
For more information about the buildup method, see:
Unity Application Block 1.0 series (5): Use buildup to enable dependency injection for existing object instances
Conclusion
When using property/setter injection, pay special attention not to circular references. Otherwise, application errors may occur.
For circular references, refer:
Unity Application Block 1.0 series (6): prevents circular references
By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie