Skip to main content
added 474 characters in body
Source Link
Ewan
  • 84.6k
  • 5
  • 91
  • 189

It's totally fine for Entities to be in an 'invalid' state according to business rules such as 'Students must register a guardian or we cant legally enrol them'

These rules are subject to change and exceptional cases. You want to flag such cases as breaking the rules, rather than make it impossible for your program to instantiate an object of that type.

In this case your application can simply reference both AR repositories, ARs can have a sudentId/guardianId property that you can look up, or you can have the relationship in a separate service.

Now, if the 'invalidness' is not simply a business rule, but inherent to the nature of the Entity, such as, I don't know, Cats have 0-4 Legs and Cat.Speed depends on the exact number. Then Leg Should be part of the Cat AR.

If a Cat has 5 Legs then Cat.Speed will cause the program will crash, If I change the number of Legs on a cat then its Speed should automatically update or I will get the wrong output from my program etc.

Putting Cat and Legs in separate ARs would be problematic, Cat.Speed would have to call the LegRepo before returning a result, I could add a Leg without updating the Cat's speed etc.

You will have to decide which case your Student and Guardian fall into. Should you have a single AR so you can have Student.RemoveGuardian(id) and enforce not allowing the removal of the last guardian.

Or would it be more sensible to have GuardianRemovalService.RemoveGuardian(studentId,guardianId) which can also enforce the rule, but allows the possibility of another application removing the guardian directly from the GuardianRepo without following the business rule?

It's totally fine for Entities to be in an 'invalid' state according to business rules such as 'Students must register a guardian or we cant legally enrol them'

These rules are subject to change and exceptional cases. You want to flag such cases as breaking the rules, rather than make it impossible for your program to instantiate an object of that type.

In this case your application can simply reference both AR repositories, ARs can have a sudentId/guardianId property that you can look up, or you can have the relationship in a separate service.

Now, if the 'invalidness' is not simply a business rule, but inherent to the nature of the Entity, such as, I don't know, Cats have 0-4 Legs and Cat.Speed depends on the exact number. Then Leg Should be part of the Cat AR.

If a Cat has 5 Legs then Cat.Speed will cause the program will crash, If I change the number of Legs on a cat then its Speed should automatically update or I will get the wrong output from my program etc.

Putting Cat and Legs in separate ARs would be problematic, Cat.Speed would have to call the LegRepo before returning a result, I could add a Leg without updating the Cat's speed etc.

It's totally fine for Entities to be in an 'invalid' state according to business rules such as 'Students must register a guardian or we cant legally enrol them'

These rules are subject to change and exceptional cases. You want to flag such cases as breaking the rules, rather than make it impossible for your program to instantiate an object of that type.

In this case your application can simply reference both AR repositories, ARs can have a sudentId/guardianId property that you can look up, or you can have the relationship in a separate service.

Now, if the 'invalidness' is not simply a business rule, but inherent to the nature of the Entity, such as, I don't know, Cats have 0-4 Legs and Cat.Speed depends on the exact number. Then Leg Should be part of the Cat AR.

If a Cat has 5 Legs then Cat.Speed will cause the program will crash, If I change the number of Legs on a cat then its Speed should automatically update or I will get the wrong output from my program etc.

Putting Cat and Legs in separate ARs would be problematic, Cat.Speed would have to call the LegRepo before returning a result, I could add a Leg without updating the Cat's speed etc.

You will have to decide which case your Student and Guardian fall into. Should you have a single AR so you can have Student.RemoveGuardian(id) and enforce not allowing the removal of the last guardian.

Or would it be more sensible to have GuardianRemovalService.RemoveGuardian(studentId,guardianId) which can also enforce the rule, but allows the possibility of another application removing the guardian directly from the GuardianRepo without following the business rule?

Source Link
Ewan
  • 84.6k
  • 5
  • 91
  • 189

It's totally fine for Entities to be in an 'invalid' state according to business rules such as 'Students must register a guardian or we cant legally enrol them'

These rules are subject to change and exceptional cases. You want to flag such cases as breaking the rules, rather than make it impossible for your program to instantiate an object of that type.

In this case your application can simply reference both AR repositories, ARs can have a sudentId/guardianId property that you can look up, or you can have the relationship in a separate service.

Now, if the 'invalidness' is not simply a business rule, but inherent to the nature of the Entity, such as, I don't know, Cats have 0-4 Legs and Cat.Speed depends on the exact number. Then Leg Should be part of the Cat AR.

If a Cat has 5 Legs then Cat.Speed will cause the program will crash, If I change the number of Legs on a cat then its Speed should automatically update or I will get the wrong output from my program etc.

Putting Cat and Legs in separate ARs would be problematic, Cat.Speed would have to call the LegRepo before returning a result, I could add a Leg without updating the Cat's speed etc.