ASP.NET Core的依赖注入教程
admin
2021-06-03ASP.NET Core 中的 依赖注入介绍
本文作者:梁桐铭- 微软最有价值专家(Microsoft MVP)
本文出自《从零开始学 ASP.NET Core 与 EntityFramework Core》目录
视频课程效果更佳:跨平台开发实战掌握 ASP.NET Core 与 EntityFramework Core
ASP.NET Core 依赖注入
在这个视频中,我们将通过一个例子详细讨论依赖注入。
HomeController
public class HomeController : Controller
{
private IStudentRepository _studentRepository;
//使用构造函数注入的方式注入IStudentRepository
public HomeController(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
//返回学生的名字
public string Index()
{
return _studentRepository.GetStudent(1).Name;
}
}
注意事项 :
- HomeController依赖于IStudentRepository来 查询 Student 数据。
- 我们使用构造函数将IStudentRepository实例注入HomeController,而不是HomeController对IStudentRepository接口创建新的实例化。
- 这称为构造函数注入,因为我们使用构造函数来注入依赖项。
- 请注意,我们将注入的依赖项分配给只读字段
readonly
。这是一个很好的做法,因为它可以防止在方法中误操作地为其分配另一个值,比如 null。 - 此时,如果我们运行项目,则会收到以下错误:
InvalidOperationException: Unable to resolve service for type 'StudentManagement.Model.IStudentRepository'
while attempting to activate 'StudentManagement.Controllers.HomeController'.
- 这是因为如果有人请求实现IStudentRepository的对象,ASP .NET Core 依赖注入容器不知道要提供哪个对象实例
IStudentRepository可能有多个实现。在我们的项目中,我们只有一个实现,那就是MockStudentRepository
顾名思义,MockStudentRepository使用内存中的学生模拟数据。
在我们即将发布的视频中,我们将讨论为IStudentRepository提供另一个实现,该实现从 SQLServer 数据库中查询学生数据。
现在,让我们使用MockStudentRepository。
要修复 InvalidOperationException 错误,我们需要在 ASP.NET Core 中使用依赖注入容器注册MockStudentRepository类。
我们在 Startup 类的 ConfigureServices()方法中执行此操作
使用 ASP.NET Core 依赖注入容器注册服务:
ASP.NET Core 提供以下 3 种方法来使用依赖项注入容器注册服务。我们使用的方法决定了注册服务的生命周期。
AddSingleton()
AddSingleton()方法创建一个Singleton
服务。首次请求时会创建Singleton
服务。然后,所有后续请求都使用相同的实例。因此,通常,每个应用程序只创建一次Singleton
服务,并且在整个应用程序生命周期中使用该单个实例。
AddTransient()
AddTransient() 方法可以称作:暂时性模式,会创建一个 Transient 服务。每次请求时,都会创建一个新的 Transient 服务实例。
AddScoped()
AddScoped()方法创建一个 Scoped 服务。在范围内的每个请求中创建一个新的 Scoped 服务实例。例如,在 Web 应用程序中,它为每个 http 请求创建 1 个实例,但在同一 Web 请求中的其他调用中使用相同的实例,在一个客户端请求中是相同的,但在多个客户端请求中是不同的。
请不要担心,如果这一点有点令人困惑。我们将在本系列即将发布的视频中好好单独讲这三种方法。
现在,要修复 InvalidOperationException 错误,让我们使用AddSingleton()
向 ASP.NET Core 依赖注入容器注册MockStudentRepository类方法如下图所示。
所以在此代码中,如果有人调用IStudentRepository,将调用MockStudentRepository的实例服务。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IStudentRepository, MockStudentRepository>();
}
在这一点上,您可能在想,为什么我们必须做这一切。为什么我们不能使用 new 关键字在 HomeController 中简单地创建MockStudentRepository类的实例,如下所示。
public class HomeController : Controller
{
private readonly IStudentRepository _studentRepository;
//使用构造函数注入的方式注入IStudentRepository
public HomeController(IStudentRepository studentRepository)
{
_studentRepository = new MockStudentRepository();
}
//返回学生的名字
public string Index()
{
return _studentRepository.GetStudent(1).Name;
}
}
这使HomeController
与MockStudentRepository紧密耦合。
稍后如果我们为IStudentRepository 提供新的实现,并且如果我们想要使用新的实现而不是MockStudentRepository,我们必须更改 HomeController 中的代码。您可能会想,这只是一行代码更改,所以这并不难。
那么,如果我们在我们的应用程序中的 50 个其他控制器中使用了这个MockStudentRepository呢? 所有 50 个控制器中的代码都必须更改。这不仅无聊而且容易出错。
简而言之,使用 new 关键字创建依赖关系的实例会产生紧密耦合,因此您的应用程序将很难更改。通过依赖注入,我们不会有这种紧密耦合。
使用依赖注入,即使我们在我们的应用程序中的 50 个其他控制器中使用了MockStudentRepository,如果我们想用不同的实现交换它,我们只需要在 Startup.cs 文件中更改以下一行代码。请注意,我们现在使用DatabaseStudentRepository而不是MockStudentRepository。
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IStudentRepository, DatabaseStudentRepository>();
}
这样带来的效果是单元测试也变得更加容易,因为我们可以通过依赖注入轻松地交换依赖项。 如果这有点令人困惑,请不要担心。我们将在即将发布的视频中为IStudentRepository提供不同的实现。此新实现将从 SQLServer 数据库中查询数据。然后,我们将使用DatabaseStudentRepository实现替换MockStudentRepository实现。那个时候,您将了解依赖注入提供的功能和灵活性。
文章说明
如果您觉得我的文章质量还不错,欢迎打赏,也可以订阅我的视频哦
未得到授权不得擅自转载本文内容,52abp.com 保留版权
感谢您对我的支持