Dependency
Dependency injection for Elixir.
Dependency injection allows you to swap out dependencies when unit testing your modules.
In test mode a Registry
is used that holds a mapping between dependency name and implementation.
In dev and production mode, the dependency is compiled inline - there is no perormance hit.
Inspired by constantizer
Installation
The package can be installed by adding dependency
to your list of dependencies in mix.exs
:
def deps do
[
{:dependency, "~> 0.1.0"}
]
end
Also, you’ll need to add the application to your list of applications
def application do
[
# ...
extra_applications: [:dependency],
]
end
Usage
Resolving a dependency
import Dependency
mod = Dependency.resolve(MyModule)
Registering an implementation
Dependency.register(MyModule, MyImplementation)
License
MIT