I always have the same question when I must to work with nested objects structure. An example:
class Criteria {
filters: Filter[];
}
class Filter {
field: string;
operator: Operator;
value: string;
}
class Operator {
operator: '=' | '!=';
}
I try to apply Demeter's Law at return methods on that cases, but what's the best practice on creation step with that structures?
- Create all objects from the client
class Client {
constructor() {
const criteria = new Criteria([new Filter('field1', new Operator('='), 'value1')]);
}
}
- Create all objects from the root object (Criteria) passing only primitives from the client to root class.
class Client {
constructor() {
const criteria = new Criteria([{field: 'field1', operator: '=', 'value': 'value1'}]);
}
}