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!
Trending in Announcing
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Asd
What do you mean? Without
ginjectAnd test config like
But with ginject
And
What’s the difference?
Gigitsu
With
ginject, in most cases you only need two lines of configuration:and in test:
The
BehaviourAsDefaultstrategy uses the behaviour’s own module implementation as the default service.For example, a typical service in my project looks like this:
You only need to configure
BehaviourAsDefaultlike: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
Moxstrategy 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:for every module and every injected service.
This grows quickly in large codebases, and you must duplicate these configs in both
config.exsandtest.exs, plus define mock/test implementations somewhere.I also find
much easier to read and write than:
and then calling:
versus:
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
Nice!
Could u point out the differences between Ginject and Mimic lib?
Gigitsu
I didn’t know the
mimiclibrary. 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
Oh, so your library just calls
Mox.defmockautomatically and provides syntax sugar, I see, that’s a nice thing.I have these questions and notes
serviceand some other macros won’t work with erlang style module names like:something. This is a quick thing to fixservicefor 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 adependencyor I’d call a macroinjectrecompileI suspect that the modules using this di configuration won’t be recompiled, cause you don’t usecompile_envand you just useget_envduring compile time. This can be very frustrating for someone who debugs their tests and is used tocompile_env’s behaviorMox?Gigitsu
That applies only to the
Moxstrategy. 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.