在我的項目,有一個用戶概念的一個發送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.
這篇文章https://medium.com/p/671f7a5596ac談論DDD授權和認證。披露者:我是作者 – martinezdelariva
@ martinezdelariva謝謝。它讓我有點輕鬆。我會去檢查應用程序服務中的檢查。 – foresightyj