Purpose of Dependency Injection
In this example what's the benefit of DI here. I can just create instances of
EmailService
and MessagingService
class and call the SendMessage
method on them without having to create an extra instance of the NotificationService
class.
I asked AI then it just mentioned:
1. some theoretical information about how DI has loose coupling and all but couldn't prove any benefit in the code.
2. how I can independently test the NotificationService
but I can just independently test MessagingService
& EmailService
.
3. how adding features to the MessagingService
& EmailService
can cause problems but couldn't show me how it can cause problems in the code.9 Replies
This is a pretty poor example of DI if you ask me
the core idea is that you pass dependencies in (usually via the constructor) to things, instead of letting them create them for itself
compare and contrast these two
this is what "inversion of control" is all about
instead of the service creating and managing its own dependencies, they are passed in
with DI, we manage these dependencies and their dependencies etc with a DI container/provider
Note that if you only have one implementation of a dependency, you don't have to abstract it into an interface unnecessarily. Just inject the implementation as is. Use interfaces if you need to, e.g. testing/mocking, actually having multiple implementations etc.
@Pobiega & @SleepWellPupper thanks! That helps. What real-world problem would make me feel the need for DI? (because I still feel a lack of clarity in my knowledge which I believe would be met once I solve a real problem with DI)
honestly, just building any non-trivial app while being aware of the problem it solves
you see, the most common problem you face with the
NoDI
type code I pasted above is.. how do you deal with shared lifetimes? what if I have a ServiceOne that I need to re-use, but I want ServiceTwo to be new every time? or worse, what if I need it to be the same for a given thing (maybe a web request lifetime?) but if a new request comes in, that should have a separate instance of that dependency
DI solves all these
like this
oh okay wait now I get it... that service one inside NoDI can't be reused to be passed to other methods
right?
exactly
and manually handling this will quickly get annoying
yea new new new new extravagant lol
yeah, and if you ever decide to change your mind, you now need to rewrite half the app D:
but with DI, you just change a single line of code
Cool
Thanks a lot again!