종속성 주입 Unity의 조건부 해결
종속성 주입(DI)을 사용하면 종속성을 객체에 자동으로 주입할 수 있어 수동 작업의 필요성이 줄어듭니다. 인스턴스화 및 구성. 조건부 해결은 특정 조건을 기반으로 인터페이스의 다양한 구현을 동적으로 해결하는 기능을 의미합니다.
문제:
두 인증 공급자인 TwitterAuth 및 IAuthenticate 인터페이스를 구현하는 FacebookAuth:
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 */ } }
컨트롤러에 인증 공급자를 기반으로 구현을 인증합니다. 이 경우 작업 방법(예: Twitter 또는 Facebook)에 따라 결정됩니다.
솔루션으로서의 팩토리 패턴:
한 가지 접근 방식은 친구가 제안한 대로 팩토리 패턴을 사용하는 것입니다. 그러나 여기에는 각 인증 공급자에 대한 팩토리 클래스를 생성하는 작업이 포함되어 잠재적인 코드 중복 및 유지 관리 문제가 발생할 수 있습니다.
조건부 해결을 사용한 전략 패턴:
보다 유연한 솔루션 조건부 패턴과 함께 전략 패턴을 사용하는 것입니다. 해결:
인터페이스:
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) { 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); } }
각 공급자는 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 공급자 배열을 사용하여 조건부 확인을 처리합니다. 공급자 이름과 일치하는 공급자를 찾기 위해 공급자를 반복하고 여기에 로그인 작업을 위임합니다.
Unity 등록:
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") ) ));
이 등록은 Unity를 구성합니다. 공급자 이름으로 IAuthenticate 구현을 확인하고 이러한 확인된 방법을 사용하여 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가 컨트롤러에 삽입된 다음 공급자를 기반으로 로그인 작업을 위임합니다. 이름.
장점:
위 내용은 Unity 종속성 주입은 여러 인증 공급자에 대한 조건부 해결을 어떻게 처리할 수 있나요?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!