栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

依赖注入统一性-条件解析

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

依赖注入统一性-条件解析

解决此问题的一种简单方法是使用策略模式。请注意,您可以添加或删除登录提供程序而无需更改设计-
您只需要更改DI配置即可。

介面

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

验证提供者

public class TwitterAuth : IAuthenticate{    bool Login(string user, string pass)    {        //connect to twitter api    }    bool AppliesTo(string providerName)    {        // I used the type name for this example, but        // note that you could use any string or other        // datatype to select the correct provider.        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);    }}

战略

public class AuthenticateStrategy: IAuthenticateStrategy{    private readonly IAuthenticate[] authenticateProviders;    public AuthenticateStrategy(IAuthenticate[] authenticateProviders)    {        if (authenticateProviders == null) throw new ArgumentNullException("authenticateProviders");        this.authenticateProviders = authenticateProviders;    }    public bool Login(string providerName, string user, string pass)    {        var provider = this.authenticateProviders .FirstOrDefault(x => x.AppliesTo(providerName));        if (provider == null)        { throw new Exception("Login provider not registered");        }        return provider.Login(user, pass);    }}

统一注册

// Note that the strings used here for instance names have nothing // to do with the strings used to select the instance in the strategy patternunityContainer.RegisterType<IAuthenticate, TwitterAuth>("twitterAuth");unityContainer.RegisterType<IAuthenticate, FacebookAuth>("facebookAuth");unityContainer.RegisterType<IAuthenticateStrategy, AuthenticateStrategy>(    new InjectionConstructor(        new ResolvedArrayParameter<IAuthenticate>( new ResolvedParameter<IAuthenticate>("twitterAuth")        ),        new ResolvedArrayParameter<IAuthenticate>( new ResolvedParameter<IAuthenticate>("facebookAuth")        )    ));

用法

private readonly IAuthenticateStrategy _authenticateStrategy;public AuthenticateController(IAuthenticateStrategy authenticateStrategy){    if (authenticateStrategy == null)        throw new ArgumentNullException("authenticateStrategy");    _authenticateStrategy = authenticateStrategy;}// login with twitterpublic virtual ActionResult Twitter(string user, string pass){    bool success = _authenticateStrategy.Login("TwitterAuth", user, pass);}// login with fbpublic virtual ActionResult Facebook(string user, string pass){    bool success = _authenticateStrategy.Login("FacebookAuth", user, pass);}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号