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

Mugen Injection [Child container]

Sometimes it is necessary to separate parts of the application to avoid duplicated bindings (e.g. Plugins that share interfaces from the main application) or to ensure that some part can not have a side affect to other parts (e.g. to ensure that wrong scoping has no influence). In these cases you can use the child container, which forwards any unknown request to the parent container.
This code shows how it works:

// Create your MugenInjector.
_injector = new MugenInjector();
_injector.Bind<string>().ToConstant("parent");
_injector.Bind<string>().ToConstant("parentNamed").NamedBinding("parent");
using (var child = _injector.CreateChild())
{
    //_injector contains child
_injector.ChildInjectors.Contains(child);
    //injector equals to _injector
var injector = child.GetParent();
child.Bind<string>().ToConstant("child");

//s equals to "child"
var s = child.Get<string>();
//parent equals to "parentNamed" because child doesn't has a named binding with name = "parent"
var parent = child.Get<string>("parent");
}
When parent container was disposed it will dispose all child containers.

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]