Persisting a Collection of Value Objects

Imagine that we'd now like to add a collection of prices to be persisted to our Product Entity. These prices could represent the different prices the product has borne throughout its lifetime or the product price in different currencies. This could be named HistoricalPrice, as shown below:

class HistoricalProduct extends Product 
{
/**
* @var Money[]
*/
protected $prices;

public function __construct(
$aProductId,
$aName,
Money $aPrice,
array $somePrices
){
parent::__construct($aProductId, $aName, $aPrice);
$this->setPrices($somePrices);
}

private function setPrices(array $somePrices)
{
$this->prices = $somePrices;
}

public function prices()
{
return $this->prices;
}
}

HistoricalProduct extends from Product, so it inherits the same behavior, plus the price collection functionality.

As in the previous sections, serialization is a plausible approach if you don't care about querying capabilities. However, Embedded Values are a possibility if we know exactly how many prices we want to persist. But what happens if we want to persist an undetermined collection of historical prices?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.137.240