聚合根或应用程序服务中的用户权限检查?
User permission check in Aggregate Root or Application Service?
在我的项目中,有一个用户A发送一个FriendRequest
给用户B的概念,简化版的请求是这样的:
class FriendRequest
{
long Id;
int UserId;
int OtherUserId;
string Message;
}
在Accept
方法中,我需要检查当前认证用户是否等于FriendRequest
中的OtherUserId
。 currentAuthenticatedUserId
从控制器向下传递到应用程序服务。现在,问题来了,我应该在应用程序服务中还是在 FriendRequest
聚合根中进行检查。
//In application service code:
if(currentAuthenticatedUserId !=friendRequest.OtherUserId)
{
throw new FriendRequestException("Can only accept friend requests sent to you");
}
friendRequest.Accept();
对比
//In application service, but we are not checking it here.
friendRequest.Accept(currentAuthenticatedUserId); //The check is done inside `FriendRequest` and the exception is also thrown there.
friendRequest.Accept(...)
在域名术语中它是什么意思?请求接受自己或它接受什么?我相信,您需要使用更多与名词相关的动词来扩展您的通用语言。
举个例子,我可能会想到"a person can accept a friend request that was sent by another person"。在这种情况下,您将得到 person.Accept(friendRequest)
。然后服务负责根据当前身份验证详细信息获取 Person
。
访问控制是应用服务的主要职责之一。
所以在应用服务中检查用户ID,而不是在实体中。
在我的项目中,有一个用户A发送一个FriendRequest
给用户B的概念,简化版的请求是这样的:
class FriendRequest
{
long Id;
int UserId;
int OtherUserId;
string Message;
}
在Accept
方法中,我需要检查当前认证用户是否等于FriendRequest
中的OtherUserId
。 currentAuthenticatedUserId
从控制器向下传递到应用程序服务。现在,问题来了,我应该在应用程序服务中还是在 FriendRequest
聚合根中进行检查。
//In application service code:
if(currentAuthenticatedUserId !=friendRequest.OtherUserId)
{
throw new FriendRequestException("Can only accept friend requests sent to you");
}
friendRequest.Accept();
对比
//In application service, but we are not checking it here.
friendRequest.Accept(currentAuthenticatedUserId); //The check is done inside `FriendRequest` and the exception is also thrown there.
friendRequest.Accept(...)
在域名术语中它是什么意思?请求接受自己或它接受什么?我相信,您需要使用更多与名词相关的动词来扩展您的通用语言。
举个例子,我可能会想到"a person can accept a friend request that was sent by another person"。在这种情况下,您将得到 person.Accept(friendRequest)
。然后服务负责根据当前身份验证详细信息获取 Person
。
访问控制是应用服务的主要职责之一。
所以在应用服务中检查用户ID,而不是在实体中。