[英]Why do i need to Initiate the DBContext object inside the Controller class constructor & inside the Repository class constructor
I am reading the following tutorial about asp.net mvc + Entity framework:-我正在阅读有关 asp.net mvc + Entity 框架的以下教程:-
and i find that the author is initiating the DbContext object inside the repository class's constructor as follow:-我发现作者正在存储库类的构造函数中启动 DbContext 对象,如下所示:-
public class StudentRepository : IStudentRepository, IDisposable
{
private SchoolContext context;
public StudentRepository(SchoolContext context)
{
this.context = context;
}
}
and also the author will be initiating the DBContext object inside the Controller class constructor as follow:-并且作者将在 Controller 类构造函数中启动 DBContext 对象,如下所示:-
public class StudentController : Controller
{
private IStudentRepository studentRepository;
public StudentController()
{
this.studentRepository = new StudentRepository(new SchoolContext());
}
public StudentController(IStudentRepository studentRepository)
{
this.studentRepository = studentRepository;
}
//
First Question .第一个问题。 can anyone advice what is the purpose of initiating the DBContext object inside the Repository & Controller classes Constructor ?任何人都可以建议在 Repository 和 Controller 类 Constructor 中启动 DBContext 对象的目的是什么? i mean i can replace the repository to be as follow:-我的意思是我可以将存储库替换为如下:-
public class StudentRepository : IStudentRepository, IDisposable
{
private SchoolContext context = new SchcooleContext
//No need for this constructor !!!
// public StudentRepository(SchoolContext context)
// {
// this.context = context;
// }
and keep the controller class as is ...并保持控制器类不变......
second question .第二个问题。 do i need to explicitly mention that the repository implements the IDisposable as follow:-我是否需要明确提及存储库实现 IDisposable 如下:-
public class StudentRepository : IStudentRepository, IDisposable
now if i remove IDisposable the code should work well.. so what is the purpose of explicitly implementing the IDisposable inside the Repository class ?现在,如果我删除 IDisposable 代码应该可以正常工作..那么在 Repository 类中显式实现 IDisposable 的目的是什么? i mean since i will be calling the StudentRepository.Dispose() method from the Controller class itself,, and the base Controller class by default implement the Idisposable object.. so is there any valid reason to explicitly implement the IDisposable object inside the StudentRepository ?我的意思是因为我将从 Controller 类本身调用 StudentRepository.Dispose() 方法,并且默认情况下基 Controller 类实现 Idisposable 对象..那么是否有任何正当理由在 StudentRepository 中显式实现 IDisposable 对象?
To answer your questions:回答您的问题:
can anyone advice what is the purpose of initiating the DBContext object inside the Repository & Controller classes Constructor ?任何人都可以建议在 Repository 和 Controller 类 Constructor 中启动 DBContext 对象的目的是什么?
The practice of passing in dependencies in the constructor to the classes that need them is called Dependency Injection .将构造函数中的依赖项传递给需要它们的类的做法称为依赖注入。 It is generally considered good practice and has great advantages.它通常被认为是很好的做法,并且具有很大的优势。
Actually, he is not!其实他不是! If you notice, the StudentRepository
just accepts a SchoolContext
.如果您发现,该StudentRepository
只是接受SchoolContext
。 It never instantiates anything.它从不实例化任何东西。 That means that StudentRepository
is happy taking any class that derives from SchoolContext
and it can do its work.这意味着, StudentRepository
很高兴采取任何类派生自SchoolContext
,它可以做其工作。 A common use case is, in production you will pass it the real thing, in testing you may pass it a Mock SchoolContext
which never saves to the database but maybe just an in memory list.一个常见的用例是,在生产中你会传递真实的东西,在测试中你可能会传递一个Mock SchoolContext
,它永远不会保存到数据库中,而可能只是一个内存列表。
You'll notice in StudentController
he has 1 constructor which takes an IStudentRepository
.您会注意到在StudentController
有 1 个构造函数,它采用IStudentRepository
。 This follows the same as above.这与上述相同。 Any ol' IStudentRepository
will do.任何 ol' IStudentRepository
都可以。 However, you also notice a constructor which instantiates both classes new StudentRepository(new SchoolContext());
但是,您还会注意到一个构造函数,它实例化了两个类new StudentRepository(new SchoolContext());
. . This is actually referred to as Poor Mans Dependency Injection
.这实际上被称为Poor Mans Dependency Injection
。 It's a shortcut.这是一条捷径。 In the real world you would typically see something in some startup config file that says: When a class needs an IStudentRepository
give it new StudentRepository(new SchoolContext())
.在现实世界中,您通常会在某些启动配置文件中看到一些内容:当一个类需要IStudentRepository
给它new StudentRepository(new SchoolContext())
。
do i need to explicitly mention that the repository implements the IDisposable as follow:-我是否需要明确提及存储库实现 IDisposable 如下:-
Yes.是的。 DbContext
implements IDisposable
, and so anything that wraps it should as well. DbContext
实现了IDisposable
,因此任何包装它的东西也应该如此。 The idea is that when you are done with DbConext
it needs to be closed.这个想法是,当您完成DbConext
它需要关闭。 IDisposable
communicates that. IDisposable
传达了这一点。 It is typically used for "unmanaged resources" such as files and connections that need to be "released".它通常用于“非托管资源”,例如需要“释放”的文件和连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.