- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
注册表:
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 时,它会注入(inject) AuthenticationHandler 实现,然后尝试创建 UserHandler 实例,然后循环开始...
我的问题是如何在(SIMPLE INJECTOR)这种情况下以及其他需要像这样注入(inject)的情况下解决这个问题?
谢谢!
更新:
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());
}
基本上我需要调用 AuthenticationHandler 中的 UserHandler 来获取用户并验证是否存在用户。
我需要调用 UserHandler 中的 AuthenticationHandler 来获取对密码进行 salt 和散列处理的函数。
我想我可以调用存储库来获取用户,但我不应该通过服务处理程序以防在用户服务中完成更多工作
最佳答案
循环依赖往往是由SOLID引起的违反原则,因为具有太广泛接口(interface)和太多功能的类更有可能需要彼此的功能。
我相信您的情况也是如此,因为 UserHandler.AddUser
功能取决于 AuthenticationHandler.GenerateRandomSalt
和 HashPassword
功能,虽然它与 AuthenticationHandler
的功能不同(即 Authenticate
),但它依赖于 UserHandler
的另一个功能。这强烈表明 IAuthenticationHandler
抽象实际上违反了 Interface Segregation Principle其实现违反了 Single Responsibility Principle .
解决方案是将 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);
}
}
这将优雅地解决您的问题,因为您:
最终图表将如下所示:
new AuthenticationHandler(
new UserHandler(
new PasswordUtilities()),
new PasswordUtilities());
关于c# - 简易喷油器 : Cyclic Graph Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41071590/
我正在设计一个可在hbase上运行的应用程序,并且希望以交互方式浏览集群的内容。我在hbase shell中,我想对所有以chars“abc”开头的键进行扫描。这样的键可能包括“abc4”,“abc9
我是一名优秀的程序员,十分优秀!