Ninject依赖注入的MVC3 - 一个控制器外 [英] Ninject Dependency Injection in MVC3 - Outside of a Controller

查看:202
本文介绍了Ninject依赖注入的MVC3 - 一个控制器外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将在我们的MVC3项目中使用Ninject做的依赖注入。 我用的NuGet包来添加引用Ninject和Ninject.MVC3包。当我做这一点,在我的App_Start文件夹中创建一个NinjectMVC3类:

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;
    }
}

该控制器上的IPrincipal的依赖,我已经建立了我的引导程序类解析到HttpContext.Current.User。我还有一个类,它有一个依赖的IPrincipal这不是一个控制器:

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? }

推荐答案

那么,理想情况下这不应该是一个问题。所有的依赖应该inected到控制器,以及那些依赖依赖任何依赖关系自动地获得注为好。

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.

在MVC,(几乎)一切开始于控制器。所以,你可能有:

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.

不过,有可能是你需要动态创建一个对象次。在这种情况下,你可以使用MVC DependencyResolver。

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;}
}

这篇关于Ninject依赖注入的MVC3 - 一个控制器外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆