When to use method call 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.
Hide dependent objects and do not expose them as attributes.
Modify the code of the dependent object to control which objects can be injected without modifying the parent object or application.Program
Preparations
Public Interface IPlayer
{
Void Play ();
}
Public Class Mp3player: iPlayer
{
Public Song msong;
[Injectionmethod]
Public Void Init (Song)
{
This . Msong = Song;
}
Public Void Play ()
{
Console. writeline ( String . Format ( " {0}: Now playing [{1}] singing by ({2 }) " , This . Name, This . Msong. Name, This . Msong. Singer ));
}
Public String Name
{
Get
{
Return " MP3 player " ;
}
}
}
Start
Constructor injection is triggered when the container creates an object instance, while method call injection is triggered only when the method of the object instance is called.
By attaching the [injectionmethod] label to the class method, the Unity container will automatically instantiate the object that the method depends on when obtaining the class object instance and inject it into the method.
Let's look at an example:
In the mp3player class, the [injectionmethod] label is attached to the init (Song) method:
[Injectionmethod]
Public VoidInit (Song)
{
This. Msong=Song;
}
You can use the following method to obtain the mp3player object instance:
Iunitycontainer container= New Unitycontainer ();
Container. registertype<IPlayer, mp3player>();
IPlayer player=Container. Resolve<IPlayer>();
Player. Play ();
Output:
Here, the [injectionmethod] label is attached to the init method of the mp3player class to indicate that the dependent object (Song object) will be automatically instantiated when the unity container loads the mp3player object ), then inject it into the init method of mp3player (execute this method ).
Here, we mainly perform two operations:
1. Song = new song ();
2. This. msong = song;
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 allow object instances to support dependency injection.
Conclusion
When using method call injection, pay special attention not to circular references. Otherwise, errors may occur in the application. For details about circular references, we will provide a special articleArticleIntroduction.
By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie