gpt4 book ai didi

entity - DDD : what methods should entity contain?

转载 作者:行者123 更新时间:2023-12-02 03:10:15 24 4
gpt4 key购买 nike

我有代表用户授权的类(scala 代码):

case class Authorization(
userId: UUID,
permissions: Seq[String],
userRoles: Seq[String],
companyId: UUID
) {

private val policy = new Policy(permissions)

def isAllowed(path: String, action: String): Boolean = policy.isAllowed(path, action)

def isSuperuser: Boolean = userRoles.contains(SuperUser)

}

该类在应用程序的许多地方使用来检查用户是否对特定实体具有特定权限,例如:

authorization.isAllowed(s"some/entity/path/$id", "read")

为了提高可用性并避免直接字符串操作,我将此类方法包装在更具体的方法中:

def canViewCompany(companyId:UUID)=authorization.isAllowed(s"company/$companyId","read")

def canViewUser(userId:UUID)=authorization.isAllowed(s"user/$userId","read")

def canEditTask(id:UUID)=authorization.isAllowed(....)

....

等等,最多20种方法。Authorization 是否应该包含诸如 canViewCompany() 之类的方法?
这个类有责任了解每个特定的实体检查吗?

更新:所以最终的问题是:

  1. 创建像这样的包装器是个好主意吗canViewCompany(companyId:UUID) 为了提高可用性类(class)?
  2. 如果是这样,这些方法应该放在 Authorization 本身中还是应该放在提取到某些服务,例如:PermissionService.canViewCompany(id,authorization)=authorization.isAllowed()?

最佳答案

在 DDD 中,只有与实体相关的方法才应该位于实体类中,如 createupdate 。这种方法仅适用于实体属性和字段。据我所知,Authorization是服务而不是实体,因此最好将其定义为其他实体(例如 User)的服务。和Policy应该在其中用于执行一些操作,例如 isAllowed 。最后,您应该合并与 Authorization 相关的方法进入此服务。

关于entity - DDD : what methods should entity contain?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57809891/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com