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

Mugen Injection [Modules]

With some dependency injection frameworks, your XML mapping files can quickly grow large and difficult to navigate. Even in some that aren’t based on XML mapping files, you still end up with a big monster service binding sequence. With MugenInjection, your type bindings are collected into groups called modules. Each of these modules represents an independent segment of your application. Modules just need to implement the IInjectorModule interface, but most should extend the InjectorModule class for simplicity.
This code shows how it works:

public class MyModule : InjectorModule
{
    private readonly bool _isDebug;

    public MyModule(bool isDebug)
    {
        _isDebug = isDebug;
    }

    #region Overrides of InjectorModule

    /// <summary>
    /// Loads bindings and components in current module.
    /// </summary>
    public override void Load()
    {
        if (_isDebug)
            Bind<IRepository>().To<FakeRepository>();
        else
            Bind<IRepository>().To<Repository>();
    }

    #endregion
}


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

//Unload module.
_injector.Unload<MyModule>();
//Load module.
_injector.Load(new MyModule(true));

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]