【发布时间】:2014-10-03 10:47:57
【问题描述】:
我想以 DDD 的方式编写我的第一个应用程序(电子商务),我想知道我是否一切正常,所以我希望您对我的建模提出意见 - 如何改进它,应该如何改进我正在寻找等。每一个反馈将不胜感激。
我已将我的应用程序划分为几个有界上下文(目录、购物、订购),下面的示例基于购物上下文。我也在尝试使用 CQRS(因此是 AddProductToCart 命令)。
我的业务需求如下:
- 客户应该能够将产品添加到他的购物车
- 添加产品的价格应针对用户,并基于以下几个因素:(全球折扣、用户折扣和客户所在国家/地区(某些国家/地区降低了基本价格)
我认识了以下参与者(请注意方法和类的 cmets):
/**
* A customer (Aggregate Root) having an unique cart
*/
class Customer
{
/**
* @var int
*/
private $customerId;
/**
* @var string
*/
private $country;
/**
* @var Cart
*/
private $cart;
/**
* @var PriceProvider
*/
private $priceProvider;
/**
* Adds product to customers cart with user-specific price
*
* @param $productId
* @return CartLine
*/
public function addProductToCart($productId)
{
$price = $this->priceProvider->priceForProduct($productId, $this->customerId, $this->country);
return $this->cart->addLine($productId, $price);
}
}
/**
* Simple CartLine object for persisting purposes
*/
class CartLine
{
public $productId;
public $price;
public $cartId;
function __construct($cartId, $productId, $price)
{
$this->cartId = $cartId;
$this->price = $price;
$this->productId = $productId;
}
}
class Cart
{
private $cartId;
public function addLine($productId, $price)
{
return new CartLine($this->cartId, $productId, $price);
}
}
/**
* Provides price for specific country
*/
class PriceProvider
{
public function priceForProduct($productId, $userId, $country)
{
// Logic for determining product price for customer
// Based on available global discounts, user discounts and country
}
}
/**
* Command for adding product to cart
*/
class AddProductToCart
{
public $customerId;
public $productId;
}
/**
* An application service to bind everything together
*/
class CustomerService
{
public function addProductToCart(AddProductToCart $command)
{
/** @var Customer $customer */
$customer = $this->customerRepository->customerOfId($command->customerId);
$cartLine = $customer->addProductToCart($command->productId);
$this->cartLineRepository->save($cartLine);
}
}
这是正确的方法吗?我在这里违反了任何 DDD 原则吗?我可以改进吗?
【问题讨论】:
-
你也需要一个事件:AddProductToCartCommand-->ProductAddedToCartEvent
-
只是出于好奇,这是打算作为学习应用还是生产应用?
-
@Sudarshan,它可能最终会成为小型生产设备。 ;)
-
你为什么要问?我应该改变什么吗?
标签: oop design-patterns domain-driven-design modeling cqrs