DEV Community

Robson TenĂ³rio
Robson TenĂ³rio

Posted on

Our way: Actions vs Services

đŸ‘‰ Go to Summary

There is no wrong or right way, there is our way.

-- Someone

.
.
.

Actions

  1. It represents an internal app use case.
  2. Each action must contain one use case.
  3. Naming is important.
  4. An action can call another action or service in complex scenarios.

class MakeUserAvatarAction {

    function __construct(){ 
        // ... 
    }

    function execute(){
        // It does only 1 thing
    }    
}

Enter fullscreen mode Exit fullscreen mode

Services

  1. It represents external integrations, commonly through API.
  2. You can have many methods.
class KeycloakService{

    function __construct(){ 
        // ... 
    }

    function createUser() { }

    function syncRoles() { }

    ...
}
Enter fullscreen mode Exit fullscreen mode

Actions vs Services

  1. An action has one method, that solves one internal use case.
  2. An service can have more methods, that represents an external integration.
app/
    |
    |__ Actions/    
    |   |   
    |   |__ post/
    |   |   |
    |   |   |__ DeletePostAction.php
    |   |   |__ PromotePostAction.php
    |   |
    |   |__ users/
    |       |
    |       |__ MakeAvatarAction.php
    |
    |__ Services/
        |
        |__ PaymentService.php
        |__ KeycloackService.php

Enter fullscreen mode Exit fullscreen mode

Top comments (0)