第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

如何在 ASP.NET Core 中使用 FromServices

標(biāo)簽:
C#

ASP.NET Core 中内置了对依赖注入的支持,可以使用 依赖注入 的方式在运行时实现组件注入,这样可以让代码更加灵活,测试和可维护,通常有三种方式可以实现依赖注入。

  • 构造函数注入

  • 属性注入

  • 方法注入

构造函数 这种注入方式在 ASP.NET Core 中应用的是最广的,可想而知,只用这种方式也不是 放之四海而皆准 ,比如说,我不希望每次 new class 的时候都不得不注入,换句话说,我想把依赖注入的粒度缩小,我希望只对某一个或者某几个方法单独实现注入,而不是全部,首先这能不能实现呢?实现肯定是没有问题的,只需用 FromServices 特性即可,它可以实现对 Controller.Action 单独注入。

这篇文章我们将会讨论如何在 ASP.NET Core 中使用 FromServices 特性实现依赖注入,同时我也会演示最通用的 构造函数注入

使用构造函数注入

接下来先通过 构造函数 的方式实现依赖注入,考虑下面的 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;
        }
    }

要想实现依赖注入,还需要将 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();
        }

下面的代码片段展示了如何通过 构造函数 的方式实现注入。


    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 简介

FromServicesAttribute 特性是在 Microsoft.AspNetCore.Mvc 命名空间下,通过它可以直接将service注入到action方法中,下面是 FromServicesAttribute 的源码定义:


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

        public BindingSource BindingSource { get; }
    }

使用 FromServices 依赖注入

接下来将 FromServices 注入到 Action 方法参数上,实现运行时参数的依赖解析,知道这些基础后,现在可以把上一节中的 构造函数注入 改造成 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();
        }
    }

总的来说,如果你只想在某些Action上而不是整个 Controller 中使用依赖注入,那么使用 FromServices 将是一个非常好的选择,而且还可以让你的代码更加干净,更加可维护。

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消