| 1 |
Declaring DTOs on app layers |
| 2 |
----------------------------- |
| 3 |
|
| 4 |
So far we've seen DTO declared per handler. Let's have a look at a script that declares multiple handlers - something |
| 5 |
more typical of a real application. |
| 6 |
|
| 7 |
.. literalinclude:: /examples/data_transfer_objects/factory/tutorial/multiple_handlers.py |
| 8 |
:language: python |
| 9 |
:linenos: |
| 10 |
|
| 11 |
DTOs can be defined on any :ref:`layer <layered-architecture>` of the application which gives us a chance to tidy up our |
| 12 |
code a bit. Let's move the handlers into a controller and define the DTOs there. |
| 13 |
|
| 14 |
.. literalinclude:: /examples/data_transfer_objects/factory/tutorial/controller.py |
| 15 |
:language: python |
| 16 |
:linenos: |
| 17 |
:emphasize-lines: 30,31,44 |
| 18 |
|
| 19 |
The previous script had separate handler functions for each route, whereas the new script organizes these into a |
| 20 |
``PersonController`` class, allowing us to move common configuration to the controller layer. |
| 21 |
|
| 22 |
We have defined both ``dto=WriteDTO`` and ``return_dto=ReadDTO`` on the ``PersonController`` class, removing the need |
| 23 |
to define these on each handler. We still define ``PatchDTO`` directly on the ``patch_person`` handler, to override the |
| 24 |
controller level ``dto`` setting for that handler. |