构造与控制器参数 - MVC [英] Constructor with parameter in Controller - MVC

查看:345
本文介绍了构造与控制器参数 - MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读IOC和Unity几篇文章,得到了我的自我困惑:(

I have read few articles on IOC and Unity and got my self confused :(

所以要回到基本能有人电话我什么低于code呢?

So going back to basics can some one tel me what the below code does?

private IStudent _student;
        public HomeController(IStudent student)
        {
            _student= student;
        }

public interface IStudent 
    {
        // Some method
    }

伊茨基本的,但我试图从一个门外汉视图来了解。究竟上述code呢?

Itz Basic but am trying to understand from a layman view. What exactly the above code does?

推荐答案

HomeController中拥有的依赖的学生上,因为它代表了一些责任Student类。

HomeController has a dependency on Student because it delegates some responsibility to Student class.

来实现的一种方法是:

public HomeController()
{
    private Student _student;
    public HomeController()
    {
        _student = new Student();
    }
}
public class Student 
{
    // Some method
}

但随后的HomeController有一个硬盘在Student类的依赖。如果你想使用一些其他实现学生(如想嘲笑学生,而单元测试你的HomeController)。你将不得不修改学生类或HomeController类(或使用其他一些不那么好的选择)。这意味着你的HomeController中的紧耦合以Student类。

but then HomeController has a hard dependency on Student class. What if you wanted to use some other implementation of Student (e.g. wanted to mock Student while unit testing your HomeController). You will have to modify the Student class or HomeController class (or use some other not-so-good option). This means your HomeController is tightly-coupled to Student class.

另一种方法是,你已经张贴了code:

public class HomeController
{
    private IStudent _student;
    public HomeController(IStudent student)
    {
        _student = student;
    }
}
public interface IStudent
{
    // Some method
}
public class Student : IStudent
{
    // Implementation of some method
}

在这里,你可以在IStudent即的任何实现通过在单元测试中你可以通过IStudent的模拟对象,在实际code你会通过Student类的对象。所以,你的HomeController现在的依赖的的IStudent接口(抽象),而不是对学生类(实现)上。
这是行内的OOP原则:

Here, you can pass on any implementation of IStudent i.e. in your unit tests you can pass the mock object of IStudent, in your actual code you will pass object of Student class. So you HomeController is now dependent on the IStudent interface (abstraction) rather than on the Student class (an implementation). This is inline with the OOP principles:

接口编程,而不是实现。

Program to interfaces, not implementations.

依靠。不要依赖于具体的类。

Depend on abstractions. Do not depend on concrete classes.

此外,它现在有一个柔软的依赖。它不再是紧耦合到Student类。它的松耦合
现在,通常你不必担心它IStudent的实施,同时您的实例,你的HomeController应该传递。这件事情的Depedency注入容器(团结在你的情况)会照顾的,只要你注册正确的接口和类吧。

Also, it now has a soft dependency. It's no longer tightly-coupled to the Student class. It's loosely-coupled. Now, generally you don't need to worry about which implementation of IStudent you should pass while instantiating your HomeController. That's something the Depedency Injection Container (Unity in your case) will take care of, as long as you register correct interface and classes with it.

_container.Register<IStudent, Student>();

因此​​,需要的HomeController的新实例时,该容器将识别IStudent的一个实例是必需的。因此,将实例化注册的实例IStudent,并把它作为放慢参数在实例HomeController类。

So when a new instance of HomeController is required, the container will identify that an instance of IStudent is required. So it will instantiate the registered instance of IStudent and pass it as paramter while instantiating HomeController class.

另外请注意,你指的是依赖注入(这是国际奥委会的一个具体形式)。还有其他形式的IoC(例如回调,观察者模式,等等)。

Also, note that what you are referring to is 'Dependency Injection' (which is one specific form of IoC). There are other forms of IoC (e.g. callbacks, Observer pattern, etc.).

编辑:
不要忘记阅读DI的热门文章

这篇关于构造与控制器参数 - MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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