【问题标题】:Am I using my Service properly with my Entities and Mappers. Is my business logic correct?我是否正确使用我的服务与我的实体和映射器。我的业务逻辑正确吗?
【发布时间】:2013-03-18 02:20:53
【问题描述】:

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

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

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 中的项目,它们的数据(idquantity)将在从Controller 传递的$request 对象中,所以我将在@987654336 中创建每个Item @ 并将它们放入一个数组中,并将带有$customer$courier 的数组传递给sendRequest() 方法。这意味着我必须在该方法中进行检查(检查输入的数量是否不超过数量的数据库值等),这是正确的方法还是不好的方法?

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

任何帮助将非常感谢。

【问题讨论】:

    标签: php oop model-view-controller


    【解决方案1】:

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

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

    1. 如你所愿

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

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

    如果您的服务代码的代码只涉及服务利益,那就太好了。

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

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 2011-03-28
      • 2019-09-05
      • 2016-04-30
      • 2013-08-28
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      相关资源
      最近更新 更多