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에 categoryId
, id
및 user
을 추가 할 수 있습니다. 내 ItemDTO에 필드가 여러 개있는 경우 좋은 접근 방법인가요?하지만 두 개만 사용합니까? 이처럼 :
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
및 기타 필터와 같은 필드가 포함될 수 있습니까? 좋은 접근인가?