Giter Site home page Giter Site logo

Comments (4)

codeliner avatar codeliner commented on July 28, 2024

Ok, let's see if I understand your question:

You first want to change the state of the aggregate by recording an event and then test the new state against a specification?

Can you give me a real world example?

The thing is, an aggregate should only record an event if everything is fine. Doing the validation after event recording sounds like a modeling issue.This also means that NO validation should take place in apply functions. Once an event is recorded, it is written in stone. The aggregate has to change state according to the event, no matter what.

Anyway, if you really want to change state before passing it to the specification (so basically validate a future state), you should modify a local copy of the state and pass the temporary modified state to the specification. If you use a single state property in your aggregates and this state property is an immutable data structure, changing a local copy in a handle function should be doable.

But again, I'd be interested in a real world example to see if this can be designed in a different way.

from php-engine.

arnedesmedt avatar arnedesmedt commented on July 28, 2024

Previously we did something like this in the event sourcing library:

public function addVhost(
        VhostUuid $vhostUuid,
        VhostName $name,
        VhostAliases $aliases,
        VhostPort $port,
        VhostWebroot $webroot,
        AbstractSpecification $specification
    ) : self {
        $this->recordThat(
            VhostAdded::fromData(
                $this->aggregateId(),
                $vhostUuid,
                $name,
                $aliases,
                $port,
                $webroot
            )
        );

        $this->checkVhost($vhostUuid, $specification);

        return $this;
    }

private function checkVhost(VhostUuid $vhostUuid, AbstractSpecification $specification) : void
    {
        /** @var stdClass|null $vhost */
        $vhost = $this->vhost($vhostUuid);

        if (! $specification->isSatisfiedBy($vhost)) {
            throw $specification->notSatisfiedException();
        }
    }

CheckVhost throws an exception if the specification is not met. And because of that the events are not written to the database (but yes, they are already applied in the code, and now I know, that's wrong)

Could it be that the specification pattern is not a good way to check business logic in the aggregate? Because of the state that isn't applied when you need to check the constraints.

Do you use specific design patterns to execute some business logic?

Thx in advance!

Arne

from php-engine.

codeliner avatar codeliner commented on July 28, 2024

Hi, sorry I was offline a few days. The specification pattern is absolutely fine here. You just use it too late ;). You need to do the vhost check before recording an event.

Looking at your code I'd say vhost should be an immutable record. You could construct it from the arguments passed to addVhost and then pass the vhost to the specification.

from php-engine.

arnedesmedt avatar arnedesmedt commented on July 28, 2024

Thx for the answer! It's indeed a better solution to create the vhost manually instead of using event engine for it.

from php-engine.

Related Issues (14)

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.