Dependency Injection: coding tutorial LIVE

preview_player
Показать описание
Another addition to the Software Engineering 101 series.
Why and how is dependency injection helpful for clean code?

Become a channel member!

You can follow me on:

#DependencyInjection #SoftwareDevelopment #Onboarding
Рекомендации по теме
Комментарии
Автор

It took years for me to wrap my head around DI. Here are my collection notes(following are different definitions):
1. DI is the technique of providing (read it as injecting, imagine an injection syringe that has objects needed for another class alias object)
2. Objects are dependent on each other. But instead of object A go in search of object B, object C, object D, all those dependencies are injected to your primary object (Think of it as wedding services. For wedding, you need to catergin services which depends on vegetables, rice etc. Wedding depends on flower services. Now instead of you go in search of each object, they all come to the primary object Wedding )
3. Decoupling the class construction from the construction of its dependencies (Now read my example in 2, it will make more sense)
Dependency Injection is one way to achieve Inversion of Control (IoC). [ I still need to understand IoC ]. Gaurav could you help here.
Then what is IoC? Moving creation of dependent objects from inside a class to the outside, is called Inversion of Control.

DI is a 25 dollar term for a 5 cent concept.

imsivanesan
Автор

Good one.. joined in last 10 mins. It will be helpful if this video is uploaded.

shaileshagarwal
Автор

Thanks a lot. It's really a nice explanation. You are always my favourite.

diptiprakashsinha
Автор

Another great video Gaurav! This was one super clear, and clearly demonstrates the use of dependency injection!

StyleTrick
Автор

This is a good video on a topic that is used more widely than it is understood. Thanks for coming up with this video. Have you also considered explaining concepts specific to web (mainly server side) frameworks? There are tonnes of tutorials explaining how to use a framework, which is more like explaining how to drive a car. This works in case of a car, because we are not worried about other specifics (how many times have we had to know how the transmission system works ?). But it is not true when it comes to software. Understanding how a framework implements a certain idea is more valuable. Would be happy to know what you think!

johnz
Автор

Maybe I didn't get the crux of the video, or the DI explanation seems not perfect.
1. Isn't changingfrom SugaredLogger to FastLogger just an example of polymorphic initialization? Shouldn't we still recompile and redeploy the whole service when this change is done?
2. Can you explain the difference between Dependency Injection, Factory Pattern and Inversion of Control and how these are related?
3. Can you give a simple DI example using just Java (no annotations), say with respect to 3 classes A, B and C?
Much appreciated!

Update: Got it. After going through Martin Flower's blogs and other youtube videos like Java Brains I got the "why and where" of DI and then revisting this video made everything perfect. Actually your video had everything, but the ideas seemed scattered maybe because of live stream. Anyway super quality content.

"What" is Di:
While designing a class, move the responsibility of the creation of its states (construction of member objects and hardcoding the primitive states) to out of the class.

"What" is getting injected:
The states of a class (ie member variables - both primitives and objects).

"Why": S of SOLID. A class should have only single responsibility. For a class to get modified there should be only one reason - its responsibility changes (else code maintenance is difficult and often we will have to touch multiple classes for a small change only to find out later existing test cases are breaking). And object creation is not the responsibility of most classes (here Logger), it should be the responsibility of a seperate class itself (here modules).
Ex without DI:
- Environment: A has B and B has C. All three classes have default and param constructors. A is newing B (ie, A has new B(...)), B is newing C.
- Problem 1: Inside B, I need D instead of C. Now I will have to rewrite the whole new C(..) in B.
- Problem 2: I need to change states of C, thus its param constructors too. But now I will have to touch B since it has new C();
- Solution: Move the newing of C outside of B to A. And use constructor or setter to inject C. DI!
Problem 3: Same as problem 1 and 2. Now A will have to be touched often when B or C changes.
Solution: Use DI on A and use factory pattern (a seperate class only for object creation) so that no objects in the object tree of A - B - C gets touched.

"How" / implementation:
- Object creation: Classes solely responsible for object creation are designed (here Module). In Spring its BeanFactory or Application context. These classes are a design pattern in itself - Factory pattern.
Note: Primitives are mentioned in a properties file or xml file.
- Identifying injectable objects: Here Gaurav is doing it manually in Module itaelf. In Spring, these are automatically identified when they are annotated with @Component (and stereotype annotations like @RestController etc), @Bean.
- Injected using: Constructor, setter, Autowire.

Note 1: For interviews, be ready to code in both Java (like GKCS) and Spring.
Note 2: I think only technical objects like controller, service, repo, logger, etc should be used for DI since they are ised by developers and developers decide when to change. Data objects shouldnt be used for DI I believe since they are so dynamic since determined by customers of the application.

vyshnavramesh
Автор

Great video. Can you please do system design video of a ecommerce platform such as: amazon, walmart, etc. Thank you

samirhere
Автор

Sugaredlogger getlogger factory method will return a logger with a different outputstream than asked for

aabhishek
Автор

Great explanation Gaurav, I can totally relate with that first day experience with finding main method. 😂

AbhishekChourasiya
Автор

When I hear of DI first thing I can visualise is unit testing. And unit test cannot exist without mocking, spying, injecting mock. Doing them with new keyword here and thr thrown in code is nightmare.… Yeah loose coupling makes sense and have seen it’s usage a lot in feature enhancements but IOC comes as blessing for unit testing..

arnavkarforma
Автор

If we have to change the Logger impl from SugaredLogger to FastLogger, we have to change the code and recompile and deploy again, for the leaf node configuration we can directly point to a properties file or a config server, how is DI saving this redeployment as you said?

RISHABHJAIN
Автор

I am having the mutual feeling as a fresher...full day I was searching for new keyword🤣

deeproy
Автор

Hi Gaurav, can you please tell which software you use while doing screen recording and video editing ?

shubhambansal
Автор

I had more expectations referring to your earlier videos (may be because those were not LIVE)

You could have:
1. Explain what type of audience were you expecting in this demo. What should they know already?
2. Explain what each class does in the start
3. Explain the problem without using DI and then switch to DI (probably starting with a simple interface doing the DI job). We don’t need to jump to the annotations immediately to explain, do we?
4. I missed the connection of modules. Why weren’t the modules shown in the picture?
5. What does the bind method in modules do? What does @Provides do?

Ironically, you mentioned in your video that any new engineer would get confused with this concept and your video did nothing easier.

I myself did not understand major part of the video until I went ahead and looked at the code and figured out what each component does.
eg: Why did u use CompletableFuture. This was so irrelevant to the topic.

I would be really happy if you could take this subject again and explain it more clearly.

firstlast
Автор

Solid stuff! Been writing Spring Boot apps without understanding DI in detail. This has opened by eyes to the implementation detail.
Will the code be shared on Github?

aymanpatel
Автор

Would be of great help if you upload the complete video, joined lil late

shalomedillu
Автор

Can you do a video on Dagger vs Guice next?

sreyas
Автор

Gaurav pls start algo ds lectures again

LeoLeo-nxgi
welcome to shbcf.ru