When to use Service vs Initializer vs Util?

The solution back in the day was usually mixins. However those have been discouraged for a number of reasons for a long time, and aren’t supported with glimmer components. If the method is more of a “pure functional” method and doesn’t mutate state you can easily put it in a util function and import that anywhere. Obviously if the method is mutating component state you wouldn’t want to keep it in a service as a service reaching into a component is pretty weird. IMO there’s nothing wrong per-se with putting things like this on a service, but it doesn’t make sense unless they’re related and it makes sense to treat them as a singleton class. In many cases that will involve global state, though I wouldn’t say it does as a rule. If they’re just methods you want to share or abstract out though a util probably makes more sense.

For another great thread about replacing mixins (which is related to this topic) see here

2 Likes