Thinking about Aggregates in Active Record

Поделиться
HTML-код
  • Опубликовано: 2 авг 2024
  • 👨‍💻 Learn Test-Driven Development with Laravel!
    tddwithlaravel.com
    Sign up to 30 Days of Laravel 👉🏻 30daysoflaravel.com
    👨‍💻 Sign up to my newsletter and receive PHP, JS and Laravel news in a weekly-basis:
    subscribe.mateusguimaraes.com
    🎉 Party up:
    / mateusjatenee
    / mateusjatenee
    / mateusguimaraes
    Thanks for watching!
  • НаукаНаука

Комментарии • 24

  • @lassestube
    @lassestube 3 месяца назад +5

    This kind of more intermediate/advanced content is dearly missed on youtube! Great explanation and example, albeit simplified does illustrate the issue and possible solution in an eloquent way.. i mean elegant 😂

    •  3 месяца назад

      Thank you my dude 🫶🏻

  • @tmanley1985
    @tmanley1985 3 месяца назад +3

    One thing this also does is put off any database transactions until the end. I've found that if you wrap everything into one database transaction at first, you end up stuffing a bunch of stuff in there without thinking about it, and those things (e.g., network calls, memory allocations for instances, etc.) all incur a cost. This can lead to long running transactions in your database which can lead to all manner of issues. This is an elegant pattern. Thanks!

  • @saralightbourne
    @saralightbourne 3 месяца назад

    oh god this is perfect. i'm a django developer and i've suffered these issues a lot. i tried learning DDD but couldn't get anything because i was thinking domain models = orm models. but it all made sense in the first 5 minutes of your video. thank uuuuu

    •  3 месяца назад

      Happy to be of help 🫡

  • @turboclown
    @turboclown 3 месяца назад +1

    Love your channel and videos, I am learning something new with every one. And I always think about how this could benefit my code base. Thank you!

    •  3 месяца назад +1

      Thay’s a very kind message. Thank you so much, you’ve made my day!

  • @afsdab
    @afsdab 3 месяца назад +1

    I confess I'm just watching your videos because your voice 🤣
    Another great vídeo !

    •  3 месяца назад +1

      Haha I’ll take that as a compliment :-)

    • @afsdab
      @afsdab 3 месяца назад

      @ hahah
      Besides that, I liked the content you have here, it's awesome, concepts and examples are rare to find nowadays

  • @mariano.pualiu
    @mariano.pualiu 3 месяца назад

    I read about Aggregates from DDD a long time ago, and didn't quite figured out them for Laravel ORM (I think it is mostly a thing for Data Mapper) but this helped me to make it sink properly, thank you, still not quite sure why you have `Ticket` and `OrderTicket` Models, and why you pass a `Ticket` to the `addTicket` method and it creates an `OrderTicket` is `Ticket` kind of a ValueObject or a DTO?

    •  3 месяца назад

      A "Ticket" is part of the Event aggregate, an "OrderTicket" is part of the Order aggregate (maybe OrderLine would've been a better name).
      In a modular context, they'd both have the same name in different contexts. e.g Order\Ticket and Event\Ticket.

  • @lpheller
    @lpheller 3 месяца назад

    Great video. Thanks for sharing these thoughts.
    With things like OrderLines - which actually only exist within the order - what do you think of the approach of denormalizing the data structure and not storing it in a separate database table but as embedded entities? This would possibly solve some "problems" with persistence (and of course bring new challenges^^).
    Keep up the good work. I really enjoying your content.

    •  3 месяца назад

      Hey! Thanks for the comment.
      I’m not sure what do you mean with “embedded entities” - do you mean storing it alongside the order row?
      If so, I’m not sure - RDBMS are meant for this and persistence/consistency should not be an issue. I can see that working with good JSON support though (e.g Postgres).

    •  3 месяца назад

      Although you’d have very limited querying abilities against line items.

  • @necrotikS
    @necrotikS 3 месяца назад

    You probably won't have a ton of line items, however, what if you're dealing with a domain model that could have a lot of "line items"? If you keep pushing into the array, you'll run out of memory. But this is not a "domain" problem. How to solve this?

    •  3 месяца назад +2

      I can’t think of a lot of scenarios where you’d have to save a bunch of stuff together in one go. In these cases I’d probably just queue it afterwards, and leave the aggregate root in a pending state until that is done.
      Do you have a concrete example?

  • @bb-gi9ky
    @bb-gi9ky 3 месяца назад

    $command->customer->id, $order->payment->customer etc violate encapsulation

    •  3 месяца назад

      How would you do it?

  • @khangle6872
    @khangle6872 3 месяца назад

    Something just doesnt sit right with me when business-related method is mixed with Eloquent "config" method on a Model.
    Especially when you have the Order model, you __can__ still call save() and all that.
    The way I see it, there are 3 methods in the save order "flow": start(), addTicket(), place(). Yet they are defined amidst a dozens of Eloquent specific methods: relationship, accessor and mutators, casts,... On top of the inherited Model methods: saves(), delete(), update(),...
    If someone would onboard 5 years from now, how would he know that these 3 methods are special and belong in a "flow", and that he should not call save() directly? How would he know to organize additional such methods?
    If we are talking about Aggregates, we are obviously implementing DDD, or at least DDD-like patterns, which separate business logic from infrastructure implementation. I think it best we dont put Domain Models (which handle business logic) and ActiveRecord models (which is atrongly coupled to db) inside each other

    •  3 месяца назад +3

      You’re not wrong; I believe my thinking differs in the sense that I am assuming Eloquent was *already chosen* and it represents a domain model (as it does in most apps) - so it’s more of: we have already chosen Eloquent - given the tradeoffs, what can we do to make our lives easier on the long run within those constraints?
      You’re right , there’s really nothing stopping you from just calling save(), etc, so it’s mostly a matter of documentation and everyone being on the same page.

  • @Competitiveforlolz
    @Competitiveforlolz 3 месяца назад

    Just use the data mapper pattern lol, active record is such an anti pattern.

  • @bled72
    @bled72 3 месяца назад

    Concepts are too advanced for me but great video.

    •  3 месяца назад +1

      What did you not understand? Maybe we can help!