Jump to content

Talk:Dependency injection

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia


[edit]

Framework based DI Code Example

[edit]

I highly discourage anybody from using hard coded strings in-line. The example in the last section of the page would be better if we added an static class to it that contains the strings:

//static class provided by the framework or written by the user to indicate which dependencies are available
public static class MyAvailableServices{
    public static final string CarBuilderService = "CarBuilderService";
    public static final string VanBuilderService = "VanBuilderService";
}

public class MyApplication {
    public static void main(String[] args) {
        Service service = (Service)DependencyManager.get(MyAvailableServices.CarBuilderService);
        Car car = (Car)service.getService(Car.class);
        car.setPedalPressure(5);
        float speed = car.getSpeedInMPH();
    }
}
Strings are only used in code because they are easy to read. Using enums is much better and type safe to book. 194.207.86.26 (talk) 07:41, 22 December 2021 (UTC)[reply]

Registry Pattern

[edit]

Should not this article reference the Registry Pattern which was very prevalent until IoC/DI was popularised?

Needlessly Object Oriented

[edit]

Isn't the description needlessly presuming an object oriented approach? Dependency injection is just as easily performed in a classes programming language. It's the basis of how C's std library qsort operates:

   int cmpfunc (const void * a, const void * b) {
     return ( someMathWithAandB);
   }
   qsort(array, size, sizeof(int), cmpfunc);

— Preceding unsigned comment added by 97.122.84.35 (talk) 06:55, 28 November 2020 (UTC)[reply]

Classes programming is usually OO too. Also a program could be designed with OO principles but implemented with an non-OO language. eg:
#include <iostream>
#include <algorithm>
#include <vector>

using array_t = std::vector<int>;

struct data {
	array_t array;
};
struct fns {
	int (*cmpfunc) (const void * a, const void * b) = 0;
};

int cmpint(const void * a, const void * b){
	int i {*reinterpret_cast<int const *>(a)};
	int j {*reinterpret_cast<int const *>(b)};
	return  i-j;
}

void qsort(data& data, fns const& f){
	std::sort(std::begin(data.array), std::end(data.array), [f](auto a, auto b){
		return f.cmpfunc(&a, &b) < 0;
	});
}

int main() {
	data data;
	data.array = array_t {4, 6, 2};
	fns fns;
	fns.cmpfunc = cmpint;
	
	qsort(data, fns);

	for(int i: data.array){
		std::cout << i << '\n';
	}
}

194.207.86.26 (talk) 08:57, 22 December 2021 (UTC)[reply]

Dependency Injection is a fancy term for reference passing from the OOP world. The term Dependency Injection simply doesn't get much usage outside of OOP. Likely because the functional world already knows how to use composition.

Galhalee (talk) 04:00, 20 August 2022 (UTC)[reply]

It's not OOP; I've never heard of "injection" from within the OOP discipline, and I've been working with C++, Java, UML and design patterns for a long time. It is only since I've been working with server-side Java "Servlet" systems and browser-side Javascript AngularJS coding that I have heard of "injection". It's a bunch of hype intended to sound esoteric. I agree with earlier comment that it is nothing more/less than reference passing during startup. "Injection" is a silly term for this, especially since it is only a startup function. The word "injection" sounds like it should be something that could happen asynchronously, more than once, preemptively.