【发布时间】:2016-01-11 20:35:53
【问题描述】:
阅读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,然后将值发送回模型以保存。是这样的吗?
注意
我可能将模型与“存储”混淆了。我可能需要一个模型来处理业务逻辑,并且数据库/持久存储应该是一个单独的层。
【问题讨论】:
标签: model-view-controller mvvm business-logic