Learn Dependency Injection Like a Pro For Faang Software Engineers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you hey guys welcome back to the channel today we are going to demystify a coding concept that might sound complex but is actually pretty simple that is dependency injection think of it as nothing more than passing values to a function just like we do in everyday life day in Day Out so let's Dive In well you can say that dependency injection is just a fancy way of saying passing stuff into a function it's about giving a function or object the things it needs to work let's say that we want to bake a pizza and we don't want to bake it at home we call a pizza place and tell what we want the pizza delivery is like a function that takes your order which are the dependencies in this case and delivers the result which is the pizza so let's look at the code example we have a function called order pizza first it creates a new oven by calling the new oven function after that it calls Bake Pizza method on the oven created in the last step this way we order a pizza the function here creates its own dependencies which is the oven instead of having them provided now let's look at another example we have the same function order pizza but now it takes an oven instead of creating it creating that on its own now what we need to do is simply call Dot Bake Pizza on the oven provided in the function as you can see that this function is more flexible because now you can choose which oven to use the oven can be a regular one a wood fired one or some custom made one we don't even care as long as the oven implements the bake pizza method so as you can see that dependency injection makes the code more flexible testable and reusable with dependency injection one can easily swap out dependencies for testing or use the same function in different scenarios so let's dive into a Hands-On example in golang let's say that there is some message which is obviously a string and we want to write that message to a buffer or to a file so for that we have to create two different functions the first function is right to buffer which takes in a buffer as well as the message and it has some logic to write that message to the buffer the next function is right to file that function takes a file as well as the message and it has then some logic to write the message to the file but as you can see that in go we have this function called f printf so F printf takes an i o writer as well as the message the message can be a formatted message but we don't need to worry about that for our case it's just a simple message without any format so yeah so f printf is a function that takes a writer as well as a message and returns back n which is an end which is the number of bytes that were written as well as some error that might have occurred and as you can see that we don't need to worry about how fprintf implements things internally we know that it takes something which is an i o writer as well as the message and we know that if we give it the message it will be written to the I O writer so how do we go ahead and Implement our right to buffer and write to file function well now that becomes very simple we know that buffer which is of type pointer to bytes dot buffer implements IO writer as well as file which is pointed to OS file also implements IO writer so instead of like Implement implementing them differently in both the functions we would simply go ahead and call fmt.fprintf which takes in an i o writer as well as the message so doing that our implementation is complete interfaces make everything so simple you have two different functions with two different purposes but now you can use a single interface function a single function which takes in an interface to actually Implement them both and if you think about it we don't even need to have these two different functions we can simply call fmd.fprintf and the only condition is that the first argument should be something that implements io.writer let's try to implement this function now so as you can see abc.txt is an empty file it has nothing in it now you can see that there is a main function here I'm uh taking the input directory which is using os.get working directory command this will give us the current working directory now I will go ahead and open the file using path.joint so part dot join actually builds a complete path irrespective of the underlying operating system so it takes here which is the current working directory as well as abc dot text which is the file that we want to open also note that we would need to open this file in a read write mode so we can read the file as well as write to the file also the permission should be a general permission which is zero seven double five uh so we are giving all permissions to this file uh we will also close the file using refer so after this program ends the file will be closed now I will go ahead and site to file using write to file function and as you remember it took the fmt dot so it is very simple it was very simply implemented it was just taking an the io writer which in this case is the file and doing an fmt.fprintf on that I will go ahead and run this program go on main.go the program completed successfully and as you can see abc dot text has the message printed to it which is the hello world message now let's go ahead and implement this function for bytes dot buffer for that I will first initiate a buffer using byte start buffer then I will call the function write to buffer which we implemented previously and as you remember it was the same function that took an an i o writer which is buffer in this case and called fmt.fprintf on that now calling this function we know that something is written to the buffer and we will simply go ahead and print it to know what was written so if we just do fmt.println and print the message written to buffer we can see that hello world is being written to that so in a way we just used a single implementation to write to two different objects here first file and then a buffer now let's think of a practical use case of this thing let's say that we have a database and that database can either contain a file or it can contain a buffer or it can contain a database connection and let me tell you something the database connection also implements the IU writer not even the database connection but the network interfaces the TCP connection the HTTP connection they all implement the I O writer interface so in a way you can use the same function fmt.fprintf2 right to the network or you can use it to write to the database so just to show you a simple example we have a file database which has file as its underlying data store we will write a function to create a new database which takes in a file and creates a new database now in the main test function we will just write a test to write to the file first we will initiate a new database and then and then we will simply write to the database but you can see the problem here let's say that we don't have a file and in real case we don't have a database let's say that we don't have a underlying database connection and we don't want to even create the connection we just want to test the functionality that whatever we are writing to is being written to the database what we can do is that instead of passing an OS dot file to the database or the underlying database connection we can simply go ahead and pass an i o writer at runtime or in production that would be replaced by database connection but for testing purposes that can be simply replaced by a byte store buffer this will make our life so much simpler using dependency injection using interfaces and let me tell you something interfaces is the actual proper solid principles in go so you don't need to worry about anything else if you know and if you understand interfaces you take care of dependency injection and you take care of all the solid principles okay that's it for the video today I hope you liked it and I will see you in the next one foreign [Music] [Music] foreign foreign
Info
Channel: Distributed Systems Programmer
Views: 1,898
Rating: undefined out of 5
Keywords: coding, software engineering, faang, google, programming, golang, rust, blockchain development, webdevelopment
Id: AJ6uz9mlFOA
Channel Id: undefined
Length: 10min 41sec (641 seconds)
Published: Tue Sep 12 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.