How do I use ninject within an AreaRegistration
-
01-07-2021 - |
Вопрос
I have an 'Account' area within my mvc3 project.
It has an AreaRegistration class to limit visibility to registered-only users like so:
public class AccountAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Account";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Account_default",
"{account}/{controller}/{action}/{id}",
new {controller = "Dashboard", action = "Index", id = UrlParameter.Optional },
new {account = new MustBeRegisteredAccount(DEPENDENCIES)},
new string[] {"Continuum.Web.Areas.Account.Controllers"}
);
}
}
so how do i set up ninject to be able to resolve DEPENDENCIES / create the MustBeRegisteredAccount object?
Решение
I'd first try and use Constructor Injection (i.e., see if MVC internally uses the registered DependencyResolver
to create instances of AreaRegistration
classes too (havent found anythign one way or the other on whether that should work but there's a pretty quick way to find out empirically :D).
Failing that, using a DependencyResolver.GetService<MustBeRegisteredAccount>()
is the Service Locator (antipattern) workaround in MVC land. (The Ninject.MVC3 extension deliberately doesn't expose a global Kernel instance as that would just encourage gratuitous use).
See http://bradwilson.typepad.com/blog/2010/10/service-location-pt5-idependencyresolver.html