2013-03-27 18 views
2

在我的应用程序中有客户和信使。只有当信使目前在线且两位用户来自同一地点时,客户才可以将信息发送给信使。我是否正确使用我的服务与我的实体和映射器。我的商业逻辑是否正确?

当客户要发送递送请求给信使时,我的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的物品,它们的数据(idquantity)将在从Controller通过$request对象,所以我将创建在Service每个Item,并把它们在阵列中,并通过与所述阵列$customer$couriersendRequest()方法。这意味着我必须在该方法内执行检查(检查输入数量是否不超过数量等数据库值),是正确的还是不好?

我正在做我的检查/验证正确,并在我的应用程序正确的地方/层?

任何帮助将非常感谢。

回答

0

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

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

  • 当您创建

    1. Item对象,您可以检查项目类中对象的有效性或使用观察者设计模式。 (困难和没有必要的,有趣的,如果你想自动保存对象)

    2. 使用一个验证类,将做的工作(这将是很好过)

    这将是一件好事,如果您的服务代码仅涉及服务利益的代码。

    但是你所做的是正确的我会说和有效的,你应该只进一步