在控制器中调用多个方法还是在服务中将所有调用都包括在一个方法中?

huangapple go评论72阅读模式
英文:

Invoking multiple methods in controller or cover all calls in one method in service?

问题

我知道两种方式都可以,但我不确定哪种方式更清晰和灵活。

有一个 OrderService,它有以下方法:

  • searchOrders
  • setBookableFlag
  • mappingWithCarrier

方法 1: 在控制器中调用所有方法

OrdersController:

public OrderCarrierDTO searchOrders(@PathVariable carrierGuid){
   List<Order> orders = ordersService.searchOrders();
   ordersService.setBookableFlag(orders, carrierGuid);
   return orderService.mappingWithCarrier(orders, carrierGuid);
}

方法 2: 在 Service 中创建一个新方法,将所有调用放在该方法中,然后从控制器调用该新方法:

OrdersController:

public OrderCarrierDTO searchOrders(@PathVariable carrierGuid){
   return orderService.searchOrdersForCarrier(carrierGuid);
}
英文:

I know both ways would work, but I'm not sure which way would be more clean and flexible.

There is OrderService and it has the following methods:

  • searchOrders
  • setBookableFlag
  • mappingWithCarrier

Method 1: Invoking all the methods in controller

OrdersController:

public OrderCarrierDTO searchOrders(@PathVariable carrierGuid){
   List&lt;Order&gt; orders = ordersService.searchOrders();
   ordersService.setBookableFlag(orders, carrierGuid);
   return orderService.mappingWithCarrier(orders, carrierGuid);
}

Method 2: Create a new method in Service and put all the callings in the method and invoke that new method from Controller:

OrdersController:

public OrderCarrierDTO searchOrders(@PathVariable carrierGuid){
   return orderService.searchOrdersForCarrier(carrierGuid);
}

答案1

得分: 1

我建议使用方法2。除此之外,你最好将其他三种方法都设为私有,不对你的服务类用户开放。

这意味着控制器不需要关心你在服务内部调用了3个方法还是30个方法,它只需要知道如果我调用了这个方法,就会得到输出。

英文:

I would suggest to use Method 2, Along with that you should ideally make all other three methods private and not accessible to the users of your service class.

That means the controller should not care if you call 3 or 30 methods inside your service, it should just know that if I call this method then it will get the output.

答案2

得分: 1

因为将所有业务逻辑保留在服务层并将控制器层中的代码最小化是一种良好的实践,所以我推荐采用第二种方法。假设获取任何承运人的订单是一个频繁的任务,因此在服务中维护一个单独的方法(searchOrdersForCarrier())来处理此类任务会更方便,而不是每次都调用多个方法。如果searchOrders() setBookableFlag() mappingWithCarrier()也具有可重用的特性,我们可以将它们维护为单独的方法,并在searchOrdersForCarrier()中使用它们。

英文:

Since it's a good practice to maintain all the business logic in service and minimum code at controller layer, I would recommend the second approach. Assuming getting Orders for any Carrier is a frequent task, so maintaining a separate method (searchOrdersForCarrier()) in service for such tasks comes handy instead of calling multiple methods each time. if searchOrders() setBookableFlag() mappingWithCarrier() are also of reusable nature we can maintain them as separate methods and use them in searchOrdersForCarrier().

答案3

得分: 1

我更喜欢方法2
控制器只应处理输入/输出。因此,控制器的工作是接收 carrierGuid,然后将其传递给服务,然后服务执行其操作。这个操作的结果(或异常)返回给控制器,然后控制器返回适当的HTTP代码/内容。

英文:

I prefer method 2
The controller should only be handling input / output. So the job of the controller is receive the carrierGuid and pass that to the service which then does its thing. The result of this (or an exception) comes back to the controller and then the controller returns the proper http code / body for it

huangapple
  • 本文由 发表于 2020年4月10日 13:09:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/61134372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定