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!

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器