An open API service indexing awesome lists of open source software.

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

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