0

I am implementing a school related app for the local government. I have classes and I want to count grade using the following formula: grade = acedemicYear - commencingYear + 1.

You can model this 2 ways:

  • both commencingYear and academicYear have a Year type and the acedemicYear year is some kind of singleton, so AcademicYear.getInstance() - this.commencingYear + 1
  • another solution that commencingYear has a type of AcademicYear, and I have a Calendar class, which gives me the current year, so Calendar.getAcademicYear() - this.commencingYear + 1

Still I don't feel these right. I am not sure whether I should inject the year into the model or it should be inside the model. Another problem that the academic year should be changed more or less manually, at least it starts every year on a different date. By increasing the academic year the grade > 8 means that the class is finished, so children from that class should not be on the current student list. What do you think, what is the best way to model this?

inf3rno
  • 24,976
  • 11
  • 115
  • 197

1 Answers1

1

AcademicYear can of course be a value type like you've done. But isn't that over complicating things?

If you have an entity type which contains both commencingYear and academicYear you can easily control the value for those fields. Thus if someone tries to enter a date which is out of bounds you can just thrown an exception.

Regarding the calculation it sounds like business rules and therefore it should be wrapped within a method in the entity or in a domain service.

i.e. it's a rule, but a rule for a specific entity in the domain. thus it should not be wrapped in a static somewhere but implemented in the correct entity class.

Writing a more specific answer is hard as I have now knowledge about your domain or how you have implemented it.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thanks! So if I understand well I could create a domain service `currentAcademicYear = CalendarService.getAcademicYear()` and finish the year with `CalendarService.finish(currentAcademicYear)`. Or I could create an entity from it `currentAcademicYear = AcademicYearRepo.getCurrentAcademicYear()` and `currentAcademicYear.finish()`. Or I could use the Date type with the service, which I think is complicated, since the academic year starts somewhere in the early September, but in a different day every year. I think it is the best to have an AcademicYear entity in my model. – inf3rno Feb 12 '17 at 09:50
  • I do not understand your model or the rule, so it's really hard to say. All I can say is that you should protect the business rule in some way. So if it changes in in the future there is only one place to modify. – jgauffin Feb 12 '17 at 10:51
  • I know, but I don't want to talk too much about it. I think an entity is much better than using a domain service or just a date type in this model. You helped a lot, thanks! – inf3rno Feb 12 '17 at 11:59