Comments (4)
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.
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.
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.
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)
- Check uniqueness of Command, Query and Event names HOT 3
- meaning of CommandDispatchResult HOT 4
- EventEngine::rebuildAggregateState does not work as expected with MultiModelStore HOT 1
- Describe differences between original project and fork HOT 1
- How does migration work? HOT 2
- Next Gen Event Engine
- Context Providers should also receive the state if exists HOT 2
- Support dedicated dispatcher for commands
- Method fromCachedConfig should only load config
- Any roadmap? HOT 3
- Cannot rewind a generator that was already run HOT 3
- Process manager in event-engine HOT 2
- Restore aggregate root in event engine HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-engine.