mercredi 6 décembre 2017

How to use DTO in this case?

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?

Aucun commentaire:

Enregistrer un commentaire