Rails service objects vs lib classes

前端 未结 2 359
长情又很酷
长情又很酷 2021-01-30 13:33

Short version: when should I put something into app/services instead of into lib/.

I\'ve been going down the service objects route recently, but I realised that

相关标签:
2条回答
  • 2021-01-30 13:58

    This is how I tend to think about code that goes in lib/:

    • It is not coupled to my app's domain models.
    • It can be reused on other projects.
    • It can potentially become its own gem. Thus, putting it in lib/ is the first step in that direction.

    Services:

    • They tend to know a decent amount about the inner workings of domain models.
    • Perform work that is specific to business domain in my app.
    • Tend to be coupled to specific models.
    0 讨论(0)
  • 2021-01-30 14:19

    IMO, services are an abstraction of your domain. Lib stuff is for convenience classes, other stuff that doesnt directly relate to your models.

    0 讨论(0)
提交回复
热议问题