0

我正在关注以下文章和存储库模式。

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net- mvc-应用程序

我一度感到困惑,控制器中定义的可选构造函数。

public StudentController(IStudentRepository studentRepository)
{
    this.studentRepository = studentRepository;
}

即使,我删除了 - 代码运行良好。这个构造函数有什么用。当我们在主默认构造函数中分配新的上下文对象时。

4

2 回答 2

0

此构造函数允许您传入 studentRepository 的不同实现。请注意,它接受一个接口,而不是存储库的具体实现。这对于单元测试很有用,您可以传入不需要访问数据库的假存储库。您还可以将此构造函数与依赖注入一起使用。

于 2013-01-09T18:38:35.863 回答
0

可选的 ctor 不会创建新的上下文,而可选的 ctor 会。上下文在 StudentRepository 的 ctor 中设置。

于 2013-01-09T18:36:34.100 回答