註冊地:簡單的注射器:循環圖錯誤
container.Register<IAuthenticationHandler, AuthenticationHandler>(Lifestyle.Transient);
container.Register<IUserHandler, UserHandler>(Lifestyle.Transient);
第1類:
public UserHandler(IAuthenticationHandler authenticationHandler)
{
_authenticationHandler = authenticationHandler;
}
2類:
public AuthenticationHandler(IUserHandler userHandler)
{
_userHandler = userHandler;
}
我明白什麼叫做循環問題是。當UserHandler初始化時,它注入AuthenticationHandler實現,然後嘗試創建UserHandler實例並開始循環...
我的問題是我該如何解決這個問題(SIMPLE INJECTOR)這種情況和其他我需要像這樣注入?
謝謝!
UPDATE:
function AddUser(User user){ // User Handler
_authenticationHandler.GenerateRandomSalt();
string hashedPassword = _authenticationHandler.HashPassword(user.Password.HashedPassword, salt);
}
function Authenticate(string username, string password){ // Authentication Handler
_userHandler.GetUserByUsername(username?.Trim());
}
Bascially我需要調用中的AuthenticationHandler的UserHandler來獲取用戶,並確認有用戶。
我需要調用UserHandler中的AuthenticationHandler來獲取函數來加密和散列密碼。
我想我可以調用庫,以獲取用戶,但我不應該去通過的情況下,更多的東西在用戶服務做處理這個
將需要更多的例子來說明如何打破這個循環。我可以肯定地說的一個暗示是,你選擇DI容器在這裏並不重要。 – jdphenix