Does Dependency Injection Exist in Rails

Does dependency injection exist in Rails?

Dependency injection is usually unnecessary with Ruby. Jamis Buck blogged extensively about the reasons why. Well worth a read.

Ruby/Rails Dependency Injection

Move initialization of Saw to the default argument.

class Builder   
def saw(saw = Saw.new(4))
@saw = saw
end

def cut_wood
Saw.saw
end
end

Builder#saw supports dependency injection now.

Remember to remove attr_reader :saw from your code because it's being overridden by your custom reader.

Technique to inject dependencies in Ruby without using DI frameworks

Looks fine to me. Classes are objects in Ruby, so dependency injection is just setting a variable at runtime. In your case, you are setting the @http_client instance variable of the AppClient class. Once that variable is accessed by the code that needs the implementation, the class you configured the application with will be returned and it will do the job as long as it responds to the correct messages.

Be careful, though. Remember that classes are singleton objects that are shared within a single Ruby process. Other code could call that accessor method and change that variable as well. This could happen at any point, not just at start up, and will affect all code that uses the implementation. The potential for conflict exists.

This makes this pattern specially unsuitable for use in libraries. Passing the implementation class as a parameter on the call site should be a more robust solution in that case.

Ruby dependency injection libraries

Jamis Buck, who wrote Copland and Needle, posted here about Needle, dependency injection and their usefulness in a Ruby world.

It's long but worth reading, but in case you want the single paragraph most relevant to your question, I'd suggest this one, from just before the end:

DI frameworks are unnecessary. In more
rigid environments, they have value.
In agile environments like Ruby, not
so much. The patterns themselves may
still be applicable, but beware of
falling into the trap of thinking you
need a special tool for everything.
Ruby is Play-Doh, remember! Let’s keep
it that way.

HTH

Ruby On Rails Service Container

After a long think through and dax -s great advice I remember how uncle bob once said that a good application is designed in a way that we could remove the code that makes the applications logic place it into another framework and it should work perfectly. So I decided that all the code that holds my applications logic is going to be in Rails lib folder and I am going to use service container for that.

After a long search I found out that Jim Weirich made a service container and since he is a common and big name in Ruby it gave me even more confidence on going with this approach here is the git-hub library https://github.com/jimweirich/dim .

It has some amazingly simple and good examples on how to use it.

I decided that in my lib folder I will make a folder for every module in my application and in there make a config folder and add a services.rb file where I register each new service.

Thank You for all the great advices!

Can I realize dependency injection in my RSpec tests?

Finally I solved my problem using lambda:

RSpec.shared_examples "changing status" do |arguments|
/* ... */

it_behaves_like "action requiring proper user logged in to be successful", action, -> {
before(:each) do
/* ... */
end
context "and when the reservation has an allowed status" do
/* ... */
end
context "and when the reservation has a not allowed status" do
/* ... */
end
}
end
RSpec.shared_examples "action requiring proper user logged in to be successful" do |action, successful_behavior|
context "- when the required logged user is logged in" do
successful_behavior.()
end
context "- when the logged in user is not the required logged user" do
before(:each) do
login(incidental_user)
end
it_behaves_like "unsuccessful attempt to change the reservation", action
end
context "- when there's no user logged in" do
it_behaves_like "unsuccessful attempt to change the reservation", action
end
end


Related Topics



Leave a reply



Submit