自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何在 ASP.NET Core 中使用 FromServices

開(kāi)發(fā) 前端

ASP.NET Core 中內(nèi)置了對(duì)依賴注入的支持,可以使用 依賴注入 的方式在運(yùn)行時(shí)實(shí)現(xiàn)組件注入,這樣可以讓代碼更加靈活,測(cè)試和可維護(hù),通常有三種方式可以實(shí)現(xiàn)依賴注入。

構(gòu)造函數(shù)注入

屬性注入

方法注入

構(gòu)造函數(shù) 這種注入方式在 ASP.NET Core 中應(yīng)用的是最廣的,可想而知,只用這種方式也不是 放之四海而皆準(zhǔn) ,比如說(shuō),我不希望每次 new class 的時(shí)候都不得不注入,換句話說(shuō),我想把依賴注入的粒度縮小,我希望只對(duì)某一個(gè)或者某幾個(gè)方法單獨(dú)實(shí)現(xiàn)注入,而不是全部,首先這能不能實(shí)現(xiàn)呢?實(shí)現(xiàn)肯定是沒(méi)有問(wèn)題的,只需用 FromServices 特性即可,它可以實(shí)現(xiàn)對(duì) Controller.Action 單獨(dú)注入。

這篇文章我們將會(huì)討論如何在 ASP.NET Core 中使用 FromServices 特性實(shí)現(xiàn)依賴注入,同時(shí)我也會(huì)演示最通用的 構(gòu)造函數(shù)注入 。

使用構(gòu)造函數(shù)注入接下來(lái)先通過(guò) 構(gòu)造函數(shù) 的方式實(shí)現(xiàn)依賴注入,考慮下面的 ISecurityService 接口。

public interface ISecurityService { bool Validate(string userID, string password); } public class SecurityService : ISecurityService { public bool Validate(string userID, string password) { //Write code here to validate the user credentials return true; } }

要想實(shí)現(xiàn)依賴注入,還需要將 SecurityService 注入到 ServiceCollection 容器中,如下代碼所示:

// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddControllersWithViews(); }

下面的代碼片段展示了如何通過(guò) 構(gòu)造函數(shù) 的方式實(shí)現(xiàn)注入。

public class HomeController : Controller { private readonly ILogger _logger; private readonly ISecurityService _securityService; public HomeController(ILogger logger, ISecurityService securityService) { _logger = logger; _securityService = securityService; } public IActionResult Index() { var isSuccess = _securityService.Validate(string.Empty, string.Empty); return View(); } }

FromServicesAttribute 簡(jiǎn)介FromServicesAttribute 特性是在 Microsoft.AspNetCore.Mvc 命名空間下,通過(guò)它可以直接將service注入到action方法中,下面是 FromServicesAttribute 的源碼定義:

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] public class FromServicesAttribute : Attribute, IBindingSourceMetadata { public FromServicesAttribute(); public BindingSource BindingSource { get; } }

使用 FromServices 依賴注入接下來(lái)將 FromServices 注入到 Action 方法參數(shù)上,實(shí)現(xiàn)運(yùn)行時(shí)參數(shù)的依賴解析,知道這些基礎(chǔ)后,現(xiàn)在可以把上一節(jié)中的 構(gòu)造函數(shù)注入 改造成 FromServices注入,如下代碼所示:

public class HomeController : Controller { private readonly ILogger _logger; public HomeController(ILogger logger) { _logger = logger; } public IActionResult Index([FromServices] ISecurityService securityService) { var isSuccess = securityService.Validate(string.Empty, string.Empty); return View(); } }

圖片

總的來(lái)說(shuō),如果你只想在某些Action上而不是整個(gè) Controller 中使用依賴注入,那么使用 FromServices 將是一個(gè)非常好的選擇,而且還可以讓你的代碼更加干凈,更加可維護(hù)。

譯文鏈接:https://www.infoworld.com/article/3451821/how-to-use-the-fromservices-attribute-in-aspnet-core.html

 

責(zé)任編輯:武曉燕
相關(guān)推薦

2021-02-03 13:35:25

ASPweb程序

2021-03-03 22:37:16

MediatR中介者模式

2021-03-10 09:40:43

LamarASP容器

2021-02-28 20:56:37

NCache緩存框架

2021-01-28 22:39:35

LoggerMessa開(kāi)源框架

2021-01-07 07:39:07

工具接口 Swagger

2021-03-17 09:45:31

LazyCacheWindows

2021-02-06 21:40:13

SignalR通訊TypeScript

2021-02-02 16:19:08

Serilog日志框架

2021-02-07 17:29:04

監(jiān)視文件接口

2021-01-26 14:57:00

中間件應(yīng)用模塊化

2021-04-12 07:03:10

輕量級(jí)模塊化框架

2021-01-04 05:44:54

框架日志

2021-06-22 16:59:56

微軟.NETC# 軟件開(kāi)發(fā)

2017-10-20 08:52:11

內(nèi)存緩存并發(fā)模式Linux

2022-08-01 08:00:00

開(kāi)發(fā)工具跟蹤偵聽(tīng)器

2009-02-05 14:02:46

SmtpMail發(fā)送郵件ASP.NET

2021-04-14 07:35:12

Json格式化日期

2021-11-01 14:52:38

ElasticSear索引SQL

2021-02-17 08:51:55

cookie身份驗(yàn)證
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)