我有一个域类设计问题,涉及以下域类的验证:
Class Course {
String name // computers,maths,economics,zoology etc...
}
class Component{
String name //ex: C1,C2,C3
boolean type // 0 means internal , 1 means external
}
Class CourseComponent{
Course course
Component component
Integer MaxMarks
...
}
在为特定的
CourseComponent
创建Course
时,有客户要求。总maxMarks
应该等于100。所以例如:对于数学类(class),组件分布如
1,数学C1 15
2.数学C2 15
3,数学C3 70
总分数应等于100。
我将如何编写检查多个记录的约束,或者如何更改域类?
最佳答案
如果我正确理解了要求,那么CourseComponent应该包含Component的集合。
在这种情况下,我不会将maxMarks属性添加到CourseComponent类,因为它可以轻松计算并可以通过访问器方法使用。因此,我将此属性移到Component类,以存储给定类(class)(或CourseComponent)的单个Component的数量。
接下来,当将组件实例添加到CourseComponent时,我将在总maxMark上添加一个验证。
但是另一个问题是,您真的需要一个CourseComponent类吗?
您可能有一个Course类,将相关的Component实例保存为集合属性。
关于grails - 设计领域模态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441927/