Reputation: 1013
Based on Uncle Bob's Entity definition:
"The Entity is pure business and nothing else."
I want to get some clarification regarding a business logical relation between entities. Let's imagine we have 2 classes and we need to implement relation between them. I see 3 options:
There is a Client and Pet classes (Client one-to-many Animal). Animal class has Client's ID
class Client {
private _clientID: ClientID;
public getID(): ClientID {
return this._clientID;
}
}
class Animal {
private _animalID: AnimalID;
private _ownerID: ClientID;
public getID(): AnimalID {
return this._animalID;
}
public getOwnerID(): ClientID {
return this._ownerID;
}
}
In this case we know who is a Pet's owner by his ID. We can made some decisions based on ownerID existance.
Concern #1. If we need to make some Critical business decision based on related entity field, what should we do?
Same classes, but Animal class has Client dependency
class Client {
private _clientID: ClientID;
public getID(): ClientID {
return this._clientID;
}
}
class Animal {
private _animalID: AnimalID;
private _owner: Client;
public getID(): AnimalID {
return this._animalID;
}
public getOwner(): Client {
return this._owner;
}
}
In this case we know everything about owner.
Concern #2. within this approach there is possible situation when we'll get a deep entity dependency like this:
A {
B {
C {
... and so on ...
}
}
}
In fact, we'll have to initialize every entity from DB and inject their instances into other entities.
We don't have any relational IDs. We create methods like "getPetsByClientId" (or vice versa) in repositories and coordinate all things in use cases.
class Client {
private _clientID: ClientID;
public getID(): ClientID {
return this._clientID;
}
}
class Animal {
private _animalID: AnimalID;
public getID(): AnimalID {
return this._animalID;
}
}
Concern #2: I feel like we are losing a major part of business logic here. For example, we need to make some decision depending on "is there an owner of this pet?" (yeah, new condition - now we able to create Pet without actual owner), and this decision is a high-level policy, a pure business logic (again, imaginary). So its place is within an Entity.
4(extra). No panacea. Each case requires individual implementation. (<= the saddest one)
Upvotes: 2
Views: 2649
Reputation: 3573
Entities should contain and model the most important business rules. If the bidirectional dependency between two domain objects is such an important business rule it should be reflected in the entities.
This can be achieved by using IDs to link the entities or with direct object references. For that later you may have implement custom equality and comparison operations for your entities depending on support by your programming language.
Upvotes: 3