At this point, the container supports four lifecycle scope, but you can add your own implementation.
SingletonScopeLifecycle - indicates that only a single instance of the binding should be created, and then should be re-used for all subsequent requests.
Example:
var injector = new MugenInjector();
injector.Bind<ITest>().To<Test>().InSingletonScope();
var test = injector.Get<ITest>();
var test1 = injector.Get<ITest>();
if (test == test1)
Console.WriteLine("Is always true.");
IInjector child = injector.CreateChild();
var test2 = child.Get<ITest>();
if (test == test2)
Console.WriteLine("And it is also true.");
ThreadScopeLifecycle - indicates that instances activated via the binding should be re-used within the same thread.
Example:
var injector = new MugenInjector();
injector.Bind<ITest>().To<Test>().InThreadScope();
var test = injector.Get<ITest>();
var test1 = injector.Get<ITest>();
if (test == test1)
Console.WriteLine("Is always true.");
var thread = new Thread(() =>
{
test1 = injector.Get<ITest>();
if (test != test1)
Console.WriteLine("Is always true.");
});
thread.Start();
IInjector child = injector.CreateChild();
var test2 = child.Get<ITest>();
if (test == test2)
Console.WriteLine("And it is also true.");
thread = new Thread(() =>
{
test1 = child.Get<ITest>();
if (test != test1)
Console.WriteLine("And it is also true.");
});
thread.Start();
TransientScopeLifecycle - indicates that instances activated via the binding should not be re-used, nor have their lifecycle managed.
Example:
var injector = new MugenInjector();
injector.Bind<ITest>().To<Test>().InTransientScope();
var test = injector.Get<ITest>();
var test1 = injector.Get<ITest>();
if (test != test1)
Console.WriteLine("Is always true.");
IInjector child = injector.CreateChild();
var test2 = child.Get<ITest>();
if (test != test2)
Console.WriteLine("And it is also true.");
if (test1 != test2)
Console.WriteLine("And it is also true.");
UnitOfWorkScopeLifecycle - indicates that only a single instance of the binding should be created in each IInjector, and then should be re-used for all subsequent requests.
var injector = new MugenInjector();
injector.Bind<ITest>().To<Test>().InUnitOfWorkScope();
var test = injector.Get<ITest>();
var test1 = injector.Get<ITest>();
if (test == test1)
Console.WriteLine("Is always true.");
IInjector child = injector.CreateChild();
var test2 = child.Get<ITest>();
var test3 = child.Get<ITest>();
if (test != test2)
Console.WriteLine("And it is also true.");
if (test2 == test3)
Console.WriteLine("And it is also true.");
An example of the creation and use of ScopeLifecycle. We will create
WeakReferenceScopeLifecycle.
public class WeakReferenceScopeLifecycle : ScopeLifecycleBase
{
#region Fields
private WeakReference _weakReference;
#endregion
#region Overrides of ScopeLifecycleBase
/// <summary>
/// Get object from current scope.
/// </summary>
/// <returns/>
public override object GetObjectFromScope()
{
if (_weakReference == null || !_weakReference.IsAlive)
{
object service = this.ResolveCurrentService();
_weakReference = new WeakReference(service);
return service;
}
return _weakReference.Target;
}
#endregion
}
//Somewhere in code
var injector = new MugenInjector();
injector.Bind<Test>().ToSelf().InScope(new WeakReferenceScopeLifecycle());
Scopes only for HTTP application:
HttpRequestScope - indicates that instances activated via the binding should be re-used within the same HTTP request, for scope used HttpContext.
SessionScope - indicates that instances activated via the binding should be re-used within the same HTTP session, for scope used HttpSessionState.
OperationRequestScope - indicates that instances activated via the binding should be re-used within the same OperationContext request, for scope used OperationContext, used for WCF.