看起來你有兩種選擇:
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