ASP.NET Core使用了大量的DI(Dependency Injection)设计,有用过Autofac或类似的DI Framework对此应该不陌生。
本篇将介绍ASP.NET Core的依赖注入。

DI运作方式

ASP.NET Core的DI是采用Constructor Injection,也就是说会把实例化的物件从建构子传入。例如:

1
2
3
4
5
6
7
8
9
10
public class HomeController : Controller
{
private readonly ISample _sample; public HomeController(ISample sample)
{
_sample = sample;
// ...
}
}

上述的样本,会在HomeController被实例化的时候注入进来。
每个请求都会把Controller实例化,所以从建构子注入,就能确保动作能够使用到被注入进来的_sample。

光看上面的程式码,可能会很困惑ASP.NET Core要如何知道样本的实做类别?
要注入的服务需要在启动中注册实做类别。如下:

1
2
3
4
5
6
7
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ISample, Sample>();
}
}

如此一来,在ASP.NET核心实例化控制器时,发现建构子有ISample这个类型的参数,就把Sample的实例注入给该控制器。

1.建立服务

基本上要注入到服务的类别没什么限制,除了静态类别。
以下范例程式就只是一般的类继承接口:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public interface ISample
{
Guid Id { get; }
} public interface ISampleTransient : ISample
{
} public interface ISampleScoped : ISample
{
} public interface ISampleSingleton : ISample
{
} public class Sample : ISampleTransient, ISampleScoped, ISampleSingleton
{
private Guid _id; public Sample()
{
_id = Guid.NewGuid();
} public Guid Id => _id;
}

2.注册服务

注册服务有分三种方式:

  1. 瞬态
    每次注入时,重新都new一个新的实体。
  2. 作用域
    每个请求都重新new一个新的实体。
  3. 辛格尔顿
    程式启动后会new一个实体。也就是运行期间只会有一个实体。
1
2
3
4
5
6
7
8
9
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ISampleTransient, Sample>();
services.AddScoped<ISampleScoped, Sample>();
services.AddSingleton<ISampleSingleton, Sample>();
}
}

第一个泛型为注入的类型,建议用interface来包装,这样在才能把相依关系拆除。
第二个泛型为实做的类别。

服务实例产生方式:

  • A为单身人士
  • B为Scoped
  • C为瞬态

3.注入服务

被注入的服务可以在Controller,View,Filter,Middleware或自订的服务等使用,只要是透过ASP.NET Core产生实例的类别,都可以在建构子定义
模态注入。此篇我只用Controller,服务,查看做为范例。

3.1 调节器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class HomeController : Controller
{
public readonly ISampleTransient _sampleTransient;
public readonly ISampleScoped _sampleScoped;
public readonly ISampleSingleton _sampleSingleton; public HomeController(ISampleTransient sampleTransient, ISampleScoped sampleScoped, ISampleSingleton sampleSingleton)
{
_sampleTransient = sampleTransient;
_sampleScoped = sampleScoped;
_sampleSingleton = sampleSingleton;
} public IActionResult Index()
{
var message = $"<tr><td>Transient</td><td>{_sampleTransient.Id}</td></tr>"
+ $"<tr><td>Scoped</td><td>{_sampleScoped.Id}</td></tr>"
+ $"<tr><td>Singleton</td><td>{_sampleSingleton.Id}</td></tr>";
return View(model: message);
}
}

3.2 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
public class SampleService
{
public ISampleTransient SampleTransient { get; private set; }
public ISampleScoped SampleScoped { get; private set; }
public ISampleSingleton SampleSingleton { get; private set; } public SampleService(ISampleTransient sampleTransient, ISampleScoped sampleScoped, ISampleSingleton sampleSingleton)
{
SampleTransient = sampleTransient;
SampleScoped = sampleScoped;
SampleSingleton = sampleSingleton;
}
}

注册SampleService

1
2
3
4
5
6
7
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<SampleService, SampleService>();
}
}

第一个泛型也可以是类别,不一定要是介面。

3.3 视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@model  string

@using MyWebsite

@inject ISampleTransient sampleTransient
@inject ISampleScoped sampleScoped
@inject ISampleSingleton sampleSingleton
@inject SampleService sampleService <table>
<colgroup>
<col width="100" />
</colgroup>
<tbody>
<tr><th colspan="2">Controler</th></tr>
@Html.Raw(Model) <tr><th colspan="2">Service</th></tr>
<tr><td>Transient</td><td>@sampleService.SampleTransient.Id</td></tr>
<tr><td>Scoped</td><td>@sampleService.SampleScoped.Id</td></tr>
<tr><td>Singleton</td><td>@sampleService.SampleSingleton.Id</td></tr> <tr><th colspan="2">View</th></tr>
<tr><td>Transient</td><td>@sampleTransient.Id</td></tr>
<tr><td>Scoped</td><td>@sampleScoped.Id</td></tr>
<tr><td>Singleton</td><td>@sampleSingleton.Id</td></tr>
</tbody>
</table>

执行结果

  1. Transient如预期,每次都不一样。
  2. Soped在同一个Requset中,不论是在哪边被注入,都是同样的实体。(红色箭头)
  3. Singleton不管Requset多少次,都会是同一个实体。(蓝色方框)

本文地址:https://blog.johnwu.cc/article/asp-net-core-dependency-injection.html
作者博客:John Wu

ASP.NET Core 的 Dependency Injection的更多相关文章

  1. 理解ASP.NET Core - [03] Dependency Injection

    注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 依赖注入 什么是依赖注入 简单说,就是将对象的创建和销毁工作交给DI容器来进行,调用方只需要接 ...

  2. ASP.NET Core 中文文档 第三章 原理(10)依赖注入

    原文:Dependency Injection 作者:Steve Smith 翻译:刘浩杨 校对:许登洋(Seay).高嵩 ASP.NET Core 的底层设计支持和使用依赖注入.ASP.NET Co ...

  3. Working with Data » 使用Visual Studio开发ASP.NET Core MVC and Entity Framework Core初学者教程

    原文地址:https://docs.asp.net/en/latest/data/ef-mvc/intro.html The Contoso University sample web applica ...

  4. 【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

    HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expo ...

  5. [转]【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

    本文转自:https://www.cnblogs.com/inday/p/6288707.html HTTP is not just for serving up web pages. It’s al ...

  6. Building simple plug-ins system for ASP.NET Core(转)

    Recently I built plug-ins support to my TemperatureStation IoT solution web site. The code for .NET ...

  7. 【原生态跨平台:ASP.NET Core 1.0(非Mono)在 Ubuntu 14.04 服务器上一对一的配置实现-篇幅1】

    鸡冻人心的2016,微软高产年. build 2016后 各种干货层出不穷. 1 Win10 集成了bash  ,实现了纳德拉的成诺,Microsoft Love Linux!!! 2 跨平台  ,收 ...

  8. Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 40 ASP.NET Core --- ...

  9. DI in ASP.NET Core

    .NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...

随机推荐

  1. 【开发笔记】-通过js控制input禁止输入空格

    <input type="text" id="fname" onkeyup="myFunction(id)"> <scri ...

  2. 浏览器标签页切换时jquery动画的问题

    最近公司在做大屏设备上的页面,其中动画的部分居多,开始的时候是用的jquery做的动画,在做完后无意中发现jquery动画存在一个问题,就是浏览器在切换标签页后,过段时间切换回来页面中的动画会出现连续 ...

  3. JavaScript 简单类型和复杂类型区别

    一.基本类型 1.概述 值类型又叫做基本数据类型,简单数据类型.在存储时,变量中存储的是值本身,因此叫做值类型 2.基本类型在内存中的存储 基本数据类型存储在栈区中. 3.基本类型作为函数的参数 基本 ...

  4. 英语muttonfatjade羊脂玉muttonfatjade单词

    羊脂玉英文(mutton fat jade) 中文名羊脂玉 外文名muttonfatjade 羊脂玉又称白玉,为软玉中之上品,极为珍贵.主要含有透闪石(95%).阳起石和绿帘石.非常洁白,质地细腻,光 ...

  5. python 使用Anaconda管理项目环境

    Pycharm没有内置的python解释器,需要我们自己下载python解释器. 在很多python项目中,会导入第三方的模块,逐个去下载导入很不方便. 我们通常使用Anaconda来管理python ...

  6. pandas 之 特征工程

    import numpy as np import pandas as pd So far(到目前为止) in this chapter we've been concerned with rearr ...

  7. 解决vant-weapp组件库的example的导入问题

    最近在学习小程序,看到了vant-weapp这个组件库,我比较喜欢边看示例边来敲代码.刚好这个组件库下载下来有 example的文件夹.废话不多说,现在来看看怎么在开发工具里面导入吧! 步骤: 1.下 ...

  8. BLE——低功耗蓝牙(Bluetooth Low Energy)

    1.简介 以下蓝牙协议特指低功耗蓝牙协议. 蓝牙协议是由SIG制定并维护的通信协议,蓝牙协议栈是蓝牙协议的具体实现. 各厂商都根据蓝牙协议实现了自己的一套函数库——蓝牙协议栈,所以不同厂商的蓝牙协议栈 ...

  9. python3 networkx

    一.networkx 1.用于图论和复杂网络 2.官网:http://networkx.github.io/ 3.networkx常常结合numpy等数据处理相关的库一起使用,通过matplot来可视 ...

  10. httprunner学习18-多进程运行模式

    前言 使用Locust进行性能测试时,当一台单机不足以模拟所需的用户数量的时候,可以使用主从模式,启动一个master节点,多个slave节点. 主从模式 loucsts 是httprunner 里面 ...