snapshotter icon indicating copy to clipboard operation
snapshotter copied to clipboard

Not working with one stream for all

Open prolic opened this issue 7 years ago • 2 comments

See https://github.com/prooph/proophessor-do/issues/158

prolic avatar Apr 30 '18 15:04 prolic

Hi!

I think I'm hitting this too. I recently introduced multiple aggregate types in one stream and noticed some snapshotters are not running. I have created a patch that seems to solve it. But as this package is deprecated (and my patch isn't backwards compatible) what are the changes this would get merged and released?

The gist of it is; instead of CustomerRepository accepting a single aggregateRepository and multiple types I tweaked it into accepting multiple repositories.

SnapshotReadModelFactory

public function __invoke(ContainerInterface $container): SnapshotReadModel
{
    return new SnapshotReadModel(
        [
            $container->get(CustomerRepository::class),
            $container->get(OrderLeadRepository::class)
        ],
        new AggregateTranslator(),
        $snapshotStore,
        [
            \HF\Api\Domain\Customer\Aggregate\Customer::class,
            \HF\Api\Domain\Customer\Aggregate\OrderLead::class,
        ]
    );
}

and the changed the persist method to try all repositories

SnapshotReadModel::persist

public function persist(): void
    {
        foreach (\array_unique($this->aggregateCache) as $aggregateId) {
            foreach($this->aggregateRepositories as $aggregateRepository) {
                $aggregateRoot = $aggregateRepository->getAggregateRoot($aggregateId);

                if ($aggregateRoot) {
                    break;
                }
            }

            $this->snapshotStore->save(new Snapshot(
                (string) AggregateType::fromAggregateRoot($aggregateRoot),
                $aggregateId,
                $aggregateRoot,
                $this->aggregateTranslator->extractAggregateVersion($aggregateRoot),
                new \DateTimeImmutable('now', new \DateTimeZone('UTC'))
            ));
        }

        foreach($this->aggregateRepositories as $aggregateRepository) {
            $aggregateRepository->clearIdentityMap();
        }

        $this->aggregateCache = [];
    }

basz avatar Jul 07 '21 10:07 basz

instead of going

[
    $container->get(CustomerRepository::class),
    $container->get(OrderLeadRepository::class)
]

can we use a AggregateRepository implementation that wraps multiple repository inside itself?

prolic avatar Aug 31 '21 03:08 prolic