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. JavaScript 调试 debug

    一.错误 1.语法错误 出现错误,有提示,很容易的解决. 2.逻辑错误 不容易发现 二.调试方式 1.alert() 方式 2.console.log()/console.error() 方式 3.断 ...

  2. 【转载】【凯子哥带你学Framework】Activity界面显示全解析(下)

    如何验证上一个问题 首先,说明一下运行条件 //主题 name="AppTheme" parent="@android:style/Theme.Holo.Light.No ...

  3. 如何测试Web服务.1

    一.什么是web服务  web服务在简单术语中可被定义为通过安装了特定设备或服务器到另一装置或客户端应用程序通过WWW彼此通信后的应用程序(万维网)提供的服务. Web服务通常在计算机网络的应用层上使 ...

  4. springboot之DevTools热部署的简单原理解析

    IDEA新建springboot选择DevTools springboot-devtools模块能够实现热部署,添加类.添加方法,修改配置文件,修改页面等,都能实现热部署. 原理就是重启项目,但比手动 ...

  5. Flux 和 Mono 的区别

    Flux 和 Mono 是 Reactor 中的两个基本概念.Flux 表示的是包含 0 到 N 个元素的异步序列.在该序列中可以包含三种不同类型的消息通知:正常的包含元素的消息.序列结束的消息和序列 ...

  6. 一个使用vue和echarts结合的demo

    前端框架真的很神奇,接触了几天vue.js,用它结合echarts做一个数据分析图,效果如下: 附上源码地址:https://github.com/qingguoYan/orderVue.git

  7. 类数组对象与arguments

    类数组对象 所谓的类数组对象: 拥有一个 length 属性和若干索引属性的对象 举个例子: var array = ['name', 'age', 'sex']; var arrayLike = { ...

  8. node 进程管理 child_process [spawn,exec] | 珠峰培训node正式课 网络爬虫

    run.js ; function set(){ i++ setTimeout(function(){ console.log(i) ){ set(); } },) }set(); child_pro ...

  9. OpenCV 学习笔记(9)RGB转换成灰度图像的一个常用公式Gray = R*0.299 + G*0.587 + B*0.114

    https://blog.csdn.net/fly_wt/article/details/86432886 RGB转换成灰度图像的一个常用公式是:Gray = R*0.299 + G*0.587 + ...

  10. LeetCode 934. Shortest Bridge

    原题链接在这里:https://leetcode.com/problems/shortest-bridge/ 题目: In a given 2D binary array A, there are t ...