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

Mugen Injection [Strategy to resolve]

When you using a child container you can specify how bindings will be activated.
This code shows two strategy to activate binding:

public class SomeClass : ISomeInterface
{
    public string Value { get; set; }

    public SomeClass(string value)
    {
        Value = value;
    }

}


const string parentValue = "parentValue";
const string childValue = "childValue";
using (var injector = GetInjector())
{
    injector.Bind<ISomeInterface>().To<SomeClass>();
    injector.Bind<string>().ToConstant(parentValue);

    using (var child = (MugenInjector)injector.CreateChild())
    {
        //override binding in child container.
        child.Bind<string>().ToConstant(childValue);

        //If not find binding, resolve in parent(use parent binding).
        injector.Settings.UseParentToResolve = true;

        var parentAlpha = injector.Get<ISomeInterface>();
        var childAlpha = child.Get<ISomeInterface>();
        parentAlpha.Value.ShouldEqual(parentValue);
        childAlpha.Value.ShouldEqual(parentValue);

        //changing flag
        child.Settings.UseParentToResolve = false;
        parentAlpha = injector.Get<ISomeInterface>();
        childAlpha = child.Get<ISomeInterface>();
        parentAlpha.Value.ShouldEqual(parentValue);
        childAlpha.Value.ShouldEqual(childValue);
    }
}

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]