Rails 服务对象与 lib 类 [英] Rails service objects vs lib classes
问题描述
简短版本:我什么时候应该将某些内容放入应用程序/服务而不是 lib/中.
我最近一直在走服务对象路线,但我意识到有时我无法真正决定天气我应该将某些内容提取到服务对象(我在应用程序/服务中)或我的类中lib文件夹.
I've been going down the service objects route recently, but I realised that sometimes I can't really decide weather I should extract something into a service object (which I have in app/services) or into a class in my lib folder.
我通常寻找的一件事是,如果类需要访问参数或其他特定于控制器的东西(即作为初始化程序中的参数),那么我倾向于将其放入服务中.
One thing I usually look for is if the class needs access to params or other controller-specific things (i.e. as a param in the initializer), then I tend to put it into services.
这里的约定是什么?证明(链接)会很好:)
What is the convention here? Proof (links) would be nice :)
推荐答案
这是我对 lib/
中代码的看法:
This is how I tend to think about code that goes in lib/
:
- 它没有与我应用的域模型耦合.
- 可以在其他项目中重复使用.
- 它有可能成为自己的宝石.因此,将它放入
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.
服务:
- 他们往往对领域模型的内部工作原理了如指掌.
- 在我的应用中执行特定于业务领域的工作.
- 倾向于与特定模型耦合.
这篇关于Rails 服务对象与 lib 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!