Hey, injecting dependencies with Procs can be super handy.
Pros:
- Code editor navigation to a dependency class will work
- Application code will be simplified, as you don’t need to put every service class to the “container”
- You don’t need to teach all newbies what is a “DI Container” and how dependencies are resolved
- Just because DI is a simple pattern that must work as is, without any dependencies to other patterns
Example:
class MyClass
include DI[
mailer: proc { SomeMailer.new }, # uses Proc
logger: proc { MyLogger }, # uses Proc
ab_tests: 'app.ab_tests', # uses container resolver
]
end
It should be relatively simple to implement, just changing
container[identifier]
# to
identifier.is_a?(Proc) identifier.call : container[identifier]
I would like to add some Pull Request if this idea will be accepted