{"id":17956040,"url":"https://github.com/sleipnir/injectx","last_synced_at":"2026-03-01T05:34:48.095Z","repository":{"id":48274413,"uuid":"389164730","full_name":"sleipnir/injectx","owner":"sleipnir","description":"Elixir dependency injection","archived":false,"fork":false,"pushed_at":"2021-08-04T14:06:53.000Z","size":52,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-20T08:54:34.715Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sleipnir.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-24T17:49:43.000Z","updated_at":"2024-08-20T18:38:15.000Z","dependencies_parsed_at":"2022-07-24T22:02:46.371Z","dependency_job_id":null,"html_url":"https://github.com/sleipnir/injectx","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sleipnir/injectx","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Finjectx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Finjectx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Finjectx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Finjectx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sleipnir","download_url":"https://codeload.github.com/sleipnir/injectx/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sleipnir%2Finjectx/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29960436,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-01T01:47:18.291Z","status":"online","status_checked_at":"2026-03-01T02:00:07.437Z","response_time":124,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T10:34:18.371Z","updated_at":"2026-03-01T05:34:48.071Z","avatar_url":"https://github.com/sleipnir.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Injectx\n\n\u003c!-- MDOC !--\u003e\n\n**Context Dependency Injection for Elixir**\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `injectx` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:injectx, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n`Injectx` module is a entrypoint to CDI.\n\n## Usage:\n\nDefine your Behavior module:\n\n```elixir\ndefmodule FooBehavior do\n  @callback greetings(String.t()) :: String.t()\nend\n```\n\nImplement it:\n\n```elixir\ndefmodule FooImpl do\n  @behavior FooBehavior\n\n  @impl true\n  def greetings(_name) do\n    \"Hello from FooImpl\"\n  end\nend\n```\n\nInitialize Injectx Context on Application bootstrap:\n\n```elixir\ndefmodule App do\n  use Application\n\n  alias Injectx.Context\n\n  @impl true\n  def start(_type, _args) do\n    context = %Context{\n      bindings: [\n        %Context.Binding{\n          behavior: FooBehavior,\n          definitions: [\n            %Context.BindingDefinition{module: FooImpl, default: true}\n          ]\n        }\n      ]\n    }\n\n    Context.from(context)\n\n    children = [\n      ...\n    ]\n\n    opts = [strategy: :one_for_one, name: App.Supervisor]\n    Supervisor.start_link(children, opts)\n  end\nend\n```\n\n### Use your behavior via Implementation resolved in runtime (using inject macro with same sintaxe of alias):\n\n```elixir\ndefmodule Bar do\n  use Injectx\n\n  inject TestBehaviour\n\n  def greetings(name), do: TestBehaviour.greetings(name)\nend\n```\n\n### Or you can inject all implementations at once (using inject_all function):\n\n```elixir\ndefmodule Caller do\n  use Injectx\n\n  # resolve all injection bindings for certain behavior\n  @all inject_all(TestBehaviour)\n\n....\n  def call(), \n    do: Enum.each(@all, fn impl -\u003e impl.greetings(\"Teddy\") end)\nend\n```\n\n## Dispatching\n\nInjectx also provides the ability to dynamically dispatch your implementations.\nFor this it is only necessary to use the dispatcher function. Sync and Async are possible options:\n\n```elixir\ndefmodule SomeBehaviour do\n    @callback test(integer()) :: {:ok, integer()}\nend\n\ndefmodule SomeImpl1 do\n  @behaviour SomeBehaviour\n\n  def test(1), do: {:ok, 1}\nend\n\ndefmodule SomeImpl2 do\n  @behaviour SomeBehaviour\n\n  def test(1), do: {:ok, 2}\nend\n\n... bootstrap\n  @impl true\n  def start(_type, _args) do\n    context = %Context{\n      bindings: [\n        %Context.Binding{\n          behavior: SomeBehaviour,\n          definitions: [\n            %Context.BindingDefinition{module: SomeImpl1, default: true},\n            %Context.BindingDefinition{module: SomeImpl2}\n          ]\n        }\n      ]\n    }\n\n    Context.from(context)\n    ....\n  end\n\n...write some client module...\n\ndefmodule SomeClientModule do\n  use Injectx\n\n  def call(arg), do: dispatching(TestBehaviour, :test, [arg], async: true)\n   \nend\n\n...then call it `iex -S mix`\n\niex(1)\u003e SomeClientModule.call(1)\n[{:ok, InjectxTest.TestImpl2, {:ok, 2}}, {:ok, InjectxTest.TestImpl1, {:ok, 1}}]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleipnir%2Finjectx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsleipnir%2Finjectx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsleipnir%2Finjectx/lists"}