Friday, February 26, 2016

Inheritance with Aggregate Roots in ProophEvent-Store

If you want to make inheritance work with aggregate roots using a common repository for all subtypes, this can be achieved very easily. You need the latest ProophEvent-Store v6.1 to do this.

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.

38 comments:

  1. Replies
    1. Great Article Cloud Computing Projects

      Networking 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

      Delete
  2. 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
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  3.  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.
    SAP training in Kolkata
    SAP training Kolkata
    Best SAP training in Kolkata
    SAP course in Kolkata

    ReplyDelete
  4.  You are really performing grand work. I must articulate that you really have done a great research before writing. Keep up the good work!
    Data Science training in Mumbai
    Data Science course in Mumbai
    SAP training in Mumbai

    ReplyDelete
  5. 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

    ReplyDelete
  6. wonderful 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.
    This 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

    ReplyDelete
  7. 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
  8. Very nice article. I enjoyed reading your post. very nice share. I want to twit this to my followers. Thanks
    พวงหรีด
    รับทำ seo
    wm casino
    poker online
    โควิด

    ReplyDelete
  9. Such a very useful blog. Very Interesting to read this blog. I would like to thank you for the efforts you had made for writing this awesome blog. Great work.
    คลิปโป๊
    คลิปxxx
    คลิปโป๊ญี่ปุ่น
    คลิปโป้ไทย
    เรียนภาษาอังกฤษ
    kardinal stick

    ReplyDelete
  10. Watch movies online sa-movie.com, watch new movies, series Netflix HD 4K หนังฝรั่ง, watch free movies on your mobile phone, Tablet, watch movies on the web.

    SEE4K Watch movies, watch movies, free series, load without interruption, sharp images in HD FullHD 4k, ดูหนังชนโรง all matters, all tastes, see anywhere, anytime, on mobile phones, tablets, computers.

    GangManga read manga, read manga, read manga online for free, fast loading, clear images in HD quality, อ่านมังงะ all titles, anywhere, anytime, on mobile, tablet, computer.

    Watch live football ดูบอลออนไลน์, watch football online, link to watch live football, watch football for free.

    ReplyDelete
  11. SamudraBet Situs Judi Online Terbaik, Terpercaya dan Terbesar di Indonesia. Menyediakan permainan :

    Judi bola terbaik

    Judi slot online terpercaya

    Poker online terbaru

    Live casino online

    Judi sabung ayam

    ReplyDelete
  12. This article will outline all the different strategies you should be aware of when it comes to soccer.

    Best IAS Coaching in India

    ReplyDelete
  13. The color and consistency have nothing to do with the quality of the CBD Flower. Only the cultivation method can be recognized by the color:

    ReplyDelete
  14. Other information on the international composition of WSOP Online 2021 will be published shortly after PokerNews guarantees that those events will be held on GGpoker.

    ReplyDelete
  15. Our UFA800 has various game rooms for you to choose from ดูบอลออนไลน์ .For those of you who like to play casino games in the name of UFAGAME, it is a collection of many games such as baccarat games, bounce games

    ReplyDelete
  16. เราคือผู้นำด้านเกมพนันออนไลน์ Major168 เราคือผู้ให้บริการ คาสิโนออนไลน์ ที่ได้รับรองว่าดีที่สุดในประเทศไทย มีค่ายเกมส์ให้เล่นมากมาย Sagaming, Sexy bacarat, Dreamgame, Ebet, Wm casino, Vivo gaming ไม่ผ่านเอเย่นต์ ระบบปลอดภัยมีทีมงานดูแลตลอด 24ชม.

    SAGAME88 แหล่งรวมเกมส์พนันออนไลน์ คาสิโนสด บาคาร่า กำถั่ว คาสิโนออนไลน์ ไฮโล รูเล็ต รับเครดิตฟรีเล่นได้ทุกเกมส์ โบนัสสมาชิกใหม่เพียบ พร้อมระบบฝากถอนออโต้ 10วิ เรามีทุกค่ายเกมส์ให้คุณเลือกเดิมพัน SA Game Sexy bacarat Dreamgame WM Casino VIVO Gaming Ebet เล่นได้ทุกเกมส์

    Our website ufabet provides betting services in the system of Auto Deposit-Withdrawal. Our members are not only in Thailand. Online football betting UEFA Bet market And this makes a guarantee that Ufabet168 is another reliable football betting website

    ufa through the website UFABET1688 a web gambling online , one that integrated all the bets from online casinos , online casino , online , and also have a game a lot more to be chosen to play such games.

    ReplyDelete
  17. Baccarat online You can play multiple tables at the same time. Make an increase in profits Members who love to bet baccarat can choose to bet on the table. Baccarat online A variety of tables And many websites Online gambling website ufabet has combined various famous online casinos into one website. Members can choose to play as much as they want.

    ReplyDelete
  18. Suggest how to play the game The preferred card game in the globe, but for anyone looking for fun with card video games. Get prepared to opt for renowned sites from around the globe. Ready to provide you with the challenge And online gambling poker tournaments Additionally, it incorporates a bonus. เล่น poker เว็บไหนดี, given to the racers of ours that are ready to fight in a major tournament worth having fun with information from real experts from the big competition with a good deal of experience We provide chip exchange services for players. All over Thailand is able to line of us and notify Games you would like to play immediately.

    ReplyDelete
  19. HDhub4u Tamil produces HD in addition to a high-quality print of newly-launched films to this is clients. This web page is known for offering Telugu films on the very next day of the release of films. hdhub4u has been building a name for themselves since the website's begging in year 2017.

    ReplyDelete
  20. WatchCartoonOnline has its server located within European nations. It offers worldwide businesses inconspicuously English talking nations, besides the US, UK, and Australia, to administer several examples. The web based site has been thought of to confront personal time that moves on for a unique span.

    ReplyDelete
  21. Credit card annual percentage rates, commonly known as APRs, determine how much you'll pay in interest if you carry a balance on your credit card. Your card's APR can vary depending on a few different factors, and there can be different types of APRs for each card. Consider your card's APR when shopping for a new credit card or negotiating rates on a current credit card.
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com
    fishyfacts4u.com

    ReplyDelete
  22. Weebly has been around for a very long time and it is an extremely popular option if you only want free with no intention of using any upgrades. Weebly is at present hosting around 40 million websites.
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com
    emagazinehub.com

    ReplyDelete
  23. Apply for a store credit card. Use the card responsibly – pay in full and on time – and you can start building a strong credit history.

    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com
    inewshunter.com

    ReplyDelete
  24. However, she says, "What arguably is different about the enhancements here is that it will go to the most vulnerable families: those who don't have earned income, those who don't have a tax liability. This enhancement really helps those who suffer, who really are poor, and even in extreme poverty."

    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com
    juicyfactor.com

    ReplyDelete
  25. Since the credit is fully refundable, that could mean more cash in parents' pockets. When combined with the other family assistance available through the American Rescue Plan Act, the Center on Poverty & Social Policy at Columbia University estimates there is the potential to cut the 2021 child poverty rate in half.

    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com
    localnewsbuzz.com

    ReplyDelete
  26. Did you guys never pirate anything? Seriously how can you pay using BDT in Steam/Origin/Uplay lol. They don't even recognize your country. torrentbd is the First Bangladeshi Torrent Site and the biggest one currently.

    ReplyDelete
  27. I love what you guys tend to be up too. This type of clever work and reporting! Keep up the awesome works guys I’ve added you guys to our blogroll.
    Tainiomania

    ReplyDelete
  28. Thank you for another informative blog. Where else could I get that type of info written in such a perfect approach? I have an undertaking that I’m simply now working on, and I’ve been on the lookout for such information.
    Crazyhd

    ReplyDelete
  29. Here I am for the first time, and I found beneficial information. I am really impressed with the information. I look forward to more comprehensive and insightful posts like this in the future. Keep up the good work.
    3Movierulz

    ReplyDelete
  30. It is truly a nice and useful piece of info. I am glad that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.
    Dhakamovie

    ReplyDelete
  31. Sectioned is a slight outlier from the rest, in that it follows no uniform purpose, but rather contains one-page websites that emulate multiple pages using section breaks. For power users or those of you with a clear artistic vision, there's also the option to begin with a blank canvas.

    Entertainment
    Fashion & Beauty
    Foods & Drinks
    Gadgets
    Gaming
    Health & Fitness
    Lifestyle
    Movie

    ReplyDelete