Designing the domain modal class

57 Views Asked by At

I have one domain class design issue regarding validation for the following domain classes:

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
  ...
}

There client requirement is when creating CourseComponents for the particular Course .. total maxMarks should be equals to hundred. So for instance: for Maths Course,component distribution like

1.Maths C1 15

2.Maths C2 15

3.Maths C3 70

The total marks should be equals 100.

How would I go about writing a constraint that examines multiple records or how do I need to change my domain classes?

1

There are 1 best solutions below

2
On

If I correctly understood the requirement then CourseComponent should hold a collection of Component. In this case I would not add a maxMarks attribute to the CourseComponent class since it may be computed easily and available through an accessor method. Therefore I would move this attribute to the Component class for storing the number of an individual Component for a given Course (or CourseComponent). Next I would add a validation on the total maxMarks when adding Component instances to the CourseComponent. But another question is do you really need a CourseComponent class? You might have a Course class that holds related Component instances as a collection attribute.