Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infer aggregate id normalizer #612

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"doctrine/dbal": "^3.8.1|^4.0.0",
"doctrine/migrations": "^3.3.2",
"patchlevel/hydrator": "^1.3.1",
"patchlevel/hydrator": "^1.4.1",
"patchlevel/worker": "^1.2.0",
"psr/cache": "^2.0.0|^3.0.0",
"psr/clock": "^1.0",
Expand Down
38 changes: 19 additions & 19 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions deptrac-baseline.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
deptrac:
skip_violations:
Patchlevel\EventSourcing\Aggregate\AggregateRootId:
- Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer
Patchlevel\EventSourcing\Aggregate\CustomId:
- Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer
Patchlevel\EventSourcing\Aggregate\Uuid:
- Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer
Patchlevel\EventSourcing\Attribute\Processor:
- Patchlevel\EventSourcing\Subscription\RunMode
Patchlevel\EventSourcing\Attribute\Projector:
Expand Down
8 changes: 2 additions & 6 deletions docs/pages/aggregate.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,16 @@ final class CreateProfileHandler

In order that an aggregate is actually saved, at least one event must exist in the DB.
For our aggregate we create the Event `ProfileRegistered` with an ID and a name.
Since the ID is a complex data type and cannot be easily serialized, we need to define a normalizer for the ID.
We do this with the `IdNormalizer` attribute.
We also give the event a unique name using the `Event` attribute.

```php
use Patchlevel\EventSourcing\Aggregate\Uuid;
use Patchlevel\EventSourcing\Attribute\Event;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Event('profile.registered')]
final class ProfileRegistered
{
public function __construct(
#[IdNormalizer]
public readonly Uuid $profileId,
public readonly string $name,
) {
Expand All @@ -114,7 +110,6 @@ final class ProfileRegistered
!!! note

You can find out more about events [here](./events.md).
And for normalizer [here](./normalizer.md).

After we have defined the event, we have to adapt the profile aggregate:

Expand Down Expand Up @@ -489,7 +484,8 @@ final class NameChanged
```
!!! warning

The payload must be serializable and unserializable as json.
You need to create a normalizer for the `Name` value object.
So the payload must be serializable and unserializable as json.

!!! note

Expand Down
22 changes: 0 additions & 22 deletions docs/pages/aggregate_id.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\Uuid;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Aggregate('profile')]
final class Profile extends BasicAggregateRoot
{
#[Id]
#[IdNormalizer]
private Uuid $id;
}
```
!!! note

If you want to use snapshots, then you have to make sure that the aggregate id are normalized.
You can find how to do this [here](normalizer.md).

You have multiple options for generating an uuid:

```php
Expand All @@ -63,13 +56,11 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Aggregate\CustomId;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Aggregate('profile')]
final class Profile extends BasicAggregateRoot
{
#[Id]
#[IdNormalizer]
private CustomId $id;
}
```
Expand All @@ -79,11 +70,6 @@ final class Profile extends BasicAggregateRoot
you need to change the `aggregate_id_type` to `string` in the store configuration.
More information can be found [here](store.md).

!!! note

If you want to use snapshots, then you have to make sure that the aggregate id are normalized.
You can find how to do this [here](normalizer.md).

So you can use any string as an id:

```php
Expand Down Expand Up @@ -124,21 +110,14 @@ So you can use it like this:
use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
use Patchlevel\EventSourcing\Attribute\Aggregate;
use Patchlevel\EventSourcing\Attribute\Id;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Aggregate('profile')]
final class Profile extends BasicAggregateRoot
{
#[Id]
#[IdNormalizer]
private ProfileId $id;
}
```
!!! note

If you want to use snapshots, then you have to make sure that the aggregate id are normalized.
You can find how to do this [here](normalizer.md).

We also offer you some traits, so that you don't have to implement the `AggregateRootId` interface yourself.
Here for the uuid:

Expand Down Expand Up @@ -167,4 +146,3 @@ class ProfileId implements AggregateRootId
* [How to create an aggregate](aggregate.md)
* [How to create an event](events.md)
* [How to test an aggregate](testing.md)
* [How to normalize value objects](normalizer.md)
5 changes: 5 additions & 0 deletions docs/pages/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ final class ProfileCreated
}
}
```
!!! tip

Built-in normalizers like `IdNormalizer` and `DateTimeImmutableNormalizer` can be inferred from the type hint
and so you don't have to specify them. If you want to configure the Normalizer, you still have to do it.

!!! note

You can find out more about normalizer [here](normalizer.md).
Expand Down
2 changes: 0 additions & 2 deletions docs/pages/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ A hotel can be created with a `name` and a `id`:
```php
use Patchlevel\EventSourcing\Aggregate\Uuid;
use Patchlevel\EventSourcing\Attribute\Event;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Event('hotel.created')]
final class HotelCreated
{
public function __construct(
#[IdNormalizer]
public readonly Uuid $hotelId,
public readonly string $hotelName,
) {
Expand Down
Loading
Loading