Created
August 19, 2012 06:52
-
-
Save beberlei/3392926 to your computer and use it in GitHub Desktop.
CQRS "complexities" compared to "regular" apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User | |
{ | |
private $email; | |
private $emailChangedAt; | |
public function changeEmail($email) | |
{ | |
// assignment in wrong method, easy to miss in larger methods | |
$this->emailChangedAt = new \DateTime("now"); | |
$this->apply(new EmailChanged(array("email" => $email))); | |
} | |
protected function applyEmailChanged($event) | |
{ | |
$this->email = $event->email; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* For every event a test of this kind is necessary, | |
* where you compare all properties of the aggregate root | |
* and all its children to a "real" object and one object | |
* that was completly reconstructed from events. | |
*/ | |
public function testApplyChangeEmail() | |
{ | |
$user = new User(); | |
$user->changeEmail("kontakt@beberlei.de"); | |
$userEs = new User(); | |
$userEs->loadFromHistory(array( | |
new EmailChanged(array("email" => "kontakt@beberlei.de")) | |
)); | |
$this->assertEquals($user, $userEs); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Subscription | |
{ | |
private $position; | |
private $user; | |
private $valid; | |
public function __construct(User $user, OrderPosition $position) | |
{ | |
$this->apply(new SubscriptionCreated(array( | |
"user" => $user, | |
"position" => $position | |
))); | |
} | |
protected function applySubscriptionCreated($event) | |
{ | |
$this->user = $event->user; | |
$this->position = $event->position; | |
// now replaying this method requires "position" | |
// to be the instance of the "position" at the point ´ | |
// where the event was triggered, otherwise it could be | |
// "true" in the past, and "false" during reconstruction | |
// of OrderPosition entity. | |
$this->valid = $event->position->isValid(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment