在我的公司,我们有一个非常具体的定价策略:
Product都有一个baseUsdPrice,例如,产品Foo的基本美元价格为9.99美元。我应该如何以DDD方式设计我的Product聚合根,使其干净、有凝聚力、解耦和易于更改?
paymentPrice是否应该由域服务及其结果作为Product聚合的一部分来计算?如果是这样的话,这意味着我的ProductRepository将有像product(productId, countryId, currency)这样的方法PaymentPriceCalculator中,并使用像getPaymentPrice(Country, Currency)这样的访问者模式吗?如果我需要使用实体中的paymentPrice来执行一些业务规则检查,该怎么办?我试图把我的头绕在它周围,我觉得我想得太多了,而且很疼。;)
发布于 2014-12-16 19:50:39
我倾向于你的第二个选择--拥有一个PaymentPriceCalculator。这样,如果您决定更改算法或使用多个算法,您将能够拥有不同的计算器。
Product总量中,因为价格因国家而异。它还会使产品类更加复杂。从一个领域的角度来看,即使是在不同的国家购买,2种相同的产品不是“平等”的吗?我可能还有第二个服务,可以将$转换为所需的任何货币。这应该是单独的服务,因为您的域名似乎使用美元的一切,直到结束时,用户需要实际支付的东西。通过这种方式,您还可以添加适用的税收/增值税等,这与按国家计算价格例外情况的逻辑不同。
发布于 2014-12-16 21:45:30
我将声明一个负责计算产品价格的对象,例如:
interface ProductPriceCalculator {
public function determineProductPrice($productId, Currency $currency = null);
}由于每个产品都有basePrice,而且我们希望修改它,所以可以这样组织:
class Product {
private $id;
/**
* Initially base price
* @var Money
*/
private $price;
public function modifyPrice(ProductPriceCalculator $calculator, Currency $currency = null) {
$newPrice = $calculator->determineProductPrice($this->id, $currency);
if ($newPrice !== null) {
$this->price = $newPrice;
}
}
}现在,在这种情况下,您需要国家作为价格调整。对我来说,有意义的解决方案是在代码中准确地反映这一点:
class Country implements ProductPriceCalculator {
private $id;
/**
* @var Currency
*/
private $currency;
/**
* Hashmap<String, Money> where product id evaluates to price in $this country
* @var array
*/
private $productPrices = array();
/**
* @param string $productId
* @param Currency $currency
* @return Money
*/
public function determineProductPrice($productId, Currency $currency = null) {
if (array_key_exists($productId, $this->productPrices)) {
$productPrice = clone $this->productPrices[$productId];
if ($currency !== null) {
$currency = $this->currency;
}
return $productPrice->convertTo($currency);
}
}
}现在要支持货币和货币逻辑:
class Money {
private $value;
private $currency;
public function __construct($amount, Currency $currency) {
$this->value = $amount;
$this->currency = $currency;
}
public function convertTo(Currency $newCurrency) {
if (!$this->currency->equalTo($newCurrency)) {
$this->value *= $newCurrency->ratioTo($this->currency);
$this->currency = $newCurrency;
}
}
}
class Currency {
private $code;
private static $conversionTable = array();
public function equalTo(Currency $currency) {
return $this->code == $currency->code;
}
public function ratioTo(Currency $currency) {
return self::$conversionTable[$this->code . '-' . $currency->code];
}
}最后,客户看起来会是这样的:
class SomeClient {
public function someAction() {
//initialize $product (it has the base price)
//initialize $selectedCountry with prices for this product
//initialize $selectedCurrency
$product->modifyPrice($selectedCountry, $selectedCurrency);
//here $product contains the price for that country
}
}发布于 2014-12-17 02:45:03
同意@dkatzel的说法,汇总不是一个进行计算的好地方。
假设产品保存了计算:
product.paymentPrice(countryId, currency, fixedPriceMatrix)要测试这一点,您需要在每个测试用例中构建一个产品,尽管有些情况只关注货币的选择。
countryId = ...
currency = ...
fixedPriceMatrix = ...
basePrice = ...
countryPrice = ...
product = new Product(id, basePrice, countryPrice...)
paymentPrice = product.paymentPrice(countryId, currency, fixedPriceMatrix)在实际项目中,聚合包含许多信息(用于不同的目的),这使得在测试中设置它们相对比较困难。
测试当前计算的容易方法是使用值对象PaymentPrice
//in unit test
paymentPrice = new PaymentPrice(basePriceForYourCountry, currency, fixedPriceMatrix)
value = paymentPrice.value()
//the product now holds countryPrice calculation
countryPrice = new Product(id, basePrice).countryPrice(countryId);您可以使用PaymentPriceCalculator作为无状态域服务作为工厂:
class PaymentPriceCalculator {
PaymentPrice paymentPrice(product, countryId, currency) {
fixedPriceMatrix = fixedPriceMatrixStore.get()
return new PaymentPrice(product.countryPrice(countryId), currency, fixedPriceMatrix())
}
}可能的变化可能是:
https://stackoverflow.com/questions/27511141
复制相似问题