70

Does the fact that Rails have an MVC approach mean that is has dependency injection?

Or is there a reason that we don't talk about dependency injection in Rails?

If Rails does have dependency injection, what does it consist of?

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
kws
  • 813
  • 1
  • 7
  • 7

5 Answers5

24

IoC is the big hammer, but DI happens everyday in Ruby / Rails. Whenever you do:

def initialize(model_klass)
  @model_klass = model_klass
end

This is DI. This paradigm is also used in various places in Rails source code. For example, the Railties gem itself is mostly a DI Engine. You can inject your favoriate ORM, various plugin configs, and generators.

Dependency Injection has a big and scary name, but what it boils down to is just decoupling class dependencies by ways of injecting the dependencies during runtime.

It doesn't matter what language you use, as long as you need to plug behavior / code in somewhere, you are probably using it.

Aaron Qian
  • 4,477
  • 2
  • 24
  • 27
15

Dependency Injection is a paradigm, so it exists in every object-oriented language.

Whether there are DI frameworks for Ruby - check this question

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

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

John Topley
  • 113,588
  • 46
  • 195
  • 237
  • 53
    Jamis was really saying that there's no need for the heavy-handed DI frameworks frequently in use with Java. He didn't say that it's unnecessary in Ruby - to the contrary, DI can be used in Ruby to reduce coupling and improve testability. It's just not AS necessary. – scottburton11 Feb 03 '10 at 18:40
  • 13
    The author of the blog says they _do_ use DI every day, just not a DI framework. – Jacob Groundwater Apr 18 '13 at 21:14
  • 1
    That was an awesome article. Must read for everyone using Ruby. – Flavia Jan 29 '14 at 17:59
  • 10
    downvoted. dependency injection is a best practice in ruby. It allows for better testability and decoupling as scottburton11 suggested. I suggest reading [Practical Object-Oriented Design in Ruby: An Agile Primer](http://amzn.to/1CTLDO1) By Sandi Metz who's considered a guru on the subject. – Dorian Feb 20 '15 at 22:03
  • 1
    I am pretty sure he was talking about DI frameworks are unnecessary in ruby. DI is a practice – Broncha Oct 28 '16 at 14:38
0

I'd say that you don't need such a thing with ruby... but if you really want to, some people have workarounds.

marcgg
  • 65,020
  • 52
  • 178
  • 231
0

I use this IoC https://github.com/alexeypetrushin/micon in my Web Framework, most of time it stays hidden and silently solves issues of dependencies and component initializtion that otherwise should be solved manually.

You can see it in action here http://ruby-lang.info (this site powered with Rad, my web framework https://github.com/alexeypetrushin/rad_core ).

Alex Craft
  • 13,598
  • 11
  • 69
  • 133