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

Mugen Injection [Keyed bindings]

The MugenInjector supports register binding with key. Keys are not necessarily unique; multiple bindings for a given service may be registered with the same key, this code shows how it work:

public class Alpha { }
public class Beta : Alpha { }
public class Bravo : Beta { }

public enum KeyEnum
{
    Alpha,
    Beta,
    Bravo
}

// Create your MugenInjector.
_injector = new MugenInjector();

_injector.Bind<Alpha>().ToSelf().KeyedBinding(KeyEnum.Alpha);
_injector.Bind<Alpha>().To<Beta>().KeyedBinding(KeyEnum.Beta);
_injector.Bind<Alpha>().To<Bravo>().KeyedBinding(KeyEnum.Bravo);

var alpha = _injector.Get<Alpha>(KeyEnum.Alpha);//Instance of Alpha.
var beta = _injector.Get<Alpha>(KeyEnum.Beta);//Instance of Beta.
var bravo = _injector.Get<Alpha>(KeyEnum.Bravo);//Instance of Bravo.
This code shows how register multiple bindings with the same key:
// Create your MugenInjector.
_injector = new MugenInjector();

_injector.Bind<Alpha>().ToSelf().KeyedBinding(KeyEnum.Alpha);
_injector.Bind<Alpha>().To<Beta>().KeyedBinding(KeyEnum.Alpha);
_injector.Bind<Alpha>().To<Bravo>().KeyedBinding(KeyEnum.Alpha);

var list = _injector.Get<IList<Alpha>>(KeyEnum.Alpha);
var enumerable = _injector.GetAll<Alpha>(KeyEnum.Alpha);

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]