2017-12-06 10 views
1

私は、コントローラのメソッドの束を持っており、今では次のようになります。この場合のDTOの使い方は?

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')); 
    } 
} 

私は、リモート・インタフェースに多くのパラメータを転送し、私のItemDTOにcategoryIdiduserを追加避けたいです。私のItemDTOに多くのフィールドがある場合は良いアプローチですか、私は2つだけを使用しますか?このように:

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); 
} 

そして、このように私のDTO:各メソッドの

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 
    } 
} 

たぶん私は必要なの別々のDTO?また、DTOには$pageNumber,$pageSizeなどのフィールドが含まれていますか?良いアプローチですか?

答えて

1

2つのパラメータの使用を検討していただけであれば、それはパターンの例によるクエリと見なされ、かなり受け入れられるでしょう。 PageNumber & PageSizeのようなものを追加することを検討しているので、あなたはそれを吹き飛ばしたと言いたいと思います。必要なパラメータを持つ別のItemQuery DTOを検討する必要があります。

関連する問題