About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

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.
The MugenInjection has a system of relationship types that can be used to provide the features of the container in a declarative way. Instead of manipulating an IInjector or IManagedScope directly, as in the above example, relationship types allow a component to specify exactly which container services are needed, in a minimal, declarative way.

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();

Comments
Leave a Reply
*bold*
_italics_
+underline+
* Bullet List
** Bullet List 2
# Number List
## Number List 2
{"Do not apply formatting"}
{code:language} code here {code:language}.
Supports: aspx c#, c#, c++, html, sql, xml
[url:http://www.example.com]