DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

Service Class by PORO

👍 Idea

Don't make it just a process
Think about encapsalation

  • Naming
    • Verb + Noun
  • Design
    • Decide only one method as kick point
    • ex: .run, .call, ..etc
    • One Class One Responsibility
    • Wite main process into instance method
    • because
      • thread-safe
      • easier handling than class method
    • comment should be converted to private method

👍 Recipe

class CreateDownloadFileService
  include Rails.application.routes.url_helpers

  def self.call(hoge:, fuga:)
    new(hoge: hoge, fuga: fuga).call
  end

  def initialize(hoge:, fuga:)
    @hoge = hoge
    @fuga = fuga
  end

  def call
    # do something
  end

  private
  attr_reader :hoge, :fuga, :type, :user_agent
end

class MeetingController < ApplicationController
  def file_download
    file = CreateDownloadFileService.call(hoge: session[:hoge], fuga: request.user_agent)
  end
end

🔗 Parent Note

Top comments (0)