gpt4 book ai didi

c# - 对在存储库模式中使用构造函数感到困惑

转载 作者:行者123 更新时间:2023-11-30 14:22:09 25 4
gpt4 key购买 nike

我是 C# 的新手,我读过构造函数,我知道我们使用它们的原因是我们可以在构造时分配我们想要的任何东西,但我很难理解的是,我有一个IRepository 学生喜欢:

    public  interface IStudentRepository
{

object getAll();
}

我在 StudentRepository 中实现了它:

     public class StudentRepository:IStudentRepository
{
public testDBEntities DB = new testDBEntities();
public object getAll()
{
var reslt = (from s in DB.Students
select new StudentViewModel
{
Name = s.Name,
Id=s.Id
});

return reslt;
}
}

在我的 homeController 中,我可以执行以下操作:

    IStudentRepository _repository=new StudentRepository();   
public JsonResult Index()
{
var m= _repository.getAll();
return Json(m,JsonRequestBehavior.AllowGet);
}

那我为什么要有一个构造函数呢?比如:

       IStudentRepository _repository=new StudentRepository();
public HomeController(StudentRepository _repository)
{
this._repository = _repository;
}
public JsonResult Index()
{
var m= _repository.getAll();
return Json(m,JsonRequestBehavior.AllowGet);
}

很抱歉,如果我的问题很基础,但我找不到正确的答案,在此先感谢

最佳答案

单一职责原则

第一个优点是您的 HomeController 不必担心创建 StudentRepository 对象。其他人已经实例化了它,从数据库、文件或用户输入或其他任何东西生成了它。这很好,因为您的 Controller 可以专注于它的真正工作,而不是如何实例化另一个对象。这越来越接近Single Responsibility Principle .

依赖注入(inject)

然后,要利用第二个,您的构造函数应该接收接口(interface)而不是类。那叫Dependency Injection .

如果以后出于某种原因,您决定以任何方式更改您的 StudentRepository 类,只要您仍然实现该接口(interface),就不必更改为你的 Controller 。

因此您的最终代码可能如下所示:

class HomeController
{
private IStudentRepository _repository;
public HomeController(IStudentRepository _repository)
{
//We don't know who instanciated the repo, but we don't care
this._repository = _repository;
}

public JsonResult Index()
{
//We only know about the repo that it implements IStudentRepository
//So we know we can call this method without a risk.
var m= _repository.getAll();
return Json(m,JsonRequestBehavior.AllowGet);
}
}

当它改变时

为了说明更改,假设您首先要测试您的存储库,但没有正确的数据库连接。所以你决定使用一个文件,你的类在那里看起来像:

public class TestStudentRepository : IStudentRepository
{
private string MyDatabaseFilePath;

public object getAll()
{
//Load everything from my text file
}
}

好的,这会起作用。现在假设您已连接到数据库,但该连接需要一些步骤才能使用 getAll() 方法。使用您的解决方案,您必须在 HomeController 中执行初始化步骤。

现在有了这个模式,你可以:

public class StudentRepository : IStudentRepository
{
private string _connectionString;

public DatabaseStudentRepository(string ConnectionString)
{
_connectionString = ConnectionString;
}

public object getAll()
{
//Load everything from my text file
}
}

然后,对象的角色使用 Controller 来确保它正确地实例化了您的对象。您的 Controller 没有变化。

工厂模式

最后一步是为您的 StudentRepository 创建一个工厂。实例化适当的 repo 是工厂的作用:

public static class StudentRepositoryFactory
{
public static IStudentRepository InstanciateRepo(bool FromDatabase)
{
if (FromDatabase)
{
return new DatabaseStudentFactory("ConnectionString To my server");
}
else
{
return new TestStudentRepository();
}
}
}

现在,无论您需要以何种方式初始化您的存储库,工厂的职责就是这样做;而使用存储库的一种方法 getAll() 是 Controller 的角色。

关于c# - 对在存储库模式中使用构造函数感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51401587/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com