Gigitsu

Gigitsu

Hi everyone!

I’d like to share a small library I’ve recently extracted from a project I’m working on: ginject.

ginject provides a minimal global injection mechanism, letting you register and inject services without plumbing extra configuration throughout your application.

Why this exists

In the app I’m developing, I needed a pragmatic way to inject alternative behaviors mainly for testing purposes.
Mox and behaviours are great — but switching implementations in test environments often requires more setup than necessary for small or isolated cases.
ginject aims to keep things lightweight and comfortable.

Part of a bigger effort

This is the first of some tools I plan to extract and publish from this project.
There are a couple more utilities in the pipeline — once cleaned up and documented — that I hope will also be useful to share with the community.

https://github.com/Gigitsu/ginject

Thanks for checking it out — and I’d love to hear any feedback!

Showing Posts 1 to 6

Asd

Asd

What do you mean? Without ginject

  @ms Application.compile_env(:app, [__MODULE__, :mapset_module], MapSet)

  def new_mapset(x) do
    @ms.new(x)
  end

And test config like

config :app, MyModule,
  mapset_module: TestMapSet

But with ginject

  use Ginject
  service MapSet, as: MS

  def new_mapset(x) do
    MS.new(x)
  end

And

config :ginject, Ginject.DI,
  strategy: Ginject.Strategy.BehaviourAsDefault,
  services: [
    {MyModule, [
      %{service: MapSet, impl: TestMapSet}
    ]}
  ]

What’s the difference?

Gigitsu

Gigitsu OP

With ginject, in most cases you only need two lines of configuration:

# config.exs
config :ginject, Ginject.DI, strategy: Ginject.Strategy.BehaviourAsDefault

and in test:

# test.exs
config :ginject, Ginject.DI, strategy: Ginject.Strategy.Mox

The BehaviourAsDefault strategy uses the behaviour’s own module implementation as the default service.

For example, a typical service in my project looks like this:

defmodule MyProject.ServiceA do
  @callback foo() :: any()

  @behaviour __MODULE__

  @impl true
  def foo do
    ...
  end
end

You only need to configure BehaviourAsDefault like:

services: [
  {MyModule, [
    %{service: MapSet, impl: TestMapSet}
  ]}
]

if you want a custom override for a specific service used by MyModule.

If you prefer to separate behaviour definitions from implementations, you can also define your own strategy, for example based on naming conventions.

Testing

In the test environment, the Mox strategy automatically creates mocks for you, and you can set expectations directly on them.
You can see an example here


Without ginject, you typically need to define configuration entries like:

config :app, MyModule, mapset_module: TestMapSet

for every module and every injected service.
This grows quickly in large codebases, and you must duplicate these configs in both config.exs and test.exs, plus define mock/test implementations somewhere.


I also find

service MapSet, as: MS

much easier to read and write than:

@ms Application.compile_env(:app, [__MODULE__, :mapset_module], MapSet)

and then calling:

MS.new(x)

versus:

@ms.new(x)

Additionally (though I’m not 100% certain), using module attributes may cause you to lose autocompletion and jump-to-definition features in editors.

Edit:

I realize the docs could be clearer and needs some more refinement - I’m working on it!

funboy

funboy

Nice!
Could u point out the differences between Ginject and Mimic lib? :thinking:

Gigitsu

Gigitsu OP

I didn’t know the mimic library. Reading the docs, it seems to be a mocking library similar to Mox or Hammox. It seems capable of swapping a module’s implementation with a mocked one during tests.

ginject, on the other hand, is a dependency injection library. It allows you to choose a service implementation based on any criteria, one of which could be the environment (test/dev/prod).

Asd

Asd

Oh, so your library just calls Mox.defmock automatically and provides syntax sugar, I see, that’s a nice thing.

I have these questions and notes

  1. I think that service and some other macros won’t work with erlang style module names like :something. This is a quick thing to fix
  2. I think that name service for the macro is somewhat misleading, since injected modules can be not only services. Calling it a “service” is an inheritance of original DI practice from OO languages, and I think it’s the bad one. I’d just call it a dependency or I’d call a macro inject
  3. If di configuration is changed, and user calls recompile I suspect that the modules using this di configuration won’t be recompiled, cause you don’t use compile_env and you just use get_env during compile time. This can be very frustrating for someone who debugs their tests and is used to compile_env’s behavior
  4. Is it possible to make some module which implements two or more behaviours with ginject?
  5. Do you plan to support other strategies aside from Mox?
Gigitsu

Gigitsu OP

That applies only to the Mox strategy. The library itself provides dependency resolution and injection functionality, independently of Mox.

Yes. I’m considering one based on naming conventions. Other ideas are definitely welcome.

I’m not entirely sure I understand. Could you elaborate a bit more on this?

Good point, thanks for catching that. I’ll fix it as soon as possible.

As for the macro name, I like inject, but I want to think a bit more about this.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews