https://github.com/joshnuss/dependency
Dependency injection for Elixir
https://github.com/joshnuss/dependency
dependency-injection elixir
Last synced: 8 months ago
JSON representation
Dependency injection for Elixir
- Host: GitHub
- URL: https://github.com/joshnuss/dependency
- Owner: joshnuss
- Created: 2018-09-18T01:14:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-18T13:34:30.000Z (almost 8 years ago)
- Last Synced: 2025-10-16T08:23:36.591Z (8 months ago)
- Topics: dependency-injection, elixir
- Language: Elixir
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dependency
Dependency injection for Elixir. [Full documentation](https://hexdocs.pm/dependency)
Dependency injection allows you to swap out dependencies when unit testing your modules.
In test mode, dependency resolution is dynamic (it uses a `Registry` lookup the current implementation).
In dev and production mode, doing a lookup would be slow and cause a bottleneck, so the dependency is compiled inline with a macro - eliminating any perormance hit.
Inspired by [constantizer](https://github.com/aaronrenner/constantizer)
## Installation
The package can be installed by adding `dependency` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:dependency, "~> 0.2.0"}
]
end
```
Also, you'll need to add the application to your list of applications
```elixir
def application do
[
# ...
extra_applications: [:dependency],
]
end
```
## Usage
### Resolving a dependency
```elixir
import Dependency
mod = Dependency.resolve(MyModule)
```
### Registering an implementation
```elixir
Dependency.register(MyModule, MyImplementation)
```
### Defining a constant
```elixir
defmodule Example do
import Dependency
# define a public constant/accesor
defconst :foo, Foo
end
# call accesor to resolve dependecy
Example.foo == Foo
```
## License
MIT