Unity uses classes inherited from the lifetimemanager base class to control how to store references to object instances and how containers destroy these instances, that is to say, unity manages object creation and destruction based on the specific lifetime manager class.
Currently, Unity provides two lifetime manager classes for our direct use. Of course, you can also implement your own lifetime manager class.
1. containercontrolledlifetimemanager
Unity saves a reference pointing to the object instance. When you use the unity container to obtain an object instance of the same type or object, the same instance is obtained each time. That is to say, the object Singleton mode is implemented. By default, the registerinstance method uses the lifetime manager.
2. externallycontrolledlifetimemanager
Unity only saves a weak reference pointing to the object instance. When you use the unity container to obtain an object instance of the same type or object, the same instance is obtained each time. However, since the container does not have a strong reference to the object after the object is created, GC may recycle the object if it is not strongly referenced elsewhere.
First, let's take a look at an interface and class, which will be used below
Public Interface IPlayer
{
Void Play ();
}
Public Class Mp3player: iPlayer
{
Public Void Play ()
{
Console. writeline ( " Playing MP3 " );
}
}
Next, we will introduce the application scenarios of lifetime manager by specifying the corresponding lifetime manager when registertype and registerinstance are used.
1. registertype
When registertype is used to register the ing relationship, if the lifetimemanager is not specified, a transient life time manager is used by default. That is to say, each time you get an object instance from the Unity container, a new instance is created. That is to say, the Unity container does not have a reference to this object.
Let's look at an example:
Iunitycontainer container = New Unitycontainer ();
Container. registertype<IPlayer, mp3player>();
IPlayer player1 = Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player1 hashcode: {0} " , Player1.gethashcode ()));
IPlayer player2 = Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player2 hashcode: {0} " , Player2.gethashcode ()));
Output result:
The hashcode values of the output player1 and player2 objects show that player1 and player2 are different instances of the mp3player class.
How can we implement the singleton mode?
To implement the singleton mode, the container needs to save a reference pointing to the object instance. You can specify the corresponding lifetime manager for registertype to implement the singleton mode. From the introduction to containercontrolledlifetimemanager and externallycontrolledlifetimemanager, you can understand that both lifetime managers support Singleton mode.
Modify the precedingCodeIs:
Iunitycontainer container = New Unitycontainer ();
//The containercontrolledlifetimemanager object is specified here.
Container. registertype<IPlayer, mp3player>(NewContainercontrolledlifetimemanager ());
IPlayer player1= Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player1 hashcode: {0} " , Player1.gethashcode ()));
IPlayer player2 = Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player2 hashcode: {0} " , Player2.gethashcode ()));
Look at the output:
The output shows that the player1 and player2 objects are the same instance of the mp3player class and point to the same memory address.
2. registerinstance
When registerinstance is used to register the lifeing relationship, if the life time manager is not specified, containercontrolledlifetimemanager is used by default, that is, Singleton mode is supported.
Let's look at an example:
Iunitycontainer container = New Unitycontainer ();
IPlayer mp3player= NewMp3player ();
Container. registerinstance<IPlayer>(Mp3player );
IPlayer player1 = Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player1 hashcode: {0} " , Player1.gethashcode ()));
IPlayer player2 = Container. Resolve < IPlayer > ();
Console. writeline ( String . Format ( " Player2 hashcode: {0} " , Player2.gethashcode ()));
Look at the output:
The output shows that the player1 and player2 objects are the same instance of the mp3player class and point to the same memory address.
By: inrie (Hong Xiaojun)
Source:Http://www.cnblogs.com/inrie