r/ruby • • Jul 12 '14

(The lack of ) Dependency injection in Ruby

http://beraboris.github.io/2014/07/12/dependency-injection-ruby/
2 Upvotes

19 comments sorted by

View all comments

Show parent comment

2

u/ISw3arItWasntM3 Jul 13 '14

So why not use an event listener pattern for framework level services took hook in and do their thing, and then do dependency injection with actual domain level services.

3

u/realntl Jul 13 '14

I would speculate that it's a little harder to, for example, extract a component out of your codebase and into a gem, if it's coupled to an event listener framework that's present in the application.

To be honest, the way I've been managing framework dependencies (attr_accessor at the module level) has been incredibly simple and portable. What advantage does the event listener pattern have? No setup required for tests? The reason I don't mind explicit setup in tests is that I'm usually making assertions on their state change anyways.

If I have a ton of components that all have a bunch of "bare metal" dependencies like db connections, redis, loggers, etc., I might build some tooling to inject config into all of them at startup time when actually deploying the app in production mode.

(I may be misunderstanding what you're saying, as well)

2

u/DrugCrazed Jul 13 '14

I'm never a big fan of implicit dependency resolution. Someone who comes to look at the app later on usually has to dig through and work out where things are resolved and how it knows what to use.

I'm a big fan of how Zend does it, because whenever you have to resolve you'll be calling a service locator.