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. 生产环境OOM\死锁问题排查修复

    OOM: 1.快速恢复业务:如果是集群中的一台机器故障,先隔离故障服务器:如果是多台,则根据Nginx转发策略,对该功能转发到单独的集群,与其他流量隔离,确保其他业务不受影响 2.收集内存溢出Dump ...

  2. Git回滚代码

    回滚命令: 1.回退到上个版本 $ git reset --hard HEAD^ 2.回退到前2次提交之前,以此类推,回退到n次提交之前 $ git reset --hard HEAD~2 3.退到/ ...

  3. css 布局 flex

    cursor 设置鼠标放上去后的形状 visability 设置是否可见 flex 详见这篇文章https://developer.mozilla.org/zh-CN/docs/Learn/CSS/C ...

  4. 不重装nginx实现动态添加模块

    如果项目在提供服务的过程中,因为需求使然,需要对nginx进行模块的动态添加,可以按照如下流程进行操作 一.查看nginx当前已经安装了那些模块 1) 进入nginx执行文件目录 cd   /usr/ ...

  5. Reducetask机制

    Reduce大致分为copy.sort.reduce三个阶段,重点在前两个阶段.copy阶段包含一个eventFetcher来获     取已完成的map列表,由Fetcher线程去copy数据,在此 ...

  6. c语言的#和##的用法

    #include <stdio.h> #define ADD(A,B) printf(#A " + " #B " = %d\n",((A)+(B)) ...

  7. Angular vs React---React-ing to change

    这篇文章的全局观和思路一级棒! The Fairy Tale Cast your mind back to 2010 when users started to demand interactive ...

  8. Java监听器中ServletRequestListener监听Request的创建和销毁

    ServletRequest和HttpServletRequest的区别 HttpServletRequest和ServletRequest都是接口,HttpServletRequest继承自Serv ...

  9. Laravel 解决blade模板转义html标签问题

    当我们使用富文本编译器(如:Ueditor编译器)保存编辑的内容后,在blade模板中,想要显示原生的html标签内容时该怎么做? 首先,了解下laravel {{ 变量名 }} 与{!! 变量名 ! ...

  10. Spring搬迁

      Spring简介 加载bean流程 Bean的生命周期 双亲委派 自定义类加载器 Spring事务 异步Async Spring设计模式 Spring单例 SpringMVC流程   备注:app ...