2

在我的应用程序中有客户和快递员。只有当 Courier 当前在线并且两个用户来自同一位置时,客户才能向 Courier 发送交付请求。

当客户想要向 Courier 发送交付请求时,我的DeliveryRequest服务有一个sendDeliveryRequest(Request request)Controller.

public function sendDeliveryRequest(Request $request) {

    $customer = $this->recognitionService->getUser();

    $courier = $this->entityFactory->build('Courier');
    $courier->setId( $request->post('courierId') );
    $courierMapper = $this->mapperFactory->build('Courier');
    $courierMapper->fetch($courier);

    $deliveryRequest = $this->entityFactory->build('DeliveryRequest');

    $someRequestedItems = array();
    $deliveryRequest->sendRequest($customer, $courier, $someRequestedItems);

}

到目前为止,在我的sendRequest(Customer $customer, Courier $courier, Array $items)方法中,我有:

public function sendRequest(Customer $customer, Courier $courier, Array $items) {

    // Check if the couriers account is active
    if( !$courier->isActive() ) {
        return 'courier not active';
    }
    // Check if the courier is online
    if( !$courier->isOnline() ) {
        return 'courier not online';
    }
    // Check the status of the customers location, active/inactive
    if( !$customer->getLocation()->isActive() ) {
        return 'customers location disabled';
    }
    // Check if the customer and the courier live in the same location
    if( !$customer->sameLocationAs($courier) ) {
        return 'customer and courier in different locations';
    }
    // More checks

}

到目前为止,对我来说,它看起来不错并且运行良好,但我不能 100% 确定我是否正确地执行了业务逻辑,尤其是!$customer->sameLocationAs($courier).

该方法使用提供的$courier对象来获取 Couriers 位置(这是一个带有 的对象id)并将其与客户位置进行比较以检查它们是否在同一位置。它工作得很好,但我不确定这是否是完成检查两个用户是否来自同一位置的最佳方法。这是有效的业务逻辑吗?

此外, 中的项目$deliveryRequest,它们的数据(id, quantity)将在从$request传递的对象中,所以我将在 中Controller创建每个并将它们放入数组中,然后将数组与 和 传递给方法。这意味着我必须在该方法中进行检查(检查输入的数量是否不超过数量的数据库值等),这是正确的方法还是不好?ItemService$customer$couriersendRequest()

我是否在我的应用程序的正确位置/层正确地进行检查/验证?

任何帮助都会非常感谢。

4

1 回答 1

0

对于第一个问题,我会说您的方法“sameLocationAs”是有效的。另一种可能性是创建一个具有静态方法并且可以在类之间提供服务的 Util 类。

这是模型检查项目是否有效(不是控制器)的工作,因此您有两种可能性:

  1. 就像你想做的一样

  2. 当您创建 Item 对象时,您可以在 item 类或使用观察者设计模式中检查对象的有效性。(很难而且不需要,如果你想自动保存对象很有趣)

  3. 使用可以完成这项工作的验证类(这也很好)

如果您的服务代码仅涉及服务利益,那将是很好的。

但是您所做的是正确的,我会说并且有效,您应该走得更远一点

于 2013-03-28T17:28:31.817 回答