What is Dependency Injection? | Why | Spring

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is Devin 20 and in this video we'll talk about dependency injection now if you are working on a project let's say if you are working on some advanced project of course you will be using some design patterns and one of the design patterns which we come across is dependency injection doesn't matter which language we use maybe Java c-sharp PHP we have this concept there but what is so important so in fact we have this questions right what is dependency injection and why everyone want to use it in fact if you talk to some expert in industry people who are working from a long time they use this concept by default or not for them it's like a normal thing but for new developers it's a new thing right for them you need to understand how what is dependency injection and why to use it now how to use it that we'll see in the practical but then how do you why do you use it that's a question now think about this now when you say dependency in in software programming software what we do is we build good right we build objects if you are working with object-oriented programming so we build objects right and those objects are dependent some on some other objects so we create this object graph right so a one object is depend upon some other object and that object depends on what that object to give you an example in real life let's say if you want to buy a laptop if you want to build a laptop let's say you are a laptop manufacturer now in this case in laptop you have certain certain parts right we have a ram you have a hard drive you have a screen and of course what makes a good laptop is good parts right example if you buy a laptop from any company the same company they have a low range laptop and a high range laptop what changes is the type of hard drive we use the type of RAM we use so of course all these components are not built by the same company so let's say if you buy a Apple machine of course Apple will not be building all the step by themselves they will be buying it from some other companies maybe the screen of MacBook is from Samsung maybe the hard drive is from Itachi maybe the RAM is from Sanders so all these different company they they he'll they help one company to build a project or build a machine now the same way if you want to build a project and if you want to let's say if you have an object and that option is depending on some other object in this case you will not be building all this stuff by yourself of course you will be having classes for that right so in Java what we do is we first grade class and then we create object now to get an example let's say we have a class here which is class let's a laptop and in the inside class laptop we need a object of hard drive we need her object of RAM now how will it do it of course you will be saying new right and when you say hard drive of course you need a point class right and that lets say we have hitachi hard drive now let's say if you build a MacBook with Hitachi hard drive what if in future you want to change it because we want to achieve this concept of loose coupling right I am sure you know about this concept of tight coupling and loose coupling that's what we have learned in software engineering when you say loose coupling it means one optic is not totally depend up on other object it may get replaced example if you have with a hard drive in future you might want to use Samsung hard drive it should pass it it should be possible right and that's why we use this concept of abstraction what we do is we say ok for each class let's say Hitachi hard drive let's create abstract class or the interface for less hard drive so when you instantiate it it will be something like hard drive ABC or hard drive obj equal to new Hitachi hard drive so that in future you can change it from Hitachi hard drive to Samsung hard drive it should be that easy but there's a question here we are hard coding it right we are saying hey we I'm going for a new with a hard drive and that's a bad thing because the moment you say hard coding the moment you say new you are going for tight coupling that's that's what we don't want so we want someone else to give me those dependency or that's important down here so the laptop object is dependent on the hard drive object that's a dependency so we want to inject the hard drive object inside this inside this laptop class and the way you can do that is by using some service which will be external service they will inject the dependency okay how it is possible so we have this concept of dependency injection containers now they are responsible to create a object for you of course the object has to be created so they are responsible to create the object for you and then they will be injecting in your class okay but we're to mention that I want to inject and how it will create the object and that's where we have to do porn figuration now there are different ways of configuration configuring it example if you are using Java in Java we have Spring Framework and in Spring Framework we need to do so a lot of configuration but earlier days we used to work with Spring Framework ways to configure everything using XML okay so that's that's that's how you can configure so create XML file and in the next sample file you can mention hey if someone is asking for hard drive give this object okay now when you say XML it means you can edit XML in future right so that is not a tight coupled okay but still as a jar of Rama you don't want to focus more on XML and that's where in we have spring boot in Java using which what you can do is just we will be having some class as hatachi hard drive right or Samsung hard drive so on top of these classes you will simply write add component that's it okay so you simply like add component which makes them dependent which makes them a component of a spring framework which will be generated as per requirement okay so those those object are ready with Spring Framework but what about this class how will this mention that you want this object so on top of your hard drive what you can say is you can say Auto wired okay let's say your Spring Framework says or your spring boot with the help of Spring Framework says ok here someone is asking for hard drive ok so this class needs a hard drive and I do have a component there so I can connect them like that subtype so Auto wired and just imagine so beautiful they are getting connected again the implementation you will say later but this is this is the main idea behind dependency injection right but why we need this is it only because of tight coupling the answer is yes we don't want to achieve tight coupling we want loose coupling plus there is one more important factor which is testing you need to test your software right now we always test our software as a whole but sometime we need to not sometimes always we should all we should also be testing your each component each unit now the thing is let's say if you want to test a laptop and laughed when you said when you test a laptop you should also test the hard drive but don't you think when you buy a hard drive like when Apple bought hard drive from Samsung they must have tested that hard drive right why you need to test the hard drive again and again so what you want is when you are testing a laptop you want to separate the hard drive right so in in fact in Jogja world what you can think is you have a plus and that class is working with a database object now you are testing this class right you don't want to test the database object so you can create a mock object of this of this class only if they are loosely coupled so if you have a mock object you can easily test this class without affecting your database okay and to achieve that feature you have to make sure that your software is loosely coupled and that's why we have so many words right we have loosely coupled we have dependency injection so so that's that's so amazing about a pencil injection you just need to implement it so that you can test it better you can maintain it better and how it exactly works that we'll see in the practical video I hope you enjoyed this thing let me know in the comment section your thoughts and do click on like button if you enjoyed it thanks for watching everyone
Info
Channel: Telusko
Views: 391,966
Rating: 4.8739653 out of 5
Keywords: telusko, navin, reddy, tutorial, java, Spring, DI, dependency injection, why, example, object, dependency, loose, tight, coupling, container, spring container, spring boot, component, autowired, @autowired, testing, mock object, maintenance
Id: Eqi-hYX50MI
Channel Id: undefined
Length: 8min 17sec (497 seconds)
Published: Wed Jun 06 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.