To use the container in MVC you need to add code to your Global.asax.cs file:

public class MvcApplication : MugenInjectionHttpApplication
    {
        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
                );
        }

        #region Overrides of MugenInjectionHttpApplication

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

        /// <summary>
        /// Creates the injector that will manage your application.
        /// </summary>
        /// <returns>
        /// The created injector.
        /// </returns>
        protected override IInjector CreateInjector()
        {
            var injector = new MugenInjector();
            injector.Bind<IRequestScopeService>()
                .To<RequestScopeService>()
                .InHttpRequestScope();

            injector.Bind<ISessionScopeService>()
                .To<SessionScopeService>()
                .InSessionScope();

            return injector;
        }

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

        #endregion
    }

In the http application is available two additional LifecycleScope, HttpRequestScope and SessionScope. More about LifecycleScope here.

You can also use RoleProvider and MembershipProvider, with custom injections, for this you must add in web.config:

<membership defaultProvider="MugenInjectionMembershipProvider">
      <providers>
        <clear/>
        <add name="MugenInjectionMembershipProvider" type="MugenInjection.Web.Mvc.Providers.MugenInjectionMembershipProvider"  />
      </providers>
</membership>

<roleManager enabled="true" defaultProvider="MugenInjectionRoleProvider">
      <providers>
        <clear/>
        <add name="MugenInjectionRoleProvider" type="MugenInjection.Web.Mvc.Providers.MugenInjectionRoleProvider" />       
      </providers>
 </roleManager>

Then in your code, you must implement to RoleProvider and MembershipProvider:

/// <summary>
/// Creates the injector that will manage your application.
/// </summary>
/// <returns>
/// The created injector.
/// </returns>
protected override IInjector CreateInjector()
{
     var injector = new MugenInjector();
     injector.Bind<IService>().To<CustomService>();

     //Register providers
     injector.WithCustomMembershipProvider<MemberProvider>();
     injector.WithCustomRoleProvider<CustomRoleProvider>();

      return injector;
}

Last edited May 7, 2012 at 3:20 PM by VVS0205, version 7

Comments

No comments yet.