简单注入器:循环图错误

use*_*265 5 .net c# dependency-injection simple-injector

注册表:

container.Register<IAuthenticationHandler, AuthenticationHandler>(Lifestyle.Transient);
container.Register<IUserHandler, UserHandler>(Lifestyle.Transient);    
Run Code Online (Sandbox Code Playgroud)

第一类:

public UserHandler(IAuthenticationHandler authenticationHandler)
{
    _authenticationHandler = authenticationHandler;
} 
Run Code Online (Sandbox Code Playgroud)

第 2 类:

public AuthenticationHandler(IUserHandler userHandler)
{
    _userHandler = userHandler;
}
Run Code Online (Sandbox Code Playgroud)

我明白循环问题是什么。当 UserHandler 初始化时,它正在注入 AuthenticationHandler 实现,然后尝试创建 UserHandler 实例,循环开始......

我的问题是如何在(简单注射器)这种情况下以及我需要像这样注射的其他情况下解决这个问题?

谢谢!

更新:

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());
}
Run Code Online (Sandbox Code Playgroud)

基本上我需要在 AuthenticationHandler 中调用 UserHandler 来获取用户并验证是否有用户。

我需要在 UserHandler 中调用 AuthenticationHandler 来获取对密码进行加盐和散列的函数。

我想我可以调用 Repository 来获取用户,但我不应该通过服务处理程序,以防在用户服务中完成更多工作

Ste*_*ven 4

循环依赖通常是由于违反SOLID原则而引起的,因为具有太广泛接口和太多功能的类更有可能需要彼此的功能。

我相信您的情况也是如此,因为UserHandler.AddUser功能取决于AuthenticationHandler.GenerateRandomSalt和功能,而(ie )HashPassword的功能取决于 的另一个功能。这强烈表明抽象实际上违反了接口隔离原则,并且其实现违反了单一职责原则AuthenticationHandlerAuthenticateUserHandlerIAuthenticationHandler

解决方案是将IAuthenticationHandler其及其实现拆分为多个独立的部分。例如

interface IPasswordUtilities {
    // NOTE: I believe GenerateRandomSalt is an implementation detail;
    // it should not be part of the interface
    string HashPassword(string plainPassword);
}

interface IAuthenticationHandler {
    void Authenticate(string username, string password);
}

class PasswordUtilities : IPasswordUtilities {
    // implementation
}

class AuthenticationHandler : IAuthenticationHandler {
    public AuthenticationHandler(IUserHandler userHandler, IPasswordUtilities utilities) { 
        ... 
    }
}

class UserHandler : IUserHandler {
    public UserHandler(IPasswordUtilities utilities) { ... }

    public void AddUser(User user) {
        string hashedPassword = _utilities.HashPassword(user.Password.HashedPassword);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将优雅地解决您的问题,因为您:

  • 通过将部分逻辑提取到更小、更集中的类中来消除循环依赖
  • 通过修复 SRP 和 ISP 违规,您可以使代码库更易于维护。

最终图表将如下所示:

new AuthenticationHandler(
    new UserHandler(
        new PasswordUtilities()),
    new PasswordUtilities());
Run Code Online (Sandbox Code Playgroud)