@@ -303,6 +303,11 @@ final class Profile extends BasicAggregateRoot
303303 }
304304}
305305```
306+ !!! tip
307+
308+ You don't necessarily need to define multiple `Apply` attributes with the event class
309+ if you define the event types in the method using a union type.
310+
306311## Suppress missing apply methods
307312
308313Sometimes you have events that do not change the state of the aggregate itself,
@@ -358,6 +363,38 @@ final class Profile extends BasicAggregateRoot
358363
359364 When all events are suppressed, debugging becomes more difficult if you forget an apply method.
360365
366+ ## Shared apply context
367+
368+ When working with [ micro-aggregates] ( ./aggregate.md#micro-aggregates ) ,
369+ it’s common that events are applied by different aggregates.
370+ As a result, an aggregate may receive events it does not handle, which can lead to multiple “missing apply” warnings.
371+
372+ The ` SharedApplyContext ` attribute allows you to declare that several aggregates share the same apply context.
373+ With this configuration, a missing apply is only reported if none of the shared aggregates handle the event.
374+
375+ ``` php
376+ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot;
377+ use Patchlevel\EventSourcing\Attribute\Aggregate;
378+ use Patchlevel\EventSourcing\Attribute\SharedApplyContext;
379+ use Patchlevel\EventSourcing\Attribute\Stream;
380+
381+ #[Aggregate('profile')]
382+ #[SharedApplyContext([PersonalInformation::class])]
383+ final class Profile extends BasicAggregateRoot
384+ {
385+ }
386+
387+ #[Aggregate('personal_information')]
388+ #[Stream(Profile::class)]
389+ #[SharedApplyContext([Profile::class])]
390+ final class PersonalInformation extends BasicAggregateRoot
391+ {
392+ }
393+ ```
394+ !!! warning
395+
396+ You need to define the `SharedApplyContext` attribute on all aggregates that share the apply context.
397+
361398## Stream Name
362399
363400!!! warning
@@ -675,8 +712,10 @@ use Patchlevel\EventSourcing\Aggregate\Uuid;
675712use Patchlevel\EventSourcing\Attribute\Aggregate;
676713use Patchlevel\EventSourcing\Attribute\Apply;
677714use Patchlevel\EventSourcing\Attribute\Id;
715+ use Patchlevel\EventSourcing\Attribute\SharedApplyContext;
678716
679717#[Aggregate('order')]
718+ #[SharedApplyContext([Shipping::class])]
680719final class Order extends BasicAggregateRoot
681720{
682721 #[Id]
@@ -706,10 +745,12 @@ use Patchlevel\EventSourcing\Aggregate\Uuid;
706745use Patchlevel\EventSourcing\Attribute\Aggregate;
707746use Patchlevel\EventSourcing\Attribute\Apply;
708747use Patchlevel\EventSourcing\Attribute\Id;
748+ use Patchlevel\EventSourcing\Attribute\SharedApplyContext;
709749use Patchlevel\EventSourcing\Attribute\Stream;
710750
711751#[Aggregate('shipping')]
712752#[Stream(Order::class)]
753+ #[SharedApplyContext([Order::class])]
713754final class Shipping extends BasicAggregateRoot
714755{
715756 #[Id]
@@ -740,6 +781,11 @@ final class Shipping extends BasicAggregateRoot
740781 }
741782}
742783```
784+ !!! tip
785+
786+ With the [SharedApplyContext](./aggregate.md#shared-apply-context) attribute,
787+ you can suppress missing applies for events that are handled by other aggregates.
788+
743789### Child Aggregates
744790
745791??? example "Experimental"
0 commit comments