Reputation: 49077
I have made an aggregated class named DrivingLog. I have only included the necassary fields in the picture below.
Now, when I want to add a driving record I say addRecord(Double distance, ...)
on the DrivingLog
object. However, is the DrivingRecord an aggregate for the Condition
and Environment
class or are they only references? Condition and Enviroment has no value outside one DrivingRecord
.
Condition and Environment is static data: The user will be selecting among predefined values in a dropdown list in the JSF view. But if the DrivingRecord
is an aggregate it should have a method namedaddEnviroment()
. Should this be taking the Enviroment
class as argument?
If thats the case would the DrivingLog
have a method named addRecord(Double distance, Environment environment...)
?
And last, shouldn't Environment and Condition be "hided" by the aggregate root and never accessed outside. This make me think if the DrivingRecord really is an aggregated root as well.
Is the nesting of aggregates even allowed, bad practice, ok in some cases?
Upvotes: 2
Views: 356
Reputation: 14883
when I want to add a driving record I say addRecord(Double distance, ...)
I'd suggest addRecord(DrivingRecord record)
instead, as an API for your service.
is the DrivingRecord an aggregate for the Condition and Environment class or are they only references? Condition and Enviroment has no value outside one DrivingRecord.
I'm not sure I follow the terminology as you might mean, but I think both answers are correct - DrivingRecord is an aggregate (I suppose "pointing to a bunch of objects"), but also a reference (as any object "handel" in Java is considered a references). The point is - it's not passed around as a blob, but as 4 byte references to the heap memory if you instanciated it with "new XYZ" (or stack, if it's a primitive that is passed as an argument to a method). And it won't be garbage collected as long as it is referenced by a living object.
Condition and Environment is static data: The user will be selecting among predefined values in a dropdown list in the JSF view. But if the DrivingRecord is an aggregate it should have a method namedaddEnviroment(). Should this be taking the Enviroment class as argument?
after constructing an instance of DrivingRecord, you'll need to allow insertion of references to List.
If thats the case would the DrivingLog have a method named addRecord(Double distance, Environment environment...)?
I don't know if the parameters are related to each other, but it they can be added seperately, I'd suggest something like that:
class DrivingRecord {
private ArrayList<Enviroment> env;
private ArrayList<Condition> cond;
private Double distance;
addEnvironment(Enviroment e) { env.add(e); }
addCondition(Condition c) { cond.add(c); }
setDistance(Double d) { distance=d;}
}
And last, shouldn't Environment and Condition be "hided" by the aggregate root and never accessed outside. This make me think if the DrivingRecord really is an aggregated root as well.
sure
Is the nesting of aggregates even allowed, bad practice, ok in some cases?
Knowing very little about your project goals I can't tell if there's better architecture. But it's certainly not a bad practice.
Upvotes: 1