The action pattern explained

Поділитися
Вставка
  • Опубліковано 25 січ 2025

КОМЕНТАРІ • 30

  • @phpannotated
    @phpannotated  Місяць тому +5

    Do you use the action pattern? Share your experience with it in the comments!

    • @ZAcharyIndy
      @ZAcharyIndy Місяць тому

      Hi, can you explain more about "virtual properties" in PHP 8.4??

  • @DiabloInformer
    @DiabloInformer Місяць тому +56

    You “invented” actions like I invented the wheel lol.

  • @brandonyoung2766
    @brandonyoung2766 Місяць тому +7

    CQRS, but simplified

  • @hotwaff
    @hotwaff Місяць тому +10

    Huh. My co-workers and I were writing actions in this way in 2012. But I must be wrong, because you invented this in 2018.

  • @sabatino-dev
    @sabatino-dev Місяць тому

    Great video! I've been using the actions pattern for a datasharing implementation in my food ordering app - something along the lines of:
    $dataShareAction = new DataShareAction($apiClient, $transformer);
    $dataShareAction($order);
    Where $apiClient and $transformer are specific to the third party receiving the data. Works great and is super easy to reuse.

  • @GamePlayByFaks
    @GamePlayByFaks Місяць тому +12

    but actions were invested by Kent Back.... in 90s

  • @KoljaLukin
    @KoljaLukin Місяць тому +9

    So, how is this different from the “Command” pattern?
    en.m.wikipedia.org/wiki/Command_pattern

  • @yramagicman675
    @yramagicman675 Місяць тому +4

    When the actions pattern goes wrong everything sucks. My current employer transitioned from outsourced developers to in-house developers, and I was hired as a part of that transition. The outsourced team used "actions" by name, but not in the way you described. Their actions aren't injected, aren't mock-able (they use a static execute method, meaning it can't be mocked), they tend to be littered with side effects, and there's instances where one action calls another action which calls a third action and so on. All of this has lead to spaghetti code that is nearly impossible to unit test, and incredibly difficult to follow.

  • @lupusge2503
    @lupusge2503 Місяць тому +7

    Reminds me of commands and queries to be honest. You could encapsulate the „PublishBook“ action logic in a command and I guess it would only be a matter of naming, correct?

    • @lucasayabe
      @lucasayabe Місяць тому +1

      Its almost that, but its more in line with what a application service should be, CQRS has a more specific meaning to write persistance logic, its more like take your repository, and extract each method into a class.

  • @juaniquito
    @juaniquito Місяць тому +2

    Mailing an email would be a side effect, so it wouldn't be as strict as functional programming, but I get the picture.
    It helps with code organization and removes possible spaghetti code inside the entry point (controller, command, etc).

  • @BuriTechVids
    @BuriTechVids Місяць тому +5

    Is this pattern somehow different from Clean Architecture? Uncle Bob has a great video about it named "The Principles of Clean Architecture by Uncle Bob Martin"
    But mainly there's (Bob calls it 'Interactor', we're calling it an 'UseCase' or 'Feature') class which encapsulate one use-case (or logic like "ListUsersUseCase", "ApproveUserUseCase", "PublishBookUseCase") which should be independent from infrastructure. And then use cases it self must not know from where the data came from.

  • @joejohn.
    @joejohn. Місяць тому

    Thoughts about a service class calling all of these independent-but-related actions like an orchestrator?

  • @ihorrud5088
    @ihorrud5088 Місяць тому

    Thanks Brendt!

  • @Pekz00r
    @Pekz00r Місяць тому

    Yes, this is a fantastic pattern. Your book Laravel beyond CRUD was a game changer for me. I am so greatful!
    I use execute as the action method. That reads good in the code where you use it I think.

  • @Wolfci
    @Wolfci Місяць тому

    So using action pattern for controller logic to keep the controllers short ?

  • @alexz75515
    @alexz75515 Місяць тому

    Don't see much difference between actions and jobs. But my struggle with Action is keep then side effect free.

  • @ahmadrio
    @ahmadrio Місяць тому

    thanks for subtitle exists

  • @coder_one
    @coder_one Місяць тому +9

    Dude read about the simplicity and reliability of functional programming where a function takes an input and returns an output, wrapped it up in classes because after all that's what people in the OOP world do and called it his pattern. What an ego!

    • @kozjabradicadomagojapintar8477
      @kozjabradicadomagojapintar8477 Місяць тому +2

      Yes 😅 keeping business logic in one place and call that new invented pattern...what is the diff between action pattern and use case or service repo pattern? Basically the same, he just calls it action pattern

    • @oceanfrog
      @oceanfrog Місяць тому

      You seem nice. 💩🤡

  • @mtdr3
    @mtdr3 Місяць тому +1

    lol ok I mean yeah sure, literally services but you separate your WhateverService into invocable little services called action. why I watch shit like that at night instead of sleeping

  • @FaridAdamgrino
    @FaridAdamgrino Місяць тому

    Thank fun!

  • @gaby14888
    @gaby14888 Місяць тому +3

    Huge ego. Little to no contribution. Study harder. Git gud

  • @spicynoodle7419
    @spicynoodle7419 Місяць тому

    Transaction Script

  • @keimx
    @keimx Місяць тому +8

    use case 🥱

    • @fredpalas
      @fredpalas Місяць тому +1

      Exactly, can be called from a query or command bus, He says similar but at the end is the same just called from different places.