#What is Dependency injection?
1 messages · Page 1 of 1 (latest)
<@&1004656351647117403> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
nah
dependency injection is just this
class Thing {
void saveToDB(String name);
}
class RequestHandler {
void handleRequest(Request request) {
var thing = new Thing();
thing.saveToDB(f(request));
}
}
class Thing {
void saveToDB(String name);
}
class RequestHandler {
void handleRequest(Thing thing, Request request) {
thing.saveToDB(f(request));
}
}
class Thing {
void saveToDB(String name);
}
class RequestHandler {
Thing thing;
RequestHandler(Thing thing) {
this.thing = thing;
}
void handleRequest(Request request) {
thing.saveToDB(f(request));
}
}
do the 2nd or the 3rd
not the 1st
dependency injection frameworks "magically" pass arguments
but you don't need them
DI is "injecting dependencies". If an object requires things to work properly, you should inject them (pass them in).
Instead of java class SomeObject { Dependency dep = new Dependency(); }You should be doing ```java
class SomeObject {
Dependency dep;
SomeObject(Dependency dep) {
this.dep = dep;
}
}```
dependency injection has nothing to do with ur project dependencies (jars, ...)
its about class dependencies
so the classic constructor/method parameters
"in order to work, i need the database, 5 services and this api class"
so instead of filling ur code with all the bloat necessary to ping pong around ur instances, dependency injection provides a very controlled mechanism of doing that out of the box automatically
and it typically also allows u to easily modify the process
for example if u want to use different instances when running on the DEV environment than when running on PROD
so u dont have to write hundreds of if-else and whatever
it just works
It's magic
Hello dependency injection is a sort of pattern which gives several advantages like loose coupling between components. With the help of framework you can instantiate an object that holds some values only once, and then call it wherever you like without the need to declare it many times.
In a real world, considee an Smartphone object that depends on OperatingSystem.
But operating system can vary in term of behavior, which means it could have multiple implementations. So we can declare it once with expected behavior
OperatingSystem os = new AndroidOs();
os.prepareGoogleAccoount();
//..
and do not need to write it inside the class(es) where we need it, just inject it.
SmartPhone sp = new SmartPhone(os);
This is the Smartphone class code:
public class Smartphone {
private final OperatingSystem os;
public Smartphone(OperatingSystem os){
this.os = os;
}
}
In this code, we do constructor injection. SmartPhone requires an object OperatingSystem but doesn't care about what it is exactly, it just accept it is a valid implementation
Detected code, here are some useful tools:
I think thats conflating dependency injection with dependency injection frameworks
although maybe "injection without extra conditional boilerplate per injection site" is something worth having its own term. There are more ways to do it in other langs that aren't really represented in Java
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍