An example
Consider the following use case:
<?php abstract class User extends \Prooph\EventSourcing\AggregateRoot { protected $name; protected $email; public function name() { return $this->name; } public function email() { return $this->email; } protected function whenUserWasRegisterd(UserWasRegisterd $event) { $this->name = $event->name(); $this->email = $event->email(); } } class Admin extends User { public static function register($name, $email) { $self = new self(); $self->recordThat(UserWasRegisterd::withData('admin', $name, $email); return $self; } } class Member extends User { public static function register($name, $email) { $self = new self(); $self->recordThat(UserWasRegisterd::withData('member', $name, $email); return $self; } }So in order to make this work, you need 3 small changes in your application.
Step 1: Create a UserAggregateTranslator
<?php final class UserAggregateTranslator extends \Prooph\EventSourcing\EventStoreIntegration\AggregateTranslator { /** * @param \Prooph\EventStore\Aggregate\AggregateType $aggregateType * @param \Iterator $historyEvents * @return object reconstructed AggregateRoot */ public function reconstituteAggregateFromHistory( \Prooph\EventStore\Aggregate\AggregateType $aggregateType, \Iterator $historyEvents ) { $aggregateRootDecorator = $this->getAggregateRootDecorator(); $firstEvent = $historyEvents->current(); $type = $firstEvent->type(); if ($type === 'admin') { return $aggregateRootDecorator->fromHistory(Admin::class, $historyEvents); } elseif ($type === 'member') { return $aggregateRootDecorator->fromHistory(Member::class, $historyEvents); } } }
Step 2: Change the assertion method in the EventStoreUserCollection
<?php final class EventStoreUserCollection extends \Prooph\EventStore\Aggregate\AggregateRepository { public function add(User $user) { $this->addAggregateRoot($user); } public function get(UserId $userId) { return $this->getAggregateRoot($userId->toString()); } protected function assertAggregateType($eventSourcedAggregateRoot) { \Assert\Assertion::isInstanceOf($eventSourcedAggregateRoot, User::class); } }
Step 3: Make use of your custom AggregateTranslator
<?php final class EventStoreUserCollectionFactory { public function __invoke(ContainerInterface $container) { return new EventStoreUserCollection( $container->get(EventStore::class), AggregateType::fromAggregateRootClass(User::class), new UserAggregateTranslator() ); } }
If you use the provided container factory (\Prooph\EventStore\Container\Aggregate\AbstractAggregateRepositoryFactory) then you can also just change the
aggregate_translator
key in your config to point to the new UserAggregateTranslator
and register the UserAggregateTranslator
in your container.
Nice way of expressing your ideas with us.thanks for sharing with us and please add more information's.
ReplyDeleteJAVA Training in Chennai
JAVA Training in Tnagar
Selenium Training in Chennai
Digital Marketing Course in Chennai
Python Training in Chennai
Big data training in chennai
JAVA training in Annanagar
Java courses in chennai anna nagar
Great Article Cloud Computing Projects
DeleteNetworking Projects
Final Year Projects for CSE
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
ReplyDeletedigital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore
Admire this blog. Keep sharing more updates like this
ReplyDeleteTally Course in Chennai
Tally Training in Chennai
Tally training in coimbatore
Tally course in madurai
Tally Course in Hyderabad
Tally Training in Bangalore
Tally classes in coimbatore
Tally coaching centre in coimbatore
Tally course in marathahalli
German Classes in Bangalore
It is actually a great and helpful piece of information. I am satisfied that you simply shared this helpful information with us. Please stay us informed like this. Thanks for sharing.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
Being a travel blogger and lover, it was appealing and helpful for me. It is a strong article to portray this topic limits the readers to widen their horizon of accepted wisdom and power as they go through the article.
ReplyDeleteSAP training in Kolkata
SAP training Kolkata
Best SAP training in Kolkata
SAP course in Kolkata
You are really performing grand work. I must articulate that you really have done a great research before writing. Keep up the good work!
ReplyDeleteData Science training in Mumbai
Data Science course in Mumbai
SAP training in Mumbai
Thanks mate. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also event marketing and What is B2B Event Marketing
ReplyDeletewonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
ReplyDeleteThis article resolved my all queries.good luck an best wishes to the team members.learn digital marketing use these following link
Digital Marketing Course in Chennai
Thanks for sharing informative post. Tamil typing online is the best tool to translate Tamil to English fast and easy. Tamil typing Software download | Bamini to Unicode
ReplyDelete