gpt4 book ai didi

c# - 依赖注入(inject) Unity - 条件解析

转载 作者:可可西里 更新时间:2023-11-01 07:49:23 25 4
gpt4 key购买 nike

条件解析是我目前最不理解的事情。

假设我们有一个接口(interface) IAuthenticate:

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

现在我有两种类型的身份验证。

推特验证

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

}

Facebook 身份验证

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

}

在统一配置中注册类型:

unityContainer.RegisterType<IAuthenticate, TwitterAuth>();
unityContainer.RegisterType<IAuthenticate, FacebookAuth>();

在我们的 Controller 中通过 DI 注入(inject)对象:

private readonly IAuthenticate _authenticate;

public AuthenticateController(IAuthenticate authenticate)
{
_authenticate = authenticate;
}



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



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



// login with google
public virtual ActionResult Google(string user, string pass)
{
bool success =
_authenticate.Login(user, pass);
}

unity 究竟如何知道它必须为不同类型的身份验证解析哪个对象?在这种情况下,我该如何进行条件解析?

我和我的 friend 谈过,他解释说如果出现这种情况是设计错误,但这只是使用的工厂模式。

最佳答案

解决这个问题的一个简单方法是使用 strategy pattern .请注意,您可以在不更改设计的情况下添加或删除登录提供程序 - 您只需更改 DI 配置。

接口(interface)

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 pattern
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")
)
));

用法

private readonly IAuthenticateStrategy _authenticateStrategy;

public AuthenticateController(IAuthenticateStrategy authenticateStrategy)
{
if (authenticateStrategy == null)
throw new ArgumentNullException("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);
}

unity.config

Instead of "Unity Registration" you could do this on your unity.config

<register type="IAuthenticate" mapTo="TwitterAuth" name="twitterAuth" />
<register type="IAuthenticate" mapTo="FacebookAuth" name="facebookAuth" />
<register type="IAuthenticateStrategy" mapTo="AuthenticateStrategy" />

关于c# - 依赖注入(inject) Unity - 条件解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com