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

Change event handling methods #33

Closed
prolic opened this issue Oct 30, 2016 · 8 comments
Closed

Change event handling methods #33

prolic opened this issue Oct 30, 2016 · 8 comments

Comments

@prolic
Copy link
Member

prolic commented Oct 30, 2016

Currenty we use something like this:

protected function apply(AggregateChanged $e)
{
    $handler = $this->determineEventHandlerMethodFor($e);

    if (! method_exists($this, $handler)) {
        throw new \RuntimeException(sprintf(
            'Missing event handler method %s for aggregate root %s',
            $handler,
            get_class($this)
        ));
    }

    $this->{$handler}($e);
}

New approach with be like this:

protected function apply(AggregateChanged $event) {
    switch (get_class($event)) {
    case ManagerWasBlocked::class:
        $this->whenManagerWasBlocked($event); // delegate to when...-method
        break;
    case ManagerWasDeleted::class:
        $this->state = ManagerState::DELETED(); // handle immediately
        break;
    default:
        throw new \UnexpectedValueException('Unknown domain event');
    }
}

Reasons:

  • performance
  • possibilty to call when-event-name-methods within is still possible
  • use of custom event names (f.e. using dots) is easy

refers to: prooph/service-bus#109

@prolic prolic added this to the 5.0 Release milestone Oct 30, 2016
@oqq
Copy link
Member

oqq commented Oct 30, 2016

This issue depends on an open PR, which benchmarks the apply method. I will provide more cases, especially for a switch construct. #32

I would like to decide if a event is handled by an "on" method in the aggregate. With the current implementation i have to create always an empty 'when' method, if not. With your approach that would be possible. So +1

@prolic
Copy link
Member Author

prolic commented Oct 30, 2016

@oqq Please note this: In the event sourcing lib, there are less problems, as we always expect the event to be an instance of AggregateChanged. But in the service-bus a message (command, event or query) doesn't have to be an object. It can also be an array or string. So here an onEvent-method helps really a lot to handle those situations for listeners.

@oqq
Copy link
Member

oqq commented Oct 30, 2016

Ok, so i write benchmarks for event sourcing with the assume it would be an instance of AggregateChanged. I have started to test against strings. Thanks for the hint. :)

@sandrokeil
Copy link
Member

Looks good. Migrating existing code to this is also easy. You have to write a little bit more code, but the flexibility/performance is worth it.

@oqq You find some extended bench examples here.

@oqq
Copy link
Member

oqq commented Oct 31, 2016

We could provide some examples in docs, so the user could catch from templates for the implementation. Maybe with a reference to benchmarks.

@bweston92
Copy link
Member

Prefer this method, looks more functional too.

@codeliner
Copy link
Member

👍 for the approach
you could also use the old approach as fallback in the default case so users don't need to migrate all their aggregates at once and prooph-cli would continue to work if you use it.

@prolic
Copy link
Member Author

prolic commented Nov 8, 2016

Fallback is already available in this repo:

protected function apply(AggregateChanged $e): void

it's protected an can simply be overridden in userland code to achive the "new style".

@prolic prolic closed this as completed Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants