asp.net core选项Options模块的笔记
这篇博客是写给自己看的。已经不止一次看到AddOptions的出现,不管是在.net core源码还是别人的框架里面,都充斥着AddOptions。于是自己大概研究了下,没有深入,因为,我的功力还是不够,等能力到了再回头研究下。在这里还是要说一遍,因为DI的重要性不言而喻,不必谈的太深,说下自己的理解:
DI实现其实很简单,首先设计类来实现接口,而不是把所有的程序逻辑写在一个类文件中,然后我们传入一个接口和一个继承自接口的类作为参数,然后我们在相应的函数那将泛型参数T作为形参,伪代码:
//调用部分
HandleDI<ITest, Test>
//实现部分
HandleDI<TInterface, T>
// 使用反射,EMIT,委托来实例化T创建TInterface的对象
然后我们使用反射或者EMIT或是委托TInterface对象。这就是DI的实现过程。
DI说白了,作用就是 解耦的 实例化继承自接口的类
如果在程序中基于IOptions<TOptions>实现了你自己的选项配置类,最好就是调用AddOptions完成Options的几个重要对象的实例化。
AddOptions:
完成几个重要的Options对象的实例化:
public static IServiceCollection AddOptions(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));
services.TryAdd(ServiceDescriptor.Transient(typeof(IOptionsFactory<>), typeof(OptionsFactory<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>)));
return services;
}
Configure: 完成的是TOption类的实例化过程,最终调用AddSingleton做DI。
/// <summary>
/// Registers an action used to configure a particular type of options.
/// Note: These are run before all <seealso cref="PostConfigure{TOptions}(IServiceCollection, Action{TOptions})"/>.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="configureOptions">The action used to configure the options.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, Action<TOptions> configureOptions) where TOptions : class
=> services.Configure(Options.Options.DefaultName, configureOptions); /// <summary>
/// Registers an action used to configure a particular type of options.
/// Note: These are run before all <seealso cref="PostConfigure{TOptions}(IServiceCollection, Action{TOptions})"/>.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <param name="name">The name of the options instance.</param>
/// <param name="configureOptions">The action used to configure the options.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string name, Action<TOptions> configureOptions)
where TOptions : class
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
} if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
} services.AddOptions();
services.AddSingleton<IConfigureOptions<TOptions>>(new ConfigureNamedOptions<TOptions>(name, configureOptions));
return services;
}
这两个的实现,都是IOC的功劳,如果不去深究其作用,单单就是看代码,其实就是DI的实例化接口类。
参考文章:ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor
asp.net core选项Options模块的笔记的更多相关文章
- 理解ASP.NET Core - 选项(Options)
注:本文隶属于<理解ASP.NET Core>系列文章,请查看置顶博客或点击此处查看全文目录 Options绑定 上期我们已经聊过了配置(IConfiguration),今天我们来聊一聊O ...
- 【笔记目录2】【jessetalk 】ASP.NET Core快速入门_学习笔记汇总
当前标签: ASP.NET Core快速入门 共2页: 上一页 1 2 任务27:Middleware管道介绍 GASA 2019-02-12 20:07 阅读:15 评论:0 任务26:dotne ...
- 基于ASP.NET core的MVC站点开发笔记 0x01
基于ASP.NET core的MVC站点开发笔记 0x01 我的环境 OS type:mac Software:vscode Dotnet core version:2.0/3.1 dotnet sd ...
- 《ASP.NET Core In Action》读书笔记系列,这是一个手把手的从零开始的教学系列目录
最近打算系统学习一下asp.net core ,苦于没有好的中文书藉,只好找来一本英文的 <ASP.NET Core In Action>学习.我和多数人一样,学习英文会明显慢于中文.希 ...
- ASP.NET Core 选项模式源码学习Options Configure(一)
前言 ASP.NET Core 后我们的配置变得更加轻量级了,在ASP.NET Core中,配置模型得到了显著的扩展和增强,应用程序配置可以存储在多环境变量配置中,appsettings.json用户 ...
- ASP.NET Core Module overview模块概述
原文地址:ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross ASP.NET Core模块(ANCM)让你 ...
- 《ASP.NET Core In Action》读书笔记系列四 创建ASP.NET Core 应用步骤及相应CLI命令
一般情况下,我们都是从一个模板(template)开始创建应用的(模板:提供构建应用程序所需的基本代码).本节使用 Visual Studio 2017 .ASP.NET Core2.0和 Visua ...
- 《ASP.NET Core In Action》读书笔记系列二 ASP.NET Core 能用于什么样的应用,什么时候选择ASP.NET Core
ASP.NET Core 能用于什么样的应用 ASP.NET Core 可以用作传统的web服务.RESTful服务.远程过程调用(RPC)服务.微服务,这归功于它的跨平台支持和轻量级设计.如下图所示 ...
- asp.net core mvc视频A:笔记1.基本概念介绍
此笔记来自视频教程 MVC本身与三层架构没有联系 使用VS2017新建一个默认的asp.net core mvc网站,认识结构及文件用途.
随机推荐
- iis配置问题
最近调试程序时发现一直用的是vs自带的服务器 当我切换成iis时,发现虽然能显示界面,却连不上数据库 (程序数据库的一系列操作是通过wcf ria完成的) 以前在winserver2012上也遇到过这 ...
- verilog-testbench 时钟和复位模板
/********************************************* ** Clocks & Reset ******************************* ...
- linux学习笔记:第二单元 UNIX和Linux操作系统概述
第二单元 UNIX和Linux操作系统概述 UNIX是什么 UNIX操作系统的特点 UNIX 与Linux的关系 GNU项目与自由软件 GUN计划 自由软件意味着什么 Linux简介 Linux是什么 ...
- 【sqli-labs】Less1~Less4
学习sql注入啦,一下都是我做sqli-labs时的笔记.可能有错误,如果有人发现了欢迎指正~~ 常用知识点: 1.mysql注释有三种:① #: 注释从#到行尾 ② --空格: 注释到行尾,注意-- ...
- Spring Security Filter执行顺序
1.场景:先走框架过滤器,后走自定义过滤器 @Bean public FilterRegistrationBean resourceFilterRegistration() { FilterRegis ...
- C++ Primer 笔记——模板与泛型编程
1.编译器用推断出的模板参数来为我们实例化一个特定版本的函数. 2.每个类型参数前必须使用关键字class或typename.在模板参数列表中,这两个关键字含义相同,可以互换使用,也可以同时使用. t ...
- 调试阶段 获取微信小程序openid
wx.login({ success: function(res) { //首先获取用户code //res.code wx.request({ url: 'https://api.weixin.qq ...
- docker 给none镜像打镜像
1.遇到none的镜像打tag方式: docker tag + docker ID + 命名:版本名 案例:docker tag 41b7307026c0 gitlab:test 这就 ...
- .NET编码解码(HtmlEncode与HtmlDecode)
编码代码: System.Web.HttpUtility.HtmlEncode("<a href=\"http://hovertree.com/\">何问起& ...
- Linux下安装软件命令详解
---------------------------------------------------------------- 或许你对于linux还不够了解,但是一旦你步入公司后,你就会发现lin ...