Re:Printf(“%s %s”, dependency, injection)
by jppittma on Tuesday October 19, 2021 @05:55AM
(#28916481)
I’ll hold the rescale image function as a function pointer and only call vía that pointer when I want to rescale. If you give me a new one via a plug-in or dll, I’ll set the value of the rescale function to your function.
Re:Printf(“%s %s”, dependency, injection)
by jcelerier on Tuesday October 19, 2021 @08:22AM
(#28917301)
Yes, and you are going to store the function pointers somewhere in your code right ? To show a GUI menu to your users with all the available plugins
Re:Printf(“%s %s”, dependency, injection)
by im3w1l on Tuesday October 19, 2021 @05:51AM
(#28916454)
The old school way of doing that is to have a "plugins" folder. Every library there is loaded. It calls a register-library function and provides a struct full of callbacks (for image rescaling or memory allocation in this case). The plugin directly calls memory allocation routines of the host, which delegates to the one selected in the ui.
Re:Printf(“%s %s”, dependency, injection)
by jcelerier on Tuesday October 19, 2021 @08:21AM
(#28917291)
> Every library there is loaded. It calls a register-library function and provides a struct full of callbacks
Congrats on reinventing DI ! Guess what your array of struct reacale_plugin* is called, hint, it starts with DI and ends with container
Re:Printf(“%s %s”, dependency, injection)
by matheusmoreira on Tuesday October 19, 2021 @09:08AM
(#28917651)
I'm not sure anyone is reinventing anything though. This function pointer stuff is so simple I don't think anyone ever bothered to give it a name before. Even the Linux kernel is full of this stuff, kernel modules are literally this and I've never read a single comment talking about dependency injection.
People invented this because Java and its friends made it so insanely hard to do it that you needed to invent convoluted techniques to accomplish the same task.
Re:Printf(“%s %s”, dependency, injection)
by jcelerier on Tuesday October 19, 2021 @10:39AM
(#28918591)
> Even the Linux kernel is full of this stuff, kernel modules are literally this and I've never read a single comment talking about dependency injection.
here's one: https://stackoverflow.com/questions/5925270/how-to-do-depend...
Re:Printf(“%s %s”, dependency, injection)
by matheusmoreira on Tuesday October 19, 2021 @01:00PM
(#28920438)
That's a stackoverflow question, not Linux kernel source code. Looks like everyone there is realizing the fact dependency injection is just a simple technique that's been around for a long time. Just like I said. Mocks? Just provide a different function. It's easy.
Re:Printf(“%s %s”, dependency, injection)
by im3w1l on Tuesday October 19, 2021 @09:33AM
(#28917897)
This is unneccesarily sarcastic.
Well, naively this would sound to me like you would pass these things in as arguments?
(Alternatively, for something more Haskell specific, you can also write your stuff generically against a typeclass, and let people provide their own implementations.)
not sure it's a really good explanation. You aren't the one giving the arguments to the function, it's the framework in which you are opting into which is going to do that.
Imagine you are writing a software for, say, rescaling images. You have hard requirements:
How do you do that ? I'd wager that whatever solution you find, it will really look like DI.