14 comments
Thank you for this tutorial. I have been finding it difficult to understand these concepts. I have even read tons of tutorials including Trailblazer framework, but your explanation seems to be very explanatory. Do you have other resources to recommend so that I can fully grasp this service-oriented architecture in rails?
Thanks for your comment ๐
I haven’t done anything with SOA so I can’t recommend any resources for that. Good luck with your search!
Thanks for your effort, very useful article, keep going
Thank you ๐
Hello Jesus,
Thank you for nice article. I’m surprised that you use modules to create service objects.
I’ve learned that it should be a class. Another thing is that service objects call standardized methods like “call”, “execute”, “run”.(depends of project or organization standard). In my opinion service object should look something like this:
https://gist.github.com/mPanasiewicz/6c929ae289836540508d571e5d9b2892
In my opinion Service object should have single responsibility.
Hi,
Like I mention in the introduction, there are different ways to implement this, I’m not a big fan of the call
/ run
method pattern because it doesn’t help add any meaning to what you’re doing & it feels very mechanical to me.
But feel free to use whatever works for you, I’m just proposing one way to do it ๐
Thanks for your comment.
Hi Jesus,
Thanks for the tutorial. Two things I’d like to share;
1) You can use module_function
to create module functions for the named methods. That way you don’t need to call self
module TwitterService module_function def send_welcome_message(twitter_handle) client.update("@#{twitter_handle} welcome to 'Oranges & Apples', we hope you enjoy our juicy fruit!") end def client @client ||= Twitter::REST::Client.new do |config| config.consumer_key = "..." config.consumer_secret = "..." config.access_token = "..." config.access_token_secret = "..." end end end
2) There’s a great talk on Service Objects here: https://www.youtube.com/watch?v=dSiE9N_f0h0
I’ve been using the Interactor gem mentioned in the talk on a project and it’s great.
Hi David!
Thanks for sharing that talk & about module_function
, I also like extend self
to get the same result ๐
Thanks for posting this, a nice solid read. One question, what are your thoughts on creating a wrapper for the Twitter Client, rather than directly accessing it in this service?
Hi Jesus, I’m learning a lot from your short pots/videos! Gracias ๐
About the presenter pattern, I don’t like two things:
* Another object per each model – I love rails but sometimes it’s frustrating to have too many layers (objects, files and, folders) to make a simple view.
* Hidden object calls inside an abstract variable called “presenter” for all views (harder reading and refactoring)
Since we already have Helpers, why should we use them to do the presenter’s job?
Keep posting great articles! =b
- You don’t need to create a presenter for every single model or view, this is only for when complexity starts building up, you get a feel for it.
- If you’re familiar with the pattern it becomes less abstract, but I’m open to other ideas for the naming.
Hi Jรณni,
thanks for your comment!
What I like about presenters is that it makes the presentation layer feel a little bit more object-oriented, helpers alone don’t do that.
Thank you for this tutorial. I am reading your book ruby deep dive, is very good. Do you have some similiar book for Rails?
Hey Jhony,
thanks for your feedback! I’m working on an online version of a Rails book with quizes & stuff.
It’s a complex topic so I want to make sure it’s easy to understand. When I have news about it I’ll let you know.