bryanhuntesl

bryanhuntesl

Dependency injection/IOC

A popular Java and C# design pattern is runtime dependency infection.
People tend to associate it with Object Oriented programming but there is nothing inherent to OOP.

Runtime dependency injection/resolution can work for any system that provides a runtime mechanism to override or modify the version of a class or module which has been requested.

It strikes me that Mox provides a limited form of dependency injection in that sense.

Now I’m not advocating for it, but just for the sake of idle discussion (and I do know about Application.env and regular configuration stuff) has anyone ever attempted bringing Java style DI to Elixir?

Most Liked

paulstatezny

paulstatezny

I was a fan of dependency injection containers when I was working with object-oriented web frameworks.

I was too, until I spent years of my life diligently writing unit tests, only to realize years later that those tests proved very little about the codebase.

Dependency injection is evangelized because it makes it easier to unit test with mocks.

Integration tests are vital. Unit tests are mostly useful for pure functions. Mocks lead to false confidence about the test suite.

When I came to Elixirland and learned to write mostly integration tests, I was blown away by the amount of real bugs the test suite caught.

hauleth

hauleth

I just pass modules as a function arguments where needed. In other cases I just do not bother at all.

mudasobwa

mudasobwa

Creator of Cure

Usually, we have a Client behaviour here, declaring a type, and refine the spec to

@typet opts [{:client, Client.t()} | {atom(), any()}]
@spec client_get(String.t(), opts()) :: {:ok, String.t()} | {:error, any()}

Last Post!

kenny-evitt

kenny-evitt

You can do it anywhere. It’s a pretty helpful pattern for, e.g. testing API clients – use the ‘web client’ for production/staging (and maybe dev too), and a ‘test client’ for (unit) tests.

A functionally similar kind of thing is to retrieve a module name from config during compilation (by storing it in a module attribute). That’s a little less flexible than just passing a module as an argument.

Where Next?

Popular in Discussions Top

ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
PragTob
Hey everyone, this has been brewing in my head some time and it came up again while reading Adopting Elixir. GenServers, supervisors et...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

Other popular topics Top

joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement