Skip to content

Commit 5eb9730

Browse files
authored
Merge pull request #46 from patchlevel/add-aggregate-dokumentation
add aggregate documentation
2 parents 27b17e2 + f9b0836 commit 5eb9730

4 files changed

Lines changed: 103 additions & 35 deletions

File tree

README.md

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ this [installation documentation](docs/installation.md).
3434
We recommend reading the documentation for the [library](https://github.com/patchlevel/event-sourcing) first,
3535
as this documentation only deals with bundle integration.
3636

37+
* [Aggregate](docs/aggregate.md)
3738
* [Repository](docs/repository.md)
3839
* [Event Bus](docs/event_bus.md)
3940
* [Processor](docs/processor.md)
@@ -149,6 +150,7 @@ use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
149150
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
150151
use Symfony\Component\Uid\Uuid;
151152

153+
#[Aggregate(name: 'hotel')]
152154
final class Hotel extends AggregateRoot
153155
{
154156
private Uuid $id;
@@ -231,42 +233,9 @@ final class Hotel extends AggregateRoot
231233
}
232234
```
233235

234-
> :book: You can find out more about aggregates
235-
> and events in the library [documentation](https://github.com/patchlevel/event-sourcing#documentation).
236+
> :warning: The attribute variant is only available since v1.2. Switch to the v1.1 branch to read the older documentation.
236237
237-
Next we have to make our aggregate known:
238-
239-
```yaml
240-
patchlevel_event_sourcing:
241-
aggregates:
242-
hotel:
243-
class: App\Domain\Hotel\Hotel
244-
```
245-
246-
or by adding the corresponding aggregate attribute.
247-
The snapshotStore is optional and `null` by default.
248-
249-
```php
250-
namespace App\Domain\Hotel;
251-
252-
use Patchlevel\EventSourcing\Aggregate\AggregateChanged;
253-
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
254-
use Patchlevel\EventSourcingBundle\Attribute\Aggregate;
255-
256-
#[Aggregate(name: 'hotel', snapshotStore: 'default')]
257-
final class Hotel extends AggregateRoot
258-
{
259-
protected function apply(AggregateChanged $event): void
260-
{
261-
262-
}
263-
264-
public function aggregateRootId(): string
265-
{
266-
return '1';
267-
}
268-
}
269-
```
238+
> :book: You can find out more about aggregates [here](./docs/aggregate.md).
270239
271240
### Define projections
272241

docs/aggregate.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
```

docs/installation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Now you have to add a minimal configuration file here `config/packages/patchleve
4444

4545
```yaml
4646
patchlevel_event_sourcing:
47+
aggregates_paths: '%kernel.project_dir%/src'
4748
connection:
4849
url: '%env(EVENTSTORE_URL)%'
4950
```

docs/snapshots.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@ patchlevel_event_sourcing:
4646
snapshot_store: default
4747
```
4848
49+
If you are using attributes then you have to put the snapshot there.
50+
51+
```php
52+
namespace App\Domain\Profile;
53+
54+
use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot;
55+
use Patchlevel\EventSourcingBundle\Attribute\Aggregate;
56+
57+
#[Aggregate(name: 'profile', snapshotStore: 'default')]
58+
final class Profile extends SnapshotableAggregateRoot
59+
{
60+
// ...
61+
}
62+
```
63+
64+
> :book: You can find out more about the attributes [here](aggregate.md).
65+
4966
## Batch (since v1.2)
5067

5168
So that not every write process also writes to the cache at the same time,

0 commit comments

Comments
 (0)