Wondering if anybody found a good way to implement dependency injection in Julia. I have many situations where I use “adapters”: take for instance a
Database module which uses various adapters (MySQL, SQLite, Postgres, etc).
And what I usually end up with is:
constin the module global scope which references the adapter
- a lot of
evalstatements in the line of:
if haskey(SearchLight.config.db_config_settings, "adapter") && SearchLight.config.db_config_settings["adapter"] != nothing db_adapter = Symbol(SearchLight.config.db_config_settings["adapter"] * "DatabaseAdapter") include("database_adapters/$(db_adapter).jl") Core.eval(@__MODULE__, :(using .$db_adapter)) Core.eval(@__MODULE__, :(const DatabaseAdapter = $db_adapter)) Core.eval(@__MODULE__, :(export DatabaseAdapter)) else const DatabaseAdapter = Nothing end
Looks like a code smell to me.
I think a nice solution would be to have a mechanism to pass arguments when
using a module, in the line of:
using Database(adapter = MySQLAdapter)