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

Mugen Injection [MVC]

Getting Started

Create a new MVC application and add references to MugenInjection.Core, MugenInjection.Web and MugenInjection.Web.Mvc. Then change the global.asax to derive from MugenInjectionHttpApplication instead of HttpApplication and override CreateInjector to create a IInjector and load all modules that you need in your application, also override Configure to set the MVC configuration. Here is an example of the global.asax.
public class MvcApplication : MugenInjectionHttpApplication
{
    #region Methods

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    }

    #endregion

    #region Overrides of MugenInjectionHttpApplication

    /// <summary>
    /// Creates the injector that will manage your application.
    /// </summary>
    /// <returns>
    /// The created injector.
    /// </returns>
    protected override IInjector CreateInjector()
    {
        return new MugenInjector();
    }

    /// <summary>
    /// Configure current <see cref="T:MugenInjection.Web.MugenInjectionHttpApplication"/>
    /// </summary>
    /// <param name="applicationConfiguration">Configuration instance.</param>
    protected override void Configure(IApplicationConfiguration applicationConfiguration)
    {
        applicationConfiguration
            .WithHttpConfiguration()
            .WithMvcConfiguration();
    }

    /// <summary>
    /// Called when the application is started.
    /// </summary>
    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    #endregion
}

Dependency injection for controllers

In the previous step we already prepared anything that is necessary for controller injection. The constructor of controllers can be changed now to accept dependencies. The only thing that has to be done is to configure the MugenInjection bindings for its dependencies. The controller itself will be found by MugenInjection even without adding a binding. Of course, you can still add a binding for the controller in case you need to specify more information for the binding (e.g. an additional constructor argument).

Here is an example of a controller that has the welcome message service as dependency.
public class HomeController : Controller
{
    private readonly IService _service;
 
    public HomeController(IService service)
    {
         _service= service;
    }
 
    public void Index()
    {
        ViewModel.Message = _service.GetMessage();
        return View();
    }
}

Injection of validators

The following example demonstrates how to create a simple validation attribute that uses a service to check if the value is valid:
public class SimpleValidationAttribute : ValidationAttribute
{
    [Inject]
    public IService Service { get; set; }

    public override bool IsValid(object value)
    {
        return Service.IsValid(value);
    }
}

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]