Mugen Injection [UnitOfWorkScopeLifecycle]
UnitOfWorkScopeLifecycle - the instance will be singleton for the duration of the unit of work. In practice this means that each IInjector will contain one unit of service.
// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InUnitOfWorkScope(); var alpha1 = _injector.Get<Alpha>(); // This is a same instance that alpha1. var alpha2 = _injector.Get<Alpha>(); using (var child = _injector.CreateChild()) { //This is a different instance. var alpha = child.Get<Alpha>(); //This is a same instance that alpha. var alpha3 = child.Get<Alpha>(); }
UoW of T
UoW instances are consumed using the UoW <T> relationship type.A UoW dependency can be released when it is no longer required.
// Create your MugenInjector. _injector = new MugenInjector(); _injector.Bind<Alpha>().ToSelf().InUnitOfWorkScope().TryDisposeObjects(); var alpha = _injector.Get<UoW<Alpha>>(); //alpha is used for some task alpha.Value.ToString(); // Here alpha is no longer needed, so // it is released alpha.Dispose();
Combining UoW with Factory
UoW 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().InUnitOfWorkScope().TryDisposeObjects(); var factory = _injector.Get<Func<UoW<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 UoW
Sometimes we don't want to include MugenInjection library in some projects, in this case you can write a custom implementation of UoW. This code shows how to do it:public sealed class CustomUoW<T> : DisposableObject { #region Fields private readonly IBindingContext _bindingContext; private readonly IInjector _injector; private readonly object _locker; private T _value; #endregion #region Constructors /// <summary> /// Initializes a new instance of the <see cref="CustomUoW{T}" /> class. /// </summary> public CustomUoW(IInjector injector, IBindingContext bindingContext) { _injector = injector.CreateChild(); _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)_injector.Resolve(_bindingContext); IsValueCreated = true; } return _value; } } #endregion #region Overrides of DisposableObject protected override void Dispose(bool disposing) { if (disposing) _injector.Dispose(); base.Dispose(disposing); } #endregion } // Create your MugenInjector. _injector = new MugenInjector(); //Register the CustomUoW<> type _injector.Components .BindingActivatorComponent .GetConverter<InjectorWrapperConverter>() .InjectorWrapperTypes .Add(typeof(CustomUoW<>)); _injector.Bind<Alpha>().ToSelf().InUnitOfWorkScope().TryDisposeObjects(); //Getting service with custom UoW wrapper. var alpha = _injector.Get<CustomUoW<Alpha>>(); //alpha is used for some task alpha.Value.ToString();