在ASP.NET MVC 3应用程序的类库中引用Ninject [英] Referencing Ninject in class library in ASP.NET MVC 3 application

查看:59
本文介绍了在ASP.NET MVC 3应用程序的类库中引用Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 3应用程序,该应用程序使用Ninject.MVC3扩展名在我的MVC应用程序中设置DI.即,在App_Start文件夹中有一个NinjectMVC3.cs文件,用于定义我的绑定.这对于将DI集成到我的应用程序的控制器中非常有效.

I have an ASP.NET MVC 3 application that uses the Ninject.MVC3 extension to setup DI in my MVC application. Namely, there's the NinjectMVC3.cs file in the App_Start folder where my bindings are defined. This works well for DI into my application's controllers.

除了ASP.NET MVC Web应用程序外,我的解决方案还包含一个类库项目,我们称其为MyProject.Core,其中包含我的域模型,服务等.在那里,我有一个名为UserServices的类,该类使用名为EmailServices的服务,如下所示:

In addition to the ASP.NET MVC web application, my Solution also contains a class library projects, let's call it MyProject.Core, which has my domain model, services, and so on. In there I have a class called UserServices that uses a service called EmailServices, like so:

public class UserServices : IUserServices
{        
    private readonly IEmailServices _emailServices = new EmailServices();

    public void CreateUser(...)
    {
        // Create user...

        _emailServices.SendWelcomeEmail(newUser);
    }
}

如您所见,UserServices类对EmailServices具有硬编码的依赖关系,但是我也希望将其配置为使用DI.即,在我的ASP.NET MVC应用程序(或单元测试项目或任何地方)中,我想说绑定IEmailServices以使用TestEmailServices",并让UserServices类使用TestEmailServices而不是EmailServices.

As you can see, the UserServices class has a hard-coded dependency on EmailServices, but I'd like for that to be configured to use DI, as well. Namely, in my ASP.NET MVC application (or unit test project or wherever) I want to be able to say, "Bind IEmailServices to use TestEmailServices" and have the UserServices class use TestEmailServices instead of EmailServices.

我该怎么做?我希望能够执行以下操作:

How do I go about doing this? I'd like to be able to do something like:

public class UserServices : IUserServices
{        
    private readonly IEmailServices _emailServices = kernel.Get<EmailServices>();

    ...
}

但是在这种情况下,我不确定kernel的来源.我问的是什么道理,还是我在这里树错树了?

But I'm not sure where kernel is going to come from, in this case. Is what I'm asking making any sense, or am I barking up the wrong tree here?

谢谢

推荐答案

您应该能够通过构造函数注入来做到这一点,就像这样:

You should be able to do this with constructor injection, like this:

private readonly IEmailServices _emailServices;

public UserServices(IEmailServices emailServices)
{
    _emailServices = emailServices;
}

Ninject定制控制器工厂应自动处理注入到控制器中的服务,该工厂将使用配置的IoC容器在创建控制器时解析IUserServices对象,而该对象又将被赋予容器旁边的对象:

The service injection into your controllers should be handled automatically by the Ninject custom controller factory, which'll use the configured IoC container to resolve the IUserServices object when the controller is created, which will in turn be given an IEmailServices object by the container:

public class MyController : Controller
{
    private readonly IUserServices _userServices;

    public MyController(IUserServices userServices)
    {
        _userServices = userServices;
    }
}

在进行单元测试时,可以将虚假或模拟的电子邮件服务手动注入到用户服务中.

When you're unit testing, you can manually inject a fake or mock email service into the user service.

这篇关于在ASP.NET MVC 3应用程序的类库中引用Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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