A Data Transfer Object (DTO) is an object used to pass data between different layers in your application. It holds no business data, but only the minimum required data to transfer between layers or applications. A simple example may be that you have the user object with a password field and in your API response you want to hide it.
In Kanvas we use the library automapper-plus to handle the way we work with DTO. The way it works is that you have 2 folders where you manage your DTO and Mapper class
As you can see, it is a simple class where you specify the properties.
Class where you map the DTO to the entity.
Like its name suggests, a class where you map the DTO properties to the entity you want to transform.
Note: You can see we have the RelationshipTrait on top, this is to allow the model relationship to work with a DTO Object via our query string search (we will discuss in this chapter)
Now how do you handle it on the controller? Well, we try to make it as simple as possible by using our trait ProcessOutputMapperTrait. This will allow us to specify in the controller the DTO Class and the Mapper Object so our function processOutput can do the rest for us.
ProcessOutputMapperTrait will overwrite the processOutput function in your controller with
And that's it , you don't have to do anything else since all output functions call processOutput, you can expect a consistent response within the controller.
Now, if you want to do it yourself, this is how you manage it via processOutput. We recommend using customMapper from the library, since it will be much faster but you will need to write a little more code.
If you don't want to use a customMapper, you can use the library mapping methods.