Summary
One of the responsibilities of Di containers is to manage the life cycle of the objects he creates. He should decide when to create an object of a given type, and when to use an object that already exists. He also needs to handle objects when the object is not needed. Ninject provides strong support for managing the life cycle of objects in different situations. When we define a binding, we define the scope of the object to be created. Within that range, the object will be reused, with each binding being only once. Note that objects are not allowed to rely on objects that have a smaller life cycle than themselves.
1. Temporary scope
Within a transient state, the object life cycle is not managed by Ninject. Any time an object of type is requested, a new object is created. Ninject does not manage to keep the created object or handle it within scope. This is the default range for Ninject. If you do not specify a range, the default is transient. In the previous article, the Consolelogger and MailServer objects were transient because they did not specify his range.
2. Single Case Range
Sometimes we don't want to create a new object every time we need to, we use a singleton. There are two ways to create a single case. One is to use singleton mode. One is to use the Ninject method Insingletonscope.
1) Use a singleton mode:
1 classConsolelogger:ilogger2 {3 Public Static ReadOnlyConsolelogger Instance =NewConsolelogger ();4 Private StaticConsolelogger ()5 {6 //hiding Constructor7 }8 Public voidLog (stringmessage)9 {TenConsole.WriteLine ("{0}: {1}", DateTime.Now, message); One } A}
The Toconstant method is then called after the bind method to specify a static read-only object consolelogger.instance as a constant object.
Kernel. Bind<ilogger> (). Toconstant (consolelogger.instance);
2) How to use Insingletonscope:
Kernel. Bind<ilogger> (). To<consolelogger> (). Insingletonscope ();
If you want to set a singleton for a Mailserverconfig class object, first call the Toself method to bind him and then call the method Insingletonscope.
Kernel. Bind<mailserverconfig> (). Toself (). Insingletonscope ();
3. Thread Range
If the thread scope is defined, each of the threads will create only one object of the given type. The life cycle of an object is as long as the thread on which the object resides.
Call method Inthreadscope to create a thread range:
Kernel. bind<Object> (). Toself (). Inthreadscope ();
Create a two test method that tests the thread range.
1 usingNinject;2 usingnunit.framework;3 usingSystem.Threading;4 5 namespaceDemo.ninject6 {7 [Testfixture]8 classninjecttest9 {Ten [Test] One Public voidReturnsthesameinstancesinonethread () A { - using(varKernel =NewStandardkernel ()) - { theKernel. bind<Object>(). Toself (). Inthreadscope (); - varInstance1 = kernel. get<Object>(); - varInstance2 = kernel. get<Object>(); - assert.areequal (Instance1, instance2); + } - } + A [Test] at Public voidreturnsdifferentinstancesindifferentthreads () - { - varKernel =NewStandardkernel (); -Kernel. bind<Object>(). Toself (). Inthreadscope (); - varInstance1 = kernel. get<Object>(); - NewThread (() = in { - varInstance2 = kernel. get<Object>(); to assert.arenotequal (Instance1, instance2); + kernel. Dispose (); - }). Start (); the } * } $}
The first method requests two object objects in the same line range, they are the same instance. The second method requests an object instance on the main line range and then opens another thread to request another instance, and they are not the same instance.
You need to add NUnit and nunit.console to test the above method. I am using NUnit 2.6.4 and Nunit.console 2.0.0.
4. Request Scope
The request scope is very useful in Web applications. A singleton object can be obtained within the same request scope. Once a request is processed and another request arrives, Ninject creates a new object instance and keeps him until the request ends.
Call method Inrequestscope to set the request scope:
Kernel. Bind<mailserverconfig> (). Toself (). Inrequestscope ();
You need to add a Ninject.Web.Common reference before you can call the Inrequestscope method.
Ninject III: Ninject object life cycle