Giter Site home page Giter Site logo

Comments (5)

arnedesmedt avatar arnedesmedt commented on July 28, 2024

In addition you need to set a maxVersion in https://github.com/event-engine/php-engine/blob/master/src/Aggregate/GenericAggregateRepository.php#L210 while the eventstore can load events without maxVersion.

from php-engine.

arnedesmedt avatar arnedesmedt commented on July 28, 2024

Oh sorry, I was confused by the word state. But now, I see, I'm retrieving the aggregate root. But my second question is still valid.

from php-engine.

arnedesmedt avatar arnedesmedt commented on July 28, 2024

I created a pull request for it: #9

from php-engine.

codeliner avatar codeliner commented on July 28, 2024

Hi,

I think in event-engine, you need to restore it via the EventEngine class?

Yes, but only if you need the aggregate state outside of its own boundary to make decisions elsewhere (f.e. in a process manager or another aggregate) or want to use it as a read model. When handling commands, Event Engine will load the aggregate and restore current state. You don't need an extra collection for that.

When using the OopFlavour aggregate state === aggregate instance, because state of the aggregate is part of it. But even when using the OopFlavour I highly recommend to pass read-only aggregate state around instead of the aggregate instance itself. For example our OOP aggregates only have one private state property. And this state is an immutable object. When the aggregate handles a command, it records an event and in the corresponding event apply function uses something like:

final class User
{
  // PHP 7.4 property type hint ;)
  private UserState $userState;
  // ...
  private function whenUsernameChanged(UsernameChanged $event)
  {
    // Replace immutable state with new version
    $this->userState = $this->userState->with([UserState::NAME => $event->newUsername()]);
  }

  public function state(): UserState
  {
    return $this->userState;
  }
}

As you can see the aggregate root provides a method to retrieve its immutable state. Only return this state from a UserCollection! No part of your code should have access to state changing methods of your aggregates. Only commands can trigger state changes and EE takes care of passing a command to a fresh aggregate instance with latest state restored.

EventEngine::loadAggreateState() gives you the latest aggregate (state) using a snapshot from the document store if available + newer events (if snapshot is behind)

EventEngine::loadAggregateStateUnil() is meant to be used only if you need aggregate (state) at a certain point in time (time travels). This is handy for some advanced use cases or projections that rely on aggregate state at the time when the corresponding event was recorded. The latter should be avoided, but that's not always possible.

from php-engine.

arnedesmedt avatar arnedesmedt commented on July 28, 2024

Thx Alexander for the detailed explanation and handy tips. It's clear for me now!

from php-engine.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.