22
33Set of tools to simplify application logic writing.
44
5- * [ Data Transfer Object (DTO)] ( #data-transfer-object-dto )
65* [ Reading] ( #reading )
76* [ Writing] ( #writing )
87* [ Side effects] ( #side-effects )
98* [ Bus Middleware] ( #bus-middleware )
109
11- ## Data Transfer Object (DTO)
12-
13- The idea is to have one DTO to enter the application layer and another to exit when needed.
14- A DTO is a subclass of [ Pydantic BaseModel] ( https://docs.pydantic.dev/latest/ ) , so it contains all these properties.
15-
16- ``` python
17- from cq import DTO
18-
19- class MyAwesomeDTO (DTO ):
20- my_awesome_value: str
21- ```
22-
2310## Reading
2411
2512### Define a query
2613
2714The purpose of a query is to read data.
28- The ` Query ` class is a subclass of DTO.
2915
3016The ` query_handler ` decorator associates a query type with a particular logic (handler).
3117Only one handler can be associated with a query type.
3218All handler dependencies are injected at runtime using [ python-injection] ( https://github.com/100nm/python-injection ) .
3319
3420``` python
35- from cq import DTO , Query, query_handler
21+ import msgspec
22+ from cq import query_handler
3623
37- class UserProfileView ( DTO ) :
24+ class UserProfileView :
3825 """ Data to retrieve """
3926
40- class ReadUserProfileQuery (Query ):
27+ class ReadUserProfileQuery (msgspec . Struct , frozen = True ):
4128 user_id: int
4229
4330@query_handler (ReadUserProfileQuery)
@@ -69,16 +56,15 @@ async def get_user_profile_1(query_bus: QueryBus[UserProfileView]) -> UserProfil
6956### Define a command
7057
7158The purpose of a command is to write data.
72- The ` Command ` class is a subclass of DTO.
7359
7460The ` command_handler ` decorator associates a command type with a particular logic (handler).
7561Only one handler can be associated with a command type.
7662All handler dependencies are injected at runtime using [ python-injection] ( https://github.com/100nm/python-injection ) .
7763
7864``` python
79- from cq import Command, command_handler
65+ from cq import command_handler
8066
81- class UpdateUserProfileCommand ( Command ) :
67+ class UpdateUserProfileCommand :
8268 """ Data required to update user profile """
8369
8470@command_handler (UpdateUserProfileCommand)
@@ -110,16 +96,15 @@ async def update_user_profile(command_bus: CommandBus[None]) -> None:
11096
11197The purpose of an event is to execute side effects.
11298An event is generally propagated at the end of a command.
113- The ` Event ` class is a subclass of DTO.
11499
115100The ` event_handler ` decorator associates a event type with a particular logic (handler).
116101Several handlers can be associated with an event type.
117102All handler dependencies are injected at runtime using [ python-injection] ( https://github.com/100nm/python-injection ) .
118103
119104``` python
120- from cq import Event, event_handler
105+ from cq import event_handler
121106
122- class UserRegistered ( Event ) :
107+ class UserRegistered :
123108 """ Data to process the event """
124109
125110@event_handler (UserRegistered)
@@ -133,9 +118,9 @@ class SendConfirmationEmailHandler:
133118To propagate an event, it must be transmitted to ` RelatedEvents ` instance.
134119
135120``` python
136- from cq import Command, RelatedEvents, command_handler
121+ from cq import RelatedEvents, command_handler
137122
138- class UserRegistrationCommand ( Command ) :
123+ class UserRegistrationCommand :
139124 """ Data required to register a user """
140125
141126@command_handler (UserRegistrationCommand)
0 commit comments