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.

76 comments:

  1. The article "Inheritance with Aggregate Roots in ProophEvent-Store" is wonderful. The guidance for aggregate translators is clear and concise. Thanks for the detailed coding explanation. I appreciate the effort you have invested in the blog. Don't stop sharing.
    Digital marketing courses in patna

    ReplyDelete
  2. The writing is excellent in this post on "Inheritance with Aggregate Roots in ProophEvent-Store." The tutorial on the topic is excellent and is written in simple terms. Thank you for providing such a thorough explanation. Continue your wonderful work. I appreciate your wonderful and enlightening writing.
    financial modelling course in kenya

    ReplyDelete
  3. It's a great article called "Inheritance with Aggregate Roots in ProophEvent-Store." The advice for aggregate translators is concise and easy to understand. I appreciate your thorough explanation of the coding. I appreciate the time and effort you put into the blog. Continue sharing.
    Data Analytics Courses in Ghana

    ReplyDelete
  4. Best blog ever on "Inheritance with Aggregate Roots in ProophEvent-Store." The administrator has given all the information you need, along with a ton of suggestions and advice, to help you understand the topic better. I appreciate you taking the time to explain the code. You're welcome. Keep up the good work.
    Data Analytics Courses In Coimbatore

    ReplyDelete
  5. The author did an excellent job with this! This blog post on "Inheritance with Aggregate Roots in ProophEvent-Store" was really interesting to read. You did a great job explaining the code, and I appreciate it. Thank you very much. Continue your good work.
    Data Analytics Courses in Gurgaon

    ReplyDelete
  6. Exceptionally well-written blog with some of the greatest stuff on the subject of "Inheritance with Aggregate Roots in ProophEvent-Store." This blog is quite elusive; I'm glad I found it. Appreciate the author's expertise. great work
    Continue to post.
    Data Analytics Courses in Mumbai

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I'm delighted I found this site because it's hard to find. Respect the author's knowledge. The writer did a fantastic job with his essay on "Inheritance with Aggregate Roots in ProophEvent-Store." The site is really nicely written and easy for everyone to understand.
    I appreciate you sharing.
    Digital Marketing Courses in Vancouver

    ReplyDelete
  9. A fantastic blog. This website is difficult to discover, so I'm glad I found it. Observe the author's expertise. With his essay on "Inheritance with Aggregate Roots in ProophEvent-Store," the author performed an excellent job. The website is really well written and simple to grasp for everyone.
    Thank you for sharing.
    Digital Marketing Courses in Australia

    ReplyDelete
  10. all the steps are clear about this topic Inheritance with Aggregate Roots in ProophEvent-Store with example also. the content is neat and clean to understand. Thank you so much for the amazing article you have written. Digital marketing Courses in Bhutan

    ReplyDelete
  11. Thank you for make us understand the concept of Inheritance with Aggregate Roots , The blog is precise and clear. Looking forward for more detailed blogs on current technologies.
    Data Analytics Courses In Nagpur

    ReplyDelete
  12. The post on "Inheritance with Aggregate Roots in ProophEvent-Store" is outstanding. You have done a great job of articulating the concept in easy to understand language. Keep up the good work! I am truly grateful for your clear and insightful writing.
    financial modelling course in bangalore

    ReplyDelete
  13. Amazing blog. Great information on " Inheritance with Aggregate Roots in ProophEvent-Store." The step-by-step explanation of the application changes is easy to follow and implement. As a novice, I found this clear-cut coding explanation handy. I appreciate the writer's effort in creating this article. Thanks, and do continue to share a more informative blog posts in the future. Financial modeling course in Singapore

    ReplyDelete
  14. Hello Blogger,
    the article is a great one. Thank you for sharing the information that I think it benefited many users. I like it .
    Data Analytics Courses in Zurich

    ReplyDelete
  15. .I'm grateful for you sharing your expertise on this amazing blog! This blog post does a great job of outlining online payments, and gives a clear understanding of how they work.. Data Analytics Courses In Indore

    ReplyDelete
  16. Excellent blog post. Great content is provided about " Inheritance with Aggregate Roots in ProophEvent-Store." The in-depth explanation of "UserAggregateTranslator, assertion method, and AggregateTranslator" is very educational. As a newbie, I found it handy. I have obtained more knowledge after reading this blog. Thanks for the post. Do continue sharing more in the future. Data Analytics courses in leeds

    ReplyDelete
  17. Hello dear,
    this quite great. The content is really helpful to several, especially wedbdevelopers. data Analytics courses in thane

    ReplyDelete
  18. I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information and well researched content material. It shows about the learnings of this subject. If you are a student and you are interested in learning more about Financial modelling course in Jaipur, then I would like to recommend you with this article on: financial modelling course in jaipur

    ReplyDelete
  19. Superb blog. This article on "Inheritance with Aggregate Roots in ProophEvent-Store" is quite informative. The application updates' detailed description is simple to follow and put into practice. I like your concise explanation of coding as a rookie. I admire the time and effort the author put into this article. Thank you, and keep sharing more educational blog posts in the future. Data Analytics courses in Glasgow

    ReplyDelete
  20. Hi dear blogger,
    that is great blog post. I really enjoyed reading it. It is an important tutorial. Data Analytics Course Fee

    ReplyDelete
  21. I have recently been reading blogs about ProophEvent-Store and this article on inheritance with aggregate roots is exceptional. The functions that are cited are very helpful and give a clear picture. Thank you for this blog.
    Data Analytics Jobs

    ReplyDelete
  22. hello blogger,
    Your tutorial is important for users, especially learners. I like it, thanks for making this post.
    Business Analytics courses in Pune

    ReplyDelete
  23. Awesome blog. This exciting paper is titled "Inheritance with Aggregate Roots in ProophEvent-Store." The precise explanation of the application updates is easy to understand and apply, and I admire how well you explained coding for beginners. I appreciate the author's time and effort in writing this post. Thank you, and keep posting informative blog entries. Data Analyst Course Syllabus

    ReplyDelete
  24. Hi dear blogger,
    I found your blog post interesting enough to read. I like your step by step process. Thanks for all. For those interested in qualification in data analytics, please check here. Data Analytics Qualifications

    ReplyDelete
  25. Hello, I have recently started reading blogs on ProophEvent-Store, but this article is very different and interesting. The application of aggregate roots looks amazing and the code examples mentioned help a lot in getting a clear picture. I'll be saving this for reference.
    Data Analytics VS Data Science

    ReplyDelete
  26. This has to be a good blog due to the one which it always comes with new and exciting information Inheritance with Aggregate Roots and going through this I learnt that this blog really has all those quality which a blog requires to have one. Also, if anyone is interested in learning more about Data Analyst Salary In India, then I would like to recommend you with this article to know and learn more about: Data Analyst Salary In India

    ReplyDelete
  27. Hi dear blogger,
    That is an excellent job you have done. After I read it, I was excited to try it. Thank you for sharing your knowledge with us here. Keep doing the great work. Best Business Accounting & Taxation Course in India

    ReplyDelete
  28. I am glad to visit your blog Sasaprolic. I liked it very much. It is very well written. Keep sharing!
    Visit- CA Coaching in Mumbai

    ReplyDelete
  29. Learn Data Analytics Courses in navi Mumbai in the top reputed institutes to learn the basics to the advanced level concepts by doing it practically which also covers technologies like Excel, Advanced Excel, Tableau, SQL, Power BI, Basics of R & Python Data Analytics Courses in navi Mumbai 

    ReplyDelete
  30. it was very interesting to read article. your expertise on this subject is amazing. keep up the good work Data Analytics Courses In Vadodara 

    ReplyDelete
  31. excellent job in writing this blog post. thanks for sharing this knowledge with us Digital marketing courses in Varanasi

    ReplyDelete
  32. financial modelling courses has the great scope in any organisation. Get trained in financial modelling to get expertise in excel proficiency, financial management, planning, budgeting, reporting & presentation etc..

    ReplyDelete
  33. Very interesting article on ProophEvent-Store. I appreciate the efforts put into this. Keep sharing more informative content like this.
    Best Business Accounting & Taxation Course in India

    ReplyDelete
  34. Hi dear blogger,
    after ai read this blog post, i was excited about the content. I found it informative, and engaging enough. Thanks a lot for this kind job. Best SEO Courses in India

    ReplyDelete
  35. Interesting explanation of the post about inheritance with aggregate roots. Best GST Courses in India

    ReplyDelete
  36. The explanations with examples on making inheritance work with aggregate roots in ProophEvent-Store was something new for me in learning and I'm really glad to come across your blog. Best GST Courses in India

    ReplyDelete

  37. Daily Affirmations About Friendships · I cherish my friends.I am thoughtful and considerate of my lovely friends. Affirmations-For-Friends

    ReplyDelete
  38. Hi dear blogger,
    I am really glad to find this blog post here. I especially appreciate it as a useful tutorial. After I went through it, I was really glad to have. thanks for all. Best Content Writing Courses in India

    ReplyDelete
  39. You've done a great job in writing this blog on inheritance with aggregate roots in Domain-Driven Design. You have explained the concept in a very detailed and simple way. I really appreciate the way you have highlighted the importance of aggregate roots in Domain-Driven Design and how it can be used in inheritance. You have also explained the concept of Bounded Contexts and how they can help in implementing inheritance. I really appreciate you for taking the time to write this blog. Thanks for the amazing job. FMVA

    ReplyDelete
  40. This article is very informative. Thanks for sharing. keep posting like this.
    Best Tally Courses in India

    ReplyDelete
  41. This blog is an excellent read. You have presented the concept of aggregate roots and inheritance in a very lucid and easy to understand manner. You have explained the concept with a simple example which is very helpful for readers. I appreciate the detailed explanation of the topic and the additional information that you have provided. Thanks for writing such an informative blog. Article Writing

    ReplyDelete
  42. You did a great job in explaining inheritance with aggregate roots in DDD. You provided very clear examples and diagrams to help explain how the model works. It was especially helpful that you highlighted the main points, like how the aggregate root can be used to ensure business invariants and to ensure that the data remains consistent. In this way, readers can understand the concept more easily. Thank you for taking the time to write this informative blog. Best Technical Writing Courses in India

    ReplyDelete
  43. Try Shopify free and start a business or grow an existing one. Get more than ecommerce software with tools to manage every part of your business.Myshopify.Stores

    ReplyDelete
  44. Great article! You did a great job in explaining how to use inheritance with aggregate roots in Domain Driven Design. You broke down all the aspects and complexities of the design, and gave some great examples to illustrate the points. I especially liked how you discussed the importance of considering lifecycles when making decisions about inheritance. Thanks for taking the time to write and share this informative article. Digital Marketing Courses in Glassglow

    ReplyDelete
  45. Thanks blogger This blog is an excellent read on basic concept of aggregate roots and inheritance with the code.
    Digital Marketing Courses In Centurion

    ReplyDelete
  46. This comment has been removed by the author.

    ReplyDelete
  47. Well explained and informative article thank you for sharing.
    Digital Marketing Courses In Krugersdorp

    ReplyDelete
  48. Informative!!! I really enjoyed reading this piece of information. Thanks for sharing this. Digital Marketing Courses In zaria 

    ReplyDelete
  49. Beat coding article. Thanks for sharing this very interesting article. I really enjoyed reading this blog. Do produce more article like this. Digital Marketing Courses In Tembisa

    ReplyDelete
  50. Very technical article. Information provided and understood by experts.
    Digital Marketing Courses In hobart

    ReplyDelete
  51. Hello, as someone who has only lately begun reading blogs on ProophEvent-Store, I find this piece to be both unique and fascinating. The use of aggregate roots looks fantastic, and the provided code examples greatly aid in visualizing the program. Thank you for sharing. Best Tally Courses in India

    ReplyDelete
  52. Unlock the power of aggregate roots in ProophEvent-Store and take your application design to the next level with the expert insights shared on Inheritance with Aggregate Roots in ProophEvent-Store blog
    Benefits of Online digital marketing course

    ReplyDelete
  53. Overall, this was a great article with a unique point of view. This is such a great information. It will be useful for many. Good job .
    Digital Marketing Courses In Atlanta

    ReplyDelete
  54. The way you explain a complex topic in an easy-to-understand way is really impressive.
    How Digital marketing is changing business

    ReplyDelete
  55. Hey blogger really a great work by you and thanks for taking time to sharing this to us
    How Digital marketing works

    ReplyDelete
  56. This article will provide accurate and contemporary information about social media marketing and advertising. It will also define the two strategies before looking at how to implement them in a step-by-step way to increase the success of your business, as well as examine where both tools are used with the greatest effect. 
     Social media marketing and advertising 

    ReplyDelete
  57. This article provides a clear and concise solution for making inheritance work with aggregate roots using a common repository for all subtypes in ProophEvent-Store v6.1. The example code and step-by-step instructions make it easy to understand and implement.
    Types of digital marketing which is ideal



    ReplyDelete
  58. Great article on Inheritance with Aggregate Roots. It was very informative. Also, Check this detailed guide on the top 10 Digital Marketing modules.
     What are the Top 10 Digital marketing modules 

    ReplyDelete
  59. Excellent article about Inheritance with Aggregate Roots in ProophEvent-Store

    Social media marketing ideas

    ReplyDelete
  60. Great write up. You are doing great job.
    Brand marketing

    ReplyDelete
  61. This article is a fantastic resource for anyone working with ProophEvent-Store in PHP. The explanations are well-structured, and the code examples make it easy to grasp the concept of inheritance with aggregate roots. Thank you for sharing this valuable knowledge with the community!
    Top benefits of using social media for business



    ReplyDelete