Mugen Injection [ManagedScopeLifecycle]
ManagedScopeLifecycle - instances activated via the IManagedScope lifecycle managment.
Example of usage:
// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InManagedScope().TryDisposeObjects(); using (var scope = _injector.CreateManagedScope()) { var alpha1 = _injector.Get<Alpha>(); // This is a same instance that alpha1. var alpha2 = _injector.Get<Alpha>(); using (var scope2 = _injector.CreateManagedScope()) { //This is different instance it did not like was alpha1. var alpha3 = _injector.Get<Alpha>(); // This is a same instance that alpha3. var alpha4 = _injector.Get<Alpha>(); }//scope2 will be disposed here. }//scope1 will be disposed here.
Managed of T
Managed instances are consumed using the Managed<T> relationship type.A managed dependency can be released when it is no longer required. Managed dependencies usually correspond to some unit of work performed by the dependent component.
// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InManagedScope().TryDisposeObjects(); var alpha = _injector.Get<Managed<Alpha>>(); //alpha is used for some task alpha.Value.ToString(); // Here alpha is no longer needed, so // it is released alpha.Dispose();
Combining Managed with Factory
Managed instances are usually used in conjunction with a Func<T> relationship, so that units of work can be begun and ended on-the-fly.// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InManagedScope().TryDisposeObjects(); var factory = _injector.Get<Func<Managed<Alpha>>>(); using (var alpha = factory()) { //alpha is used for some task alpha.Value.ToString(); // Here alpha is no longer needed, so // it is released }
Custom implementation of Managed
Sometimes we don't want to include MugenInjection library in some projects, in this case you can write a custom implementation of Managed. This code shows how to do it:public class CustomManaged<T> : DisposableObject { #region Fields private readonly IBindingContext _bindingContext; private readonly object _locker; private readonly IManagedScope _managedScope; private T _value; #endregion #region Constructors public CustomManaged(IInjector injector, IBindingContext bindingContext) { _managedScope = injector.CreateManagedScope(); _bindingContext = bindingContext; _locker = new object(); } #endregion #region Properties public bool IsValueCreated { get; set; } public T Value { get { if (IsValueCreated) return _value; lock (_locker) { if (IsValueCreated) return _value; _value = (T)_managedScope.Resolve(_bindingContext); IsValueCreated = true; } return _value; } } #endregion #region Overrides of DisposableObject protected override void Dispose(bool disposing) { if (disposing) _managedScope.Dispose(); base.Dispose(disposing); } #endregion } // Create your MugenInjector. _injector = new MugenInjector(); //Register the CustomManaged<> type _injector.Components .BindingActivatorComponent .GetConverter<InjectorWrapperConverter>() .InjectorWrapperTypes .Add(typeof(CustomManaged<>)); _injector.Bind<Alpha>().ToSelf().InManagedScope().TryDisposeObjects(); //Getting service with custom managed wrapper. var alpha = _injector.Get<CustomManaged<Alpha>>(); //alpha is used for some task alpha.Value.ToString();