Last active
February 20, 2019 11:11
-
-
Save webdevilopers/cb4352a591437e57785bec3f8dc3bcea to your computer and use it in GitHub Desktop.
DDD Domain Service vs Domain Factory vs. Double Dispatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DormerOffer | |
{ | |
/** | |
* DormerOffer constructor. | |
* @param OfferId $anId | |
* @param OfferTitle $aTitle | |
* @param DormerCalculation $aDormerCalculation | |
* @param $chargeRates | |
* @param PricesCalculator $pricesCalculator | |
*/ | |
public function __construct( | |
OfferId $anId, OfferTitle $aTitle, DormerCalculation $aDormerCalculation, | |
$chargeRates, PricesCalculator $pricesCalculator | |
) | |
{ | |
$this->id = $anId; | |
$this->dormerCalculationId = $aDormerCalculation->calculationId(); | |
$this->title = $aTitle; | |
$this->chargeRates = $chargeRates; | |
// Calculate prices | |
$prices = $pricesCalculator->calculate($aDormerCalculation, $chargeRates); | |
$this->setPrices($prices); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DormerOffer | |
{ | |
/** | |
* DormerOffer constructor. | |
* @param OfferId $anId | |
* @param OfferTitle $aTitle | |
* @param DormerCalculation $aDormerCalculation | |
* @param $chargeRates | |
* @param $prices | |
* @param PricesCalculator $pricesCalculator | |
*/ | |
public function __construct( | |
OfferId $anId, OfferTitle $aTitle, DormerCalculation $aDormerCalculation, | |
$chargeRates, $prices | |
) | |
{ | |
$this->id = $anId; | |
$this->dormerCalculationId = $aDormerCalculation->calculationId(); | |
$this->title = $aTitle; | |
$this->chargeRates = $chargeRates; | |
$this->setPrices($prices); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class OfferFactory | |
{ | |
/** @var ChargeRatesFactory $chargeRatesFactory */ | |
private $chargeRatesFactory; | |
/** @var PricesCalculator $pricesCalculator */ | |
private $pricesCalculator; | |
/** | |
* OfferPriceCalculator constructor. | |
* @param ChargeRatesFactory $chargeRatesFactory | |
* @param PricesCalculator $pricesCalculator | |
*/ | |
public function __construct(ChargeRatesFactory $chargeRatesFactory, PricesCalculator $pricesCalculator) | |
{ | |
$this->chargeRatesFactory = $chargeRatesFactory; | |
$this->pricesCalculator = $pricesCalculator; | |
} | |
/** | |
* @param OfferId $offerId | |
* @param DormerCalculation $calculation | |
* @return DormerOffer | |
*/ | |
public function withDormerCalculation(OfferId $offerId, DormerCalculation $calculation): DormerOffer | |
{ | |
$chargeRates = $this->chargeRatesFactory->init($calculation); | |
return new DormerOffer( | |
$offerId, | |
OfferTitle::fromDormerCalculation($calculation), | |
$calculation, $chargeRates, $this->pricesCalculator | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
final class OfferFactory | |
{ | |
/** @var ChargeRatesFactory $chargeRatesFactory */ | |
private $chargeRatesFactory; | |
/** @var PricesCalculator $pricesCalculator */ | |
private $pricesCalculator; | |
/** | |
* OfferPriceCalculator constructor. | |
* @param ChargeRatesFactory $chargeRatesFactory | |
* @param PricesCalculator $pricesCalculator | |
*/ | |
public function __construct(ChargeRatesFactory $chargeRatesFactory, PricesCalculator $pricesCalculator) | |
{ | |
$this->chargeRatesFactory = $chargeRatesFactory; | |
$this->pricesCalculator = $pricesCalculator; | |
} | |
/** | |
* @param OfferId $offerId | |
* @param DormerCalculation $calculation | |
* @return DormerOffer | |
*/ | |
public function withDormerCalculation(OfferId $offerId, DormerCalculation $calculation): DormerOffer | |
{ | |
$chargeRates = $this->chargeRatesFactory->init($calculation); | |
$prices = $this->pricesCalculator->calculate($calculation, $chargeRates); | |
return new DormerOffer( | |
$offerId, | |
OfferTitle::fromDormerCalculation($calculation), | |
$calculation, $chargeRates, $prices | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The constructor reveives a domain service
pricesCalculator
which needs to make some complex calculations. But no calls to the external world e.g. a repository. But it requires theDormerCalculation
Entity.This entity itself is only required for the calculation and is NOT added to the aggregate root
DormerOffer
.