{ "uuid": [ { "value": "12fb6cce-1f55-4359-b189-700c8ba16935" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T09:00:38+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "Services vs Actions\n" } ], "created": [ { "value": "2023-07-05T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T09:00:38+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2023\/07\/05\/services-vs-actions", "langcode": "en" } ], "body": [ { "value": "\n

When creating a custom module, where do you put your business logic?<\/p>\n\n

You want to keep classes like Controllers and Commands simple and move any reusable logic into separate files.<\/p>\n\n

Usually, this means 'Service' classes, but another approach I like is to use 'Action' classes.<\/p>\n\n

What is an Action class?<\/h2>\n\n

An Action is a PHP class representing a single action that must be performed.<\/p>\n\n

It usually contains a single method with a descriptive name summarising the task, such as GetAccessToken<\/code>.<\/p>\n\n

This differs from a generic service like ApiService<\/code> with multiple methods like getAccessToken()<\/code>.<\/p>\n\n

Using Action classes<\/h2>\n\n

I'll register Action classes in the service container to use dependency injection and autowiring and easily inject the Action into other classes that need it, like Controllers and Commands.<\/p>\n\n

If you need multiple implementations, multiple actions can implement the same Interface and make them swappable, such as having GetAccessToken<\/code> and GetAndCacheAccessToken<\/code> implement the same GetsAccessToken<\/code> interface.<\/p>\n\n

That also enables using design patterns like Decorators with Actions.<\/p>\n\n

Why I like Actions<\/h2>\n\n

I like more readable and meaningful class names and prefer working with multiple simpler classes than those with fewer complex ones.<\/p>\n\n

I like leveraging design patterns I'm used to, such as the Decorator pattern, by having common interfaces and contracts.<\/p>\n\n

I like that if I need to add another implementation, I can add it without changing the existing code, so it follows the SOLID principles.<\/p>\n\n

What about you?<\/h2>\n\n

Do you use Action classes in your code, or do you use Services or something else?<\/p>\n\n

Reply to this email and let me know.<\/p>\n\n ", "format": "full_html", "processed": "\n

When creating a custom module, where do you put your business logic?<\/p>\n\n

You want to keep classes like Controllers and Commands simple and move any reusable logic into separate files.<\/p>\n\n

Usually, this means 'Service' classes, but another approach I like is to use 'Action' classes.<\/p>\n\n

What is an Action class?<\/h2>\n\n

An Action is a PHP class representing a single action that must be performed.<\/p>\n\n

It usually contains a single method with a descriptive name summarising the task, such as GetAccessToken<\/code>.<\/p>\n\n

This differs from a generic service like ApiService<\/code> with multiple methods like getAccessToken()<\/code>.<\/p>\n\n

Using Action classes<\/h2>\n\n

I'll register Action classes in the service container to use dependency injection and autowiring and easily inject the Action into other classes that need it, like Controllers and Commands.<\/p>\n\n

If you need multiple implementations, multiple actions can implement the same Interface and make them swappable, such as having GetAccessToken<\/code> and GetAndCacheAccessToken<\/code> implement the same GetsAccessToken<\/code> interface.<\/p>\n\n

That also enables using design patterns like Decorators with Actions.<\/p>\n\n

Why I like Actions<\/h2>\n\n

I like more readable and meaningful class names and prefer working with multiple simpler classes than those with fewer complex ones.<\/p>\n\n

I like leveraging design patterns I'm used to, such as the Decorator pattern, by having common interfaces and contracts.<\/p>\n\n

I like that if I need to add another implementation, I can add it without changing the existing code, so it follows the SOLID principles.<\/p>\n\n

What about you?<\/h2>\n\n

Do you use Action classes in your code, or do you use Services or something else?<\/p>\n\n

Reply to this email and let me know.<\/p>\n\n ", "summary": null } ] }