3 ms·
The above related mostly to work I did in Scala, but I am writing mostly Rust now. Each language has a way of doing things so you have to adapt what I said to h
by asimpletune 3y ago
The above related mostly to work I did in Scala, but I am writing mostly Rust now. Each language has a way of doing things so you have to adapt what I said to how it makes sense for you. Still, the process is the same in spirit.
> how do you do the equivalent of “registering dependencies”
In what I was describing earlier, dependency injection is literally just calling a function. The dependencies are arguments to a function, and you “inject” them by supplying them to the function. The “registering” is just based on your function signature. That is already a contract. Dependencies are functions too.
> where do ou setup all the functions
Usually one of two places: tests or your app’s entry point. In tests you just setup your function wherever it’s needed. Like I said if you need to test what happens when a dependency of yours does something, then just write a function that returns what you are looking to test.
For your app, this same thing is in your app’s entry point, like main. So you read from config and build your dependencies, and then at the end you ultimately pass them to a function.
At the end of the day this is about inversion of control, but it’s something that makes more sense if you invent it on your own from scratch before trying to understand what that means through more complicated abstractions.