dongyu4908 2015-08-30 15:40
浏览 24
已采纳

使用值对象的多个相关值?

I'm building a rating system in which an object are rated on different parameters, each in a scale ranging from 1-5.

It's for a side project, so I'm trying to use the project to train other code-related skills, especially TDD and possibly DDD, but I'm a bit lost on how it will make sense to do the architecture.

The base structure consists of a User (we all like those guys), and I have an Thing (the thing we're rating). I User can rate multiple Things, and a Thing can be rated by multiple Users, so we and a ThingRating handling with a many-to-many relationship to User and Thing.

The ThingRating should consist of ratings of 4 different parameters, and it's the 4 parameters I'm not really sure where to put.

The easy option seems to add them as 4 properties on the ThingRating object, param1-4. Since the rating is a value ranging from 1-5, this is fine, but I'd have to validate the input in ThingRating since I can't typehint on int (or float, depending on the granularity I decide on), so I'm considering using a value object for the rating.

That means I'd either need a value object holding the rating of 1 parameter, or a value object consisting of all 4 parameters. Having the object hold all 4 parameters seems like it would basically be a mirror of the ThingRating, and it seems like it'd take out most of the functionality out of ThingRating (getSum, getAverage etc.), but having functionality in value objects makes them not value objects (as far as I've understood), which would mean I have 2 different business objects both containing part of the rating functionality, this seems like bad engineering.

Another possibility is having a value object with the rating of just 1 parameter. But then I think I'll need a property to know which parameter a value object holds the rating for, which would then mean either a string with the parameter name, or another value object.

I'm really confused which way to go, or if I'm going off in the wrong direction, or simply just overengineering. It's my first journey into DDD, which means that it's also very likely that I'm just mixing up the terms and concepts, so pointers for where and what to focus my conceptual research on, is as appreciated as pointers directly related to my problem.

  • 写回答

1条回答 默认 最新

  • douhuang2673 2015-09-03 15:48
    关注

    From what I understand ThingRating would be an aggregate root with judgeId, thingId, and param1-4 (carefully named). The param properties would be of type RatingValue which would be a value object enforcing the 1-5 rating. A database unique constraint would be used to prevent the same user from rating multiple times. Also, "but having functionality in value objects makes them not value objects (as far as I've understood)" is not true. VO's are immutable but can still have behavior.

    "Ok, so you suggest keeping the 4 parameters on the ThingRating and and use the RatingValue for constraining the input." - JarIskov

    Exactly!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证