看起来你有两种选择:
1)两个总根源。添加新的问题是这样的:
StudentDriver student = studentsRepository.findById(id);
Question question = someFactory.CreateForStudent(student);
questionsRepository.Persist(question);
并得到了学生的所有问题都将成为问题库的责任:
IList<Question> studentQuestions = questionsRepository.findByStudent(student);
从本质上讲,你必须从问题学生的持久单向关系:问题属于StudentDriver(多对一)。
2)一个聚合根。 StudentDriver是包含问题列表的聚合的根。添加新的问题是这样的:
StudentDriver student = studentsRepository.findById(id);
student.addNewQuestion("Should you stop on a red light?", "Yes");
studentsRepository.Persist(student);
要获得一个学生,你会使用类似的所有问题:
IList<Question> studentQuestions = student.GetAllQuestions();
两个选项之间的选择取决于从OP缺少信息。识别聚合根的好方法是查看生命周期逻辑。像
- 问题存在没有相应的学生?
- 问题可以从一个学生重新分配给另一个学生吗?
- 当您删除/存档学生时,是否应删除所有问题?
为什么不根据用户界面需求或外部API必须提供的内容实现需要实现的内容?您是否知道您在事务中所做的操作无论如何都不可见于其他并发事务,并且每个新事务都从头开始,并从数据库中加载所有事务。 – 2012-02-21 22:44:12