|
3 | 3 | Aggregate 7 - Pydantic and orjson |
4 | 4 | ================================= |
5 | 5 |
|
6 | | -This example shows another variation of the ``Dog`` aggregate class used |
7 | | -in the tutorial and module docs. |
8 | | - |
9 | | -Similar to the previous example, the model is expressed in a functional |
10 | | -style. In contrast to the previous example, this example uses Pydantic |
11 | | -to define immutable aggregate and event classes, rather than defining |
12 | | -them as Python frozen data classes. This has implications for the |
13 | | -persistence layer. |
14 | | - |
15 | | -The application class in this example uses its own persistence classes |
16 | | -``PydanticMapper`` and ``OrjsonTranscoder``. Pydantic is responsible |
17 | | -for converting domain model objects to object types that orjson can |
18 | | -serialise, and for reconstructing model objects from JSON objects |
19 | | -that have been deserialised by orjson. |
20 | | - |
21 | | -One advantage of using Pydantic here is that any custom value objects |
22 | | -will be automatically reconstructed without needing to define the |
23 | | -transcoding classes that would be needed when using the library's |
24 | | -default ``JSONTranscoder``. This is demonstrated in the example below |
25 | | -with the ``Trick`` class, which is used in both aggregate events and |
26 | | -aggregate state, and which is reconstructed from serialised string |
27 | | -values, representing only the name of the trick, from both recorded |
28 | | -aggregate events and from recorded snapshots. |
| 6 | +This example shows how to use Pydantic to define immutable aggregate and event classes. |
| 7 | + |
| 8 | +The main advantage of using Pydantic here is that any custom value objects |
| 9 | +used in the domain model will be automatically serialised and deserialised, |
| 10 | +without needing also to define custom :ref:`transcoding<Transcodings>` classes. |
| 11 | + |
| 12 | +This is demonstrated in the example below with the :class:`~examples.aggregate7.domainmodel.Trick` class, |
| 13 | +which is used in both aggregate events and aggregate state, and which is reconstructed from serialised string |
| 14 | +values, representing only the name of the trick, from both recorded aggregate events and from recorded snapshots. |
29 | 15 |
|
30 | 16 | Pydantic mapper and orjson transcoder |
31 | 17 | ------------------------------------- |
32 | 18 |
|
| 19 | +The application class in this example uses a :ref:`mapper<Mapper>` that supports Pydantic and a :ref:`transcoder<Transcoder>` that uses orjson. |
| 20 | + |
| 21 | +The :class:`~examples.aggregate7.orjsonpydantic.PydanticMapper` class is a |
| 22 | +:ref:`mapper<Mapper>` that supports Pydantic. It is responsible for converting |
| 23 | +domain model objects to object types that orjson can serialise, and for |
| 24 | +reconstructing model objects from JSON objects that have been deserialised by orjson. |
| 25 | + |
33 | 26 | .. literalinclude:: ../../../examples/aggregate7/orjsonpydantic.py |
| 27 | + :pyobject: PydanticMapper |
| 28 | + |
| 29 | +The :class:`~examples.aggregate7.orjsonpydantic.OrjsonTranscoder` class is a |
| 30 | +:ref:`transcoder<Transcoder>` that uses orjson, possibly the fastest JSON transcoder |
| 31 | +available in Python. |
| 32 | + |
| 33 | +.. literalinclude:: ../../../examples/aggregate7/orjsonpydantic.py |
| 34 | + :pyobject: OrjsonTranscoder |
34 | 35 |
|
35 | 36 |
|
36 | 37 | Pydantic model for immutable aggregate |
37 | 38 | -------------------------------------- |
38 | 39 |
|
| 40 | +The code below shows how to define base classes for immutable aggregates that use Pydantic. |
| 41 | + |
39 | 42 | .. literalinclude:: ../../../examples/aggregate7/immutablemodel.py |
40 | 43 |
|
41 | 44 |
|
42 | 45 | Domain model |
43 | 46 | ------------ |
44 | 47 |
|
| 48 | +The code below shows how to define an immutable aggregate in a functional style, using the Pydantic module for immutable aggregates. |
| 49 | + |
45 | 50 | .. literalinclude:: ../../../examples/aggregate7/domainmodel.py |
46 | 51 |
|
47 | 52 |
|
48 | 53 | Application |
49 | 54 | ----------- |
50 | 55 |
|
| 56 | +The :class:`~examples.aggregate7.application.DogSchool` application in this example uses the library's |
| 57 | +:class:`~eventsourcing.application.Application` class. It must receive the new events that are returned |
| 58 | +by the aggregate command methods, and pass them to its :func:`~eventsourcing.application.Application.save` |
| 59 | +method. The aggregate projector function must also be supplied when reconstructing an aggregate from the |
| 60 | +repository, and when taking snapshots. |
| 61 | + |
51 | 62 | .. literalinclude:: ../../../examples/aggregate7/application.py |
| 63 | + :pyobject: DogSchool |
52 | 64 |
|
53 | 65 |
|
54 | 66 | Test case |
55 | 67 | --------- |
56 | 68 |
|
| 69 | +The :class:`~examples.aggregate7.test_application.TestDogSchool` test case shows how the |
| 70 | +:class:`~examples.aggregate7.application.DogSchool` application can be used. |
| 71 | + |
57 | 72 | .. literalinclude:: ../../../examples/aggregate7/test_application.py |
| 73 | + :pyobject: TestDogSchool |
58 | 74 |
|
59 | 75 |
|
60 | 76 | Code reference |
|
0 commit comments