国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁(yè) 后端開(kāi)發(fā) C++ Unity 依賴(lài)注入如何處理多個(gè)身份驗(yàn)證提供程序的條件解析?

Unity 依賴(lài)注入如何處理多個(gè)身份驗(yàn)證提供程序的條件解析?

Dec 31, 2024 pm 06:00 PM

How Can Unity Dependency Injection Handle Conditional Resolution for Multiple Authentication Providers?

依賴(lài)注入 Unity 中的條件解析

依賴(lài)注入 (DI) 允許將依賴(lài)項(xiàng)自動(dòng)注入到對(duì)象中,從而減少手動(dòng)操作的需要實(shí)例化和配置。條件解析是指根據(jù)特定條件動(dòng)態(tài)解析接口的不同實(shí)現(xiàn)的能力。

問(wèn)題:

考慮以下場(chǎng)景,其中有兩個(gè)身份驗(yàn)證提供程序:TwitterAuth 和FacebookAuth,實(shí)現(xiàn) IAuthenticate 接口:

public interface IAuthenticate
{
    bool Login(string user, string pass);
}

public class TwitterAuth : IAuthenticate
{
    bool Login(string user, string pass) { /* connect to twitter api */ }
}

public class FacebookAuth : IAuthenticate
{
    bool Login(string user, string pass) { /* connect to fb api */ }
}

在控制器中,您想要注入一個(gè)基于身份驗(yàn)證提供程序的 IAuthenticate 實(shí)現(xiàn),在本例中由操作方法(例如 Twitter 或 Facebook)確定。

工廠模式作為解決方案:

一種方法是按照您朋友的建議使用工廠模式。但是,這涉及為每個(gè)身份驗(yàn)證提供程序創(chuàng)建一個(gè)工廠類(lèi),從而導(dǎo)致潛在的代碼重復(fù)和維護(hù)問(wèn)題。

具有條件解析的策略模式:

更靈活的解決方案就是將策略模式與條件結(jié)合使用解析:

接口:

public interface IAuthenticate
{
    bool Login(string user, string pass);
    bool AppliesTo(string providerName);
}

public interface IAuthenticateStrategy
{
    bool Login(string providerName, string user, string pass);
}

驗(yàn)證提供商:

public class TwitterAuth : IAuthenticate
{
    bool Login(string user, string pass) { /* connect to twitter api */ }

    bool AppliesTo(string providerName)
    {
        return this.GetType().Name.Equals(providerName);
    }
}

public class FacebookAuth : IAuthenticate
{
    bool Login(string user, string pass) { /* connect to fb api */ }

    bool AppliesTo(string providerName)
    {
        return this.GetType().Name.Equals(providerName);
    }
}

每個(gè)提供商都實(shí)現(xiàn) IAuthenticate 并提供確定它是否適用于給定提供者的方法name.

策略:

public class AuthenticateStrategy : IAuthenticateStrategy
{
    private readonly IAuthenticate[] authenticateProviders;

    public AuthenticateStrategy(IAuthenticate[] authenticateProviders)
    {
        this.authenticateProviders = authenticateProviders;
    }

    public bool Login(string providerName, string user, string pass)
    {
        var provider = authenticateProviders.FirstOrDefault(x => x.AppliesTo(providerName));
        if (provider == null) throw new Exception("Login provider not registered");
        return provider.Login(user, pass);
    }
}

該策略采用一組 IAuthenticate 提供程序并處理?xiàng)l件解析。它遍歷提供者以找到與提供者名稱(chēng)匹配的提供者,并將登錄操作委托給它。

Unity 注冊(cè):

unityContainer.RegisterType<IAuthenticate, TwitterAuth>("twitterAuth");
unityContainer.RegisterType<IAuthenticate, FacebookAuth>("facebookAuth");
unityContainer.RegisterType<IAuthenticateStrategy, AuthenticateStrategy>(
    new InjectionConstructor(
        new ResolvedArrayParameter<IAuthenticate>(
            new ResolvedParameter<IAuthenticate>("twitterAuth"),
            new ResolvedParameter<IAuthenticate>("facebookAuth")
        )
    ));

此注冊(cè)配置 Unity通過(guò)提供者名稱(chēng)解析 IAuthenticate 實(shí)現(xiàn)并使用這些解析注入 AuthenticateStrategy

用法:

private readonly IAuthenticateStrategy _authenticateStrategy;

public AuthenticateController(IAuthenticateStrategy authenticateStrategy)
{
    _authenticateStrategy = authenticateStrategy;
}

// login with twitter
public virtual ActionResult Twitter(string user, string pass)
{
    bool success = _authenticateStrategy.Login("TwitterAuth", user, pass);
}

// login with fb
public virtual ActionResult Facebook(string user, string pass)
{
    bool success = _authenticateStrategy.Login("FacebookAuth", user, pass);
}

AuthenticateStrategy 被注入到控制器中,然后控制器根據(jù)提供者委托登錄操作

優(yōu)點(diǎn):

  • 靈活的設(shè)計(jì):添加或刪除登錄提供程序非常簡(jiǎn)單,只需更改 Unity 注冊(cè)即可。
  • 可維護(hù):減少重復(fù)并提高代碼可讀性,因?yàn)樘囟ㄓ谔峁┏绦虻拇a包含在單獨(dú)的類(lèi)中。
  • 可測(cè)試:策略模式可以更輕松地對(duì)各個(gè)身份驗(yàn)證提供程序進(jìn)行單元測(cè)試。

以上是Unity 依賴(lài)注入如何處理多個(gè)身份驗(yàn)證提供程序的條件解析?的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開(kāi)發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺(jué)化網(wǎng)頁(yè)開(kāi)發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門(mén)話題

c多態(tài)性:功能是否超載一種多態(tài)性? c多態(tài)性:功能是否超載一種多態(tài)性? Jun 20, 2025 am 12:05 AM

是的,函數(shù)重載是C 中的一種多態(tài)形式,具體來(lái)說(shuō)是編譯時(shí)多態(tài)。1.函數(shù)重載允許使用相同名稱(chēng)但不同參數(shù)列表的多個(gè)函數(shù)。2.編譯器根據(jù)提供的參數(shù)在編譯時(shí)決定調(diào)用哪個(gè)函數(shù)。3.與運(yùn)行時(shí)多態(tài)不同,函數(shù)重載在運(yùn)行時(shí)沒(méi)有額外開(kāi)銷(xiāo),實(shí)現(xiàn)簡(jiǎn)單,但靈活性較低。

C中有哪種多態(tài)性的多態(tài)性?解釋了 C中有哪種多態(tài)性的多態(tài)性?解釋了 Jun 20, 2025 am 12:08 AM

C 有兩種主要的多態(tài)類(lèi)型:編譯時(shí)多態(tài)和運(yùn)行時(shí)多態(tài)。1.編譯時(shí)多態(tài)通過(guò)函數(shù)重載和模板實(shí)現(xiàn),提供高效但可能導(dǎo)致代碼膨脹。2.運(yùn)行時(shí)多態(tài)通過(guò)虛函數(shù)和繼承實(shí)現(xiàn),提供靈活性但有性能開(kāi)銷(xiāo)。

C:多態(tài)性真的有用嗎? C:多態(tài)性真的有用嗎? Jun 20, 2025 am 12:01 AM

是的,C 中的多態(tài)性非常有用。 1)它提供了靈活性,允許輕松添加新類(lèi)型;2)促進(jìn)代碼重用,減少重復(fù);3)簡(jiǎn)化維護(hù),使代碼更易擴(kuò)展和適應(yīng)變化。盡管存在性能和內(nèi)存管理的挑戰(zhàn),但其優(yōu)勢(shì)在復(fù)雜系統(tǒng)中尤為顯著。

C驅(qū)動(dòng)器:常見(jiàn)錯(cuò)誤 C驅(qū)動(dòng)器:常見(jiàn)錯(cuò)誤 Jun 20, 2025 am 12:12 AM

C destructorscanleadtoseveralcommonerrors.Toavoidthem:1)Preventdoubledeletionbysettingpointerstonullptrorusingsmartpointers.2)Handleexceptionsindestructorsbycatchingandloggingthem.3)Usevirtualdestructorsinbaseclassesforproperpolymorphicdestruction.4

c認(rèn)識(shí)python的人的教程 c認(rèn)識(shí)python的人的教程 Jul 01, 2025 am 01:11 AM

學(xué)Python的人轉(zhuǎn)學(xué)C 最直接的困惑是:為什么不能像Python那樣寫(xiě)?因?yàn)镃 雖然語(yǔ)法更復(fù)雜,但提供了底層控制能力和性能優(yōu)勢(shì)。1.語(yǔ)法結(jié)構(gòu)上,C 使用花括號(hào){}而非縮進(jìn)組織代碼塊,且變量類(lèi)型必須顯式聲明;2.類(lèi)型系統(tǒng)與內(nèi)存管理方面,C 沒(méi)有自動(dòng)垃圾回收機(jī)制,需手動(dòng)管理內(nèi)存并注意釋放資源,使用RAII技術(shù)可輔助資源管理;3.函數(shù)與類(lèi)定義中,C 需要明確訪問(wèn)修飾符、構(gòu)造函數(shù)和析構(gòu)函數(shù),并支持如運(yùn)算符重載等高級(jí)功能;4.標(biāo)準(zhǔn)庫(kù)方面,STL提供了強(qiáng)大的容器和算法,但需要適應(yīng)泛型編程思想;5

C中的多態(tài)性:綜合指南 C中的多態(tài)性:綜合指南 Jun 21, 2025 am 12:11 AM

C 中的多態(tài)性分為運(yùn)行時(shí)多態(tài)性和編譯時(shí)多態(tài)性。1.運(yùn)行時(shí)多態(tài)性通過(guò)虛函數(shù)實(shí)現(xiàn),允許在運(yùn)行時(shí)動(dòng)態(tài)調(diào)用正確的方法。2.編譯時(shí)多態(tài)性通過(guò)函數(shù)重載和模板實(shí)現(xiàn),提供更高的性能和靈活性。

C中的多態(tài)性的各種形式是什么? C中的多態(tài)性的各種形式是什么? Jun 20, 2025 am 12:21 AM

C polymorphismincludescompile-time,runtime,andtemplatepolymorphism.1)Compile-timepolymorphismusesfunctionandoperatoroverloadingforefficiency.2)Runtimepolymorphismemploysvirtualfunctionsforflexibility.3)Templatepolymorphismenablesgenericprogrammingfo

C多態(tài)性:編碼樣式 C多態(tài)性:編碼樣式 Jun 19, 2025 am 12:25 AM

C polymorphismisuniqueduetoitscombinationofcompile-timeandruntimepolymorphism,allowingforbothefficiencyandflexibility.Toharnessitspowerstylishly:1)Usesmartpointerslikestd::unique_ptrformemorymanagement,2)Ensurebaseclasseshavevirtualdestructors,3)Emp

See all articles