dtm41506 2017-12-06 09:02
浏览 248
已采纳

在这种情况下如何使用DTO?

I have a bunch of methods in my controller and now it looks like this:

class ItemController
{
     public function getItems(Request $request)
     {
         return $this->itemService->getItems($this->getUser(), $request->get('category_id'));
     }

     public function getItem(int $id, Request $request)
     {
         return $this->itemService->getItem($this->getUser(), $id, $request->get('category_id'));
     }

     public function patchItem(ItemDTO $item, Request $request)
     {
         return $this->itemService->patchItem($this->getUser(), $item, $request->get('category_id'));
     }
}

I want to avoid transfer a lot of parameters to remote interface and add categoryId, id and user to my ItemDTO. Is it good approach if I have many fields in my ItemDTO, but I use only two? Like this:

 public function getItem(int $id, Request $request)
 {
     $itemDTO = new ItemDTO($this->getUser(), $id, $request->get('category_id')); //here $title and others is empty
     return $this->itemService->getItem($itemDTO);
 }

And my DTO like this:

class ItemDTO
{
    final public $user;

    final public $id;

    final public $categoryId;

    final public $title;

    //...

    public function __construct($user = null, $id = null, $categoryId = null, $title = null, ...)
    {
       //assign variables
    }
}

Maybe I need separate DTOs for each method? Also, can DTO contains fields like $pageNumber,$pageSize and other filters? Is it good approach?

  • 写回答

1条回答 默认 最新

  • dtp0760 2017-12-06 13:40
    关注

    If you were just considering using the two parameters, I would consider it a query by example pattern, and it would be fairly acceptable. Because you are considering adding things like PageNumber & PageSize, I'd say you have blown that out, and you should now consider a separate ItemQuery DTO which has the parameters you need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?