Open webdevilopers opened 7 years ago
I also recognized @yvoyer using the term Collection
for InMemory Repositories:
If someone used Doctrine (ArrayCollections
) throughout his project it could be used as a PHP wrapper - analog to the Java (Array)List
) - and return it (like Doctrine Repositories do using default hydration):
final class OwnerCollection implements OwnerRepository, \Countable
{
/**
* @var ArrayCollection Owner[]
*/
private $owners = [];
/**
* @param OwnerId $ownerId
*
* @throws EntityNotFoundException
* @return Owner
*/
public function ownerWithId(OwnerId $ownerId)
{
if (! isset($this->owners[$ownerId->id()])) {
throw EntityNotFoundException::entityWithIdentity($ownerId);
}
return $this->owners[$ownerId->id()];
}
Analog to Java:
public List<Order> findAllMatching(Criteria someCriteria)
{
ResultSet rcds = this.db.execFindOrdersQueryWith(someCriteria.toString());
List<List<String>> results = convertToStringList(rcds);
List<Order> returnList = new ArrayList<Order>();
for(List<String> row : results)
returnList.add(this.orderFactory.createFrom(row));
return returnList;
}
When reading my first DDD Java examples I saw a lot of
List
s:A
List
is an interface likeSet
and hasArrayList
as stated by @iamzeroCollection
as stated by @brendanlongUnfortunately PHP does not have a true equivalent as @giorgiosironi knows:
But we have @doctrine Collections:
And alternatives like
Ardent
by @morrisonlevi:The docs say:
When using @doctrine
ArrayCollection
s you can use theindexBy
mapping to make yourCollection
indexed (ordered):But you have to be careful when using accessors as @MacDada warns:
Still it seems to be a matter of taste if you use
ArrayCollection
at all in your Domain Models:@tPl0ch warns:
What are your thoughts? Any use cases? Any comparison to the Java DDD approaches using
List
?