|
| 1 | +# Aggregate |
| 2 | + |
| 3 | +> Aggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster of domain objects |
| 4 | +> that can be treated as a single unit. [...] |
| 5 | +> |
| 6 | +> :book: [DDD Aggregate - Martin Flower](https://martinfowler.com/bliki/DDD_Aggregate.html) |
| 7 | +
|
| 8 | +You can find out more about the aggregates in the library [documentation](https://github.com/patchlevel/event-sourcing#documentation). |
| 9 | +This documentation is limited to bundle integration. |
| 10 | + |
| 11 | +## Register aggregates |
| 12 | + |
| 13 | +So that the necessary service such as the repositories are available, |
| 14 | +you have to register all aggregates. |
| 15 | + |
| 16 | +You can do this in the Yaml definition by listing all aggregates with names and the associated class. |
| 17 | + |
| 18 | +```yaml |
| 19 | +patchlevel_event_sourcing: |
| 20 | + aggregates: |
| 21 | + hotel: |
| 22 | + class: App\Domain\Hotel\Hotel |
| 23 | +``` |
| 24 | +
|
| 25 | +Or you use the attribute variant (since v1.2). |
| 26 | +Here you have to give all aggregates the `Aggregate` attribute and give the associated name. |
| 27 | + |
| 28 | +```php |
| 29 | +namespace App\Domain\Hotel; |
| 30 | +
|
| 31 | +use Patchlevel\EventSourcing\Aggregate\AggregateRoot; |
| 32 | +use Patchlevel\EventSourcingBundle\Attribute\Aggregate; |
| 33 | +
|
| 34 | +#[Aggregate(name: 'hotel')] |
| 35 | +final class Hotel extends AggregateRoot |
| 36 | +{ |
| 37 | + // ... |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +So that the bundle knows where to look, you also have to specify a path. |
| 42 | + |
| 43 | +```yaml |
| 44 | +patchlevel_event_sourcing: |
| 45 | + aggregates_paths: '%kernel.project_dir%/src' |
| 46 | +``` |
| 47 | + |
| 48 | +> :book: You can also define multiple paths by specifying an array. |
| 49 | + |
| 50 | +## Use snapshots |
| 51 | + |
| 52 | +You can also tell each aggregate that it should use snapshots |
| 53 | +so that the rebuilding of the state is faster. |
| 54 | + |
| 55 | +To do this, a snapshot store must first be defined. |
| 56 | +You can read that [here](snapshots.md). |
| 57 | + |
| 58 | +And then you can define the snapshot on the aggregates. |
| 59 | + |
| 60 | +```yaml |
| 61 | +patchlevel_event_sourcing: |
| 62 | + aggregates: |
| 63 | + hotel: |
| 64 | + class: App\Domain\Hotel\Hotel |
| 65 | + snapshot: default |
| 66 | +``` |
| 67 | + |
| 68 | +If you are using attributes then you have to put the snapshot there. |
| 69 | + |
| 70 | +```php |
| 71 | +namespace App\Domain\Hotel; |
| 72 | +
|
| 73 | +use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot; |
| 74 | +use Patchlevel\EventSourcingBundle\Attribute\Aggregate; |
| 75 | +
|
| 76 | +#[Aggregate(name: 'hotel', snapshotStore: 'default')] |
| 77 | +final class Hotel extends SnapshotableAggregateRoot |
| 78 | +{ |
| 79 | + // ... |
| 80 | +} |
| 81 | +``` |
0 commit comments