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 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab