What is dependency injection and why everyone want to use
-> 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 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 may be the screen of MacBook is from Samsung may be the hard drive is from Itachi may be the RAM is from Sanders so all these different company 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
-> Let's 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 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 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 figuration now there are different ways of 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 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
-> 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 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
-> 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 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.
0 comments:
Post a Comment