admin管理员组文章数量:1279085
I'm designing an application using hexagonal architecture, and I need to retrieve the current user for authorization purposes. In my application layer, I have a command to delete an item, and before deleting, I need to ensure that the current user is allowed to perform the deletion.
Below is a simplified version of my command and its handler:
data class DeleteTodoCommand(val todoId: TodoId) : Command<Unit>
internal class DeleteTodoCommandHandler(
private val todoRepository: TodoRepository,
) : CommandHandler<DeleteTodoCommand, Unit> {
override fun handle(command: DeleteTodoCommand) {
val todo = todoRepository.findById(command.todoId) ?: throw SomeException()
// TODO: Retrieve the current user and perform authorization checks before deletion
todoRepository.delete(todo)
}
}
I've considered two potential approaches:
Retrieve the user in the infrastructure layer and pass it to the application layer:
This would involve handling user resolution (e.g., from HTTP headers or CLI context) directly within each adapter. However, this could lead to duplicating the user retrieval logic across different adapters (REST, CLI, etc.).Define a port in the application layer (e.g.,
CurrentUserResolver
) implemented by an adapter in the infrastructure layer:
With this approach, the application layer defines an interface for retrieving the current user. The infrastructure layer then provides the concrete implementation (e.g., via a JWT adapter or another mechanism). This decouples the user resolution logic from the adapter details and promotes a more modular design.My question is: Is it a good practice to treat current user retrieval as a port in hexagonal architecture?
Any insights would be greatly appreciated.
本文标签: domain driven designHow should I retrieve the current user in a hexagonal architectureStack Overflow
版权声明:本文标题:domain driven design - How should I retrieve the current user in a hexagonal architecture? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741270189a2369138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论