|
3 | 3 | Aggregate 6 - Functional style |
4 | 4 | ============================== |
5 | 5 |
|
6 | | -This example shows another variation of the ``Dog`` aggregate class used |
7 | | -in the tutorial and module docs. |
| 6 | +This example shows how to define and use your own immutable aggregate base class with a more "functional" |
| 7 | +style than :doc:`example 5 </topics/examples/aggregate5>`. |
8 | 8 |
|
9 | | -Like in the previous example, this example defines immutable ``Aggregate`` and |
10 | | -``DomainEvent`` base classes, as frozen data classes. However, this time the |
11 | | -aggregate class has no methods. All the functionality has been implemented |
12 | | -as module-level functions. |
| 9 | +Base classes |
| 10 | +------------ |
| 11 | + |
| 12 | +The :class:`~examples.aggregate6.baseclasses.DomainEvent` class is defined as a "frozen" Python :class:`dataclass`. |
| 13 | + |
| 14 | +.. literalinclude:: ../../../examples/aggregate6/baseclasses.py |
| 15 | + :pyobject: DomainEvent |
| 16 | + |
| 17 | +The :class:`~examples.aggregate6.baseclasses.Aggregate` base class in this example is also defined as a "frozen" Python |
| 18 | +:class:`dataclass`. |
| 19 | + |
| 20 | +.. literalinclude:: ../../../examples/aggregate6/baseclasses.py |
| 21 | + :pyobject: Aggregate |
| 22 | + |
| 23 | +A generic :class:`~examples.aggregate6.baseclasses.aggregate_projector` function is also defined, which takes |
| 24 | +a mutator function and returns a function that can reconstruct an aggregate of a particular type from an iterable |
| 25 | +of domain events. |
| 26 | + |
| 27 | +.. literalinclude:: ../../../examples/aggregate6/baseclasses.py |
| 28 | + :pyobject: aggregate_projector |
13 | 29 |
|
14 | | -Like in the previous examples, the application code in this example must receive |
15 | | -the domain events that are returned from the aggregate command methods. The aggregate |
16 | | -projector function must also be supplied when getting an aggregate from the |
17 | | -repository and when taking snapshots. |
18 | 30 |
|
19 | 31 |
|
20 | 32 | Domain model |
21 | 33 | ------------ |
22 | 34 |
|
| 35 | +The :class:`~examples.aggregate6.domainmodel.Dog` aggregate class is defined as immutable frozen data class |
| 36 | +that extends the aggregate base class. |
| 37 | + |
23 | 38 | .. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 39 | + :pyobject: Dog |
| 40 | + |
| 41 | +The aggregate event classes, :class:`~examples.aggregate6.domainmodel.DogRegistered` and |
| 42 | +:class:`~examples.aggregate6.domainmodel.TrickAdded`, are explicitly defined as separate module level classes. |
| 43 | + |
| 44 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 45 | + :pyobject: DogRegistered |
| 46 | + |
| 47 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 48 | + :pyobject: TrickAdded |
| 49 | + |
| 50 | +The aggregate commands, :func:`~examples.aggregate6.domainmodel.register_dog` and |
| 51 | +:func:`~examples.aggregate6.domainmodel.add_trick` are defined as module level functions. |
| 52 | + |
| 53 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 54 | + :pyobject: register_dog |
| 55 | + |
| 56 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 57 | + :pyobject: add_trick |
| 58 | + |
| 59 | +The mutator function, :func:`~examples.aggregate6.domainmodel.mutate_dog`, is defined as a module level function. |
| 60 | + |
| 61 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 62 | + :start-at: @singledispatch |
| 63 | + :end-before: project_dog |
| 64 | + |
| 65 | +The aggregate projector function, :func:`~examples.aggregate6.domainmodel.project_dog`, is defined as a module |
| 66 | +level function by calling :func:`~examples.aggregate6.baseclasses.aggregate_projector` with |
| 67 | +:func:`~examples.aggregate6.domainmodel.mutate_dog` as the argument. |
| 68 | + |
| 69 | +.. literalinclude:: ../../../examples/aggregate6/domainmodel.py |
| 70 | + :start-at: project_dog |
24 | 71 |
|
25 | 72 |
|
26 | 73 | Application |
27 | 74 | ----------- |
28 | 75 |
|
| 76 | +The :class:`~examples.aggregate6.application.DogSchool` application in this example uses the library's |
| 77 | +:class:`~eventsourcing.application.Application` class. It must receive the new events that are returned |
| 78 | +by the aggregate command methods, and pass them to its :func:`~eventsourcing.application.Application.save` |
| 79 | +method. The aggregate projector function must also be supplied when reconstructing an aggregate from the |
| 80 | +repository, and when taking snapshots. |
29 | 81 |
|
30 | 82 | .. literalinclude:: ../../../examples/aggregate6/application.py |
| 83 | + :pyobject: DogSchool |
31 | 84 |
|
32 | 85 |
|
33 | 86 | Test case |
34 | 87 | --------- |
35 | 88 |
|
| 89 | +The :class:`~examples.aggregate6.test_application.TestDogSchool` test case shows how the |
| 90 | +:class:`~examples.aggregate6.application.DogSchool` application can be used. |
36 | 91 |
|
37 | 92 | .. literalinclude:: ../../../examples/aggregate6/test_application.py |
| 93 | + :pyobject: TestDogSchool |
38 | 94 |
|
39 | 95 |
|
40 | 96 | Code reference |
41 | 97 | -------------- |
42 | 98 |
|
| 99 | +.. automodule:: examples.aggregate6.baseclasses |
| 100 | + :show-inheritance: |
| 101 | + :member-order: bysource |
| 102 | + :members: |
| 103 | + :undoc-members: |
| 104 | + |
43 | 105 | .. automodule:: examples.aggregate6.domainmodel |
44 | 106 | :show-inheritance: |
45 | 107 | :member-order: bysource |
|
0 commit comments