Hacker News new | past | comments | ask | show | jobs | submit login

Given code like this:

    class RatingsController < ApplicationController
    
      def create
        success = RatesBusiness.add_rating(params[:business_id],
                                           params[:score],
                                           current_user)
        render json: {success: success}
      end
    end
Wouldn't it be better something like this (just to adapt to rails)?

    class RatingsController < ApplicationController
      logic = RatesBusiness
    end
Or, even better

    class ApplicationController < Controller
      def initialize(logic):
        // do something 
      end

      def create:
        // do something with the logic
      end
    end
And adding some routes for each logic.

But, then, you could just extract the route from the logic name (and get the rails controller back, that now it would be called "Logic")

My point is that extracting code just for the fun of it seems worthless. What is the rationale in this case?




What you're talking about looks like (is...) dependency injection. Rails is a response to the Java FactorySingletonWidgetFactoryClass culture, so the question is does DI benefit Rails, or simply allows us to pontificate about our code? Which is what DHH is arguing against.

In my experience DI has been a great pattern for writing testable code, so do you have any resources relevant to DI in Rails, and why it (DI) is more beneficial than the OPs argument of a Service based architecture? (other than a simple google search like I did?)


I am just pointing out that refactoring the code he proposed, removing duplicate code, we arrive again at the Rails controller he does not like.

I see duplicate code all over this "service based architecture".


I mostly agree, with the exception of the initializer, something I haven't seen put to much use in controllers. The main thrust of my question was, do you have any links/examples for a DI based rails controller? Other than your "in theory" refactoring?


I do have examples of similar things but I cannot disclose the code as it was written in closed applications. For instance, I am guilty of redoing poorly the java servlet API using servlets.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: