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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!