清洁架构当前的用户获取 [英] Clean architecture current user acquisition

查看:64
本文介绍了清洁架构当前的用户获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Clean Architecture用例中,应了解身份验证/授权过程,并且命令/查询层应使用某种框架对在下面的层中实现的身份验证服务进行某种抽象,否则应将用户标识符作为命令/的一部分传递查询?

Should in Clean Architecture use cases be aware of the authentication/authorization process and command/query layer should have some abstraction of the authentication service implemented in layer below using some framework or user identifier should be passed as the part of the command/query?

选项:

选项1:

class ChangePasswordCommand {

    char[] newPassword;
}

class ChangePasswordCommandHandler {

    AuthService authService;

    void handle(ChangePasswordCommand command) {
        User currentUser = authService.currentUser();
        // logic
    }
}

选项2:

class ChangePasswordCommand {

    UUID userId;
    char[] newPassword;
}

class ChangePasswordCommandHandler {

    UserRepository userRepository;

    void handle(ChangePasswordCommand command) {
         User currentUser = userRepository.findById(command.getUserId());
         // logic
    }
}

推荐答案

在谈论用例时,这是应用程序的领域. 因此,要回答这个问题,我可以说:这取决于此域.

When you talk about use cases, this is the domain of your application. So, to answer the question, I can say: it depends on this domain.

如果您的域名是关于用户管理"的, (例如:LDAP应用程序),是的!您需要在您的域中实现它.

If your domain is about the "users management" (example : LDAP application), yes! you need to implement it in your domain.

否则,这只是一个技术问题,您可以在域外进行管理(并直接调用存储库).

Otherwise, it's just a technical problem and you can manage it outside the domain (and call the repository directly).

示例:

让我们假设您有一些第三方身份验证提供程序,其提供的用户结构具有以下字段:ID,电子邮件地址等.

Let's assume that you have some third party authentication provider providing user structure that has the fllowing fields : id, email adddress, etc.

要仅对没有业务规则的用户进行身份验证,可以从应用程序层调用放置在基础结构层中的身份验证器.

To just authenticate user without business rules, you can call authenticator placed in infrastruture layer from application layer.

    userApplication.authenticate(String login, String password) {
        userProvider.authenticate(login, password);
    }

另一方面,如果要在验证用户身份时应用一些业务规则,则必须从应用程序层调用用户域API(位于域层中的接口).

In the other hand, if you want to apply some business rules while authenticating user, you must call user domain API (interface placed in the domain layer) from application layer.

以下代码仅是示例.你必须清理它.

The following code is just an example. You have to clean it.

应用层:

    userApplication.authenticate(String login, String password) {
        userAPI.authenticate(login, password);
    }

域层:

    class Member {
        ...
    }

    interface UserAPI {
        Member authenticate(login, password);
    }

*您可以通过创建一些工厂或服务或所需的设备来实现此接口.假设您有一个域UserService.

*you can implement this interface by creating some factory or service or wathever you want. Let's say you have a Domain UserService.

    class UserService implements UserAPI {
        
        UserSPI userSPI;

        Member authenticate(login, password) {
            // apply some business rules
            userSPI.authenticate(login, password);
        }
    }

    interface UserSPI {
        Member authenticate(login, password);
    }

基础设施层:

    class MemberDTO {
        ...
    }

    class userProvider implements UserSPI {

        private final RemoteProvider remoteProvider;

        Member authenticate(login, password) {
            MemberDTO memberDTO = remoteProvider.authenticate(login, password);
            return memberMapper.dtoToDomain(memberDTO);
        }
    }   

这篇关于清洁架构当前的用户获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆