Ninject Dependency Injection in MVC3 - Outside of a Controller

情到浓时终转凉″ 提交于 2020-01-13 05:18:06

问题


We are using Ninject in our MVC3 project to do dependency injection. I used NuGet to add package references to Ninject and Ninject.MVC3 packages. When I did this it created a NinjectMVC3 class in my App_Start folder:

public static class NinjectMVC3
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start()
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
        DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        RegisterServices(kernel);
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {           
        kernel.Bind<IPrincipal>().ToMethod(c => HttpContext.Current.User);
    }
}

So far this has worked great to resolve dependencies in my controllers:

public class HomeController : Controller {
    protected IPrincipal principal { get; set; }

    public HomeController(IPrincipal principal) {
        this.principal = principal;
    }
}

This controller has a dependency on IPrincipal which I have set up in my bootstrapper class to resolve to HttpContext.Current.User. I have another class which has a dependency on IPrincipal that is not a controller:

public class NonControllerClass
{
    protected IPrincipal Principal { get; set; }

    public NonControllerClass(IPrincipal principal) {
    }
}

How would i go about resolving this dependency? How would I do it if it wasn't a parameter of the constructor? }


回答1:


Well, ideally this should never be a problem. All dependencies should be inected into your controller, and any dependencies those dependencies depend on should automatically get injected as well.

In MVC, (almost) everything starts at the controller. So you might have:

public class HomeController : Controller { 
    protected IMyService myService { get; set; } 

    public HomeController(IMyService myService) { 
        this.myService = myService; 
    } 
} 

public class MyService {
    protected IPrincipal principal;

    public MyService(IPrincipal principal) { this.principal = principal)
}

Notice how you don't have to do anything, your service automatically gets injected with the right dependencies because your service was injected into your controller.

However, there might be times you need to dynamically create an object. In that case, you can use the MVC DependencyResolver.

var principal = DependencyResolver.Current.GetService<IPrincipal>();

You should avoid doing this unless absolutely necessary, however, because this is considered an anti-pattern (known as Service Location). Sometimes you don't have much choice though.

If you don't want to use constructor injection, you can use property injection.

public class MyService {
    [Inject]
    public IPrincipal principal {get; set;}
}


来源:https://stackoverflow.com/questions/8097338/ninject-dependency-injection-in-mvc3-outside-of-a-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!