在閱讀https://softwareengineering.stackexchange.com/questions/165444/where-to-put-business-logic-in-mvc-design/165446#165446之後,我仍然對我想將計算折扣代碼放在哪裏感到困惑。計算產品或服務的折扣價格我認爲這絕對是業務邏輯的一部分,不屬於應用程序路由或數據庫交互。因此,我正在努力在哪裏放置我的業務邏輯。業務邏輯是否存在於模型或控制器中?
例
class Model
{
public function saveService($options)
{
$serviceId = $options['service_id'];
//Model reads "line Item" from database
$service = $this->entityManager->find('Entity\ServiceLineItem', $serviceId);
//uses the data to compute discount
//but wait.. shouldn't model do CRUD only?
//shouldn't this computation be in Controller?
$discount = ($service->getUnitPrice() * 0.25);
// Add the line item
$item = new SalesItem();
$item->setDiscount($discount);
}
}
class Controller
{
function save()
{
$this->model->saveService($options);
}
}
問:
以上$discount
計算,它應該留在型號呢,還是進入控制器?如果進入控制器,控制器必須首先調用$service
(通過模型),然後在控制器內計算$discount
,然後將其值發送回要保存的模型。這是做到這一點的方式嗎?
注意
我可能會混淆模型與「存儲」。我可能需要有一個模型來處理業務邏輯,Database/Persistent Storage應該是一個單獨的層。