Dependency Injection Explained in 7 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you why you need to use dependency injection if you want your code to be flexible and maintainable it's really important that you think about dependencies and how you can deal with them properly so that you end up with nice Loosely coupled code a pattern that's going to help you with that is dependency injection it's actually one of my favorite patterns and arguably one of the most important patterns in software development it's really important that you quickly identify problems with dependencies and coupling in your codes you can solve them to learn how to do that check out my free workshop on code diagnosis by going to iron codes SL diagnosis it's about half an hour and teaches you a three Factor framework on how to identify problems in your code fast so iron code SL diagnosis link is also in the description of this video I have an example that I want to show you which is about sending emails so I have a class email sender and this can communicate with different kinds of email sending API so there are send grid there is mail gim there is simple SMTP I mean SMTP is not actually a service but I just include it here for Simplicity and the class email sender it's basically nothing else than a method that sends an email so I put it in the class because normally you might also want to do some other things like setting up a connection disconnecting Etc but now there's only a sent email method and this gets a service type so that's this class so send grid me chimp or SMTP there's two add add there is a subject a body uh and some other things as well that you're going to need so then what it does is there is an if else statement that checks which service we want so there is MIL chimp so then we're going to create a mil chimp object and send the email if it's send grid we're going to create a send grid email and if it's an SMTP then this is going to create an SMTP object and send the email and otherwise if there is no service then by default we just use MailChimp now the problem is that if you want to add more more services to this then this is going to add more cases to this IFL statement which means that the send email method is going to get longer and longer and longer now you could split that off into separate methods but still you're going to end up with this huge if El statement and then we're not even talking about using more features of these email sending Services because currently what we do is simply calling a simple method maybe we want to do more in the future and then that's going to further complicate this class and there's an even worse problem in this example so let's say we have the different surfaces just coded them up here as simple um mock services so there is SMTP there is send grid and there is MIL and these all inherit from the email service protocol which is just a simple class with a single method if you look at these classes you see that the initializers are actually slightly different so the SMTP class needs an SMTP server and a signature the send grid email service needs nothing and the MailChimp email service needs an attachment and this leads to a problem because that means that in the email sender class we need to supply all of these things all the time in order to be able to create either one of these things so even if we create a MM service we still need to pass an SMTP server and the signature even though MailChimp doesn't actually need those values these sorts of problems are solved by a dependency injection what is dependency injection it basically means that instead of initializing an object in a method like what we're doing here we actually pass it as an argument or we get the object from somewhere else and that way the place where you create the object is done in a different place than where you use the object in this particular case that means that as part of the send email method call we shouldn't pass a service type so that it creates a service we should pass an actual object that is the service here's an updated version of the code so I kept the email sender class but really simplified it a lot so instead of it having an if statement picking between the different Services we now have a send email method that's actually use as an email service instance variable that we set in the initializer you could also decide to pass this as an argument to the function but the idea here is that probably email sender is going to have other methods in the future that will also need the email service so that's why I put it as an instance variable here but now you see that in send email there is no more dependency on different types of email Services send email doesn't know anything about specific email Services it simply uses the email service protocol and calls send email now there is still a default value here which is the MailChimp email service so that's still a dependency you could decide to not have a default value or make like a mock email service that doesn't connect with anything else it depends a bit on the needs of your application but email sender is now way simpler and most importantly we don't need to change it if we introduce a new email sender type for example if you want to add support for mail gum yet another email service then we don't need to change the class email Center we can simply Supply it with an object that implements the email service protocol and in the main file this is actually where we use the email sender so that's where we create the actual service and here you see we're using send grids and then we pass that along when we create the email sender object and then finally we can send an email so where is the dependency injection Happening Here well that's in this line so here we inject the email surface dependency into the email sender object when we create it another nice thing about dependency injection is that it's going to make your test easier here's an example of what unit test would look like without dependency injection so then we send an email we provide it with a server ID but now we have a problem because now this is going to actually send an email so if you want to fix that we have to use patch which leads to extra boilerplate code and secondly now there is no clear idea of when there is a problem in send email whether that's due to the body of send email or whether that's because of of some issue in the MailChimp email service there's no way to make that distinction in this test here's the after version of these unit test so in this case we create the objects here and then we can call the method so this assert actually only concerns what we do in send email and then if you want to we could replace this Surface by a mock surface so that we really test the body of sent email and then we also know if there is some sort of error whether that happens in creating the SMTP service or whether that happens in actually calling the send email method so it's much easier to test your code this way by the way have you been using email sending services in your applications like send Grid or MailChimp which one do you recommend let me know in the comments so dependency injection can really help you create more Loosely coupled and easily maintainable code take a look at some of your old projects and see where you can apply this idea of dependency injection more when you do that I can assure you that you're going to end up with more modular code now like I said dependency injection also helps you write better unit test if you want to learn more about how to write unit test for python projects check out this video next thanks for watching and see you soon
Info
Channel: ArjanCodes
Views: 52,194
Rating: undefined out of 5
Keywords: dependency injection, dependency injection python, dependency injection design pattern, dependency injection design pattern python, design pattern python, design patterns in python, python decoupling, decoupling in python, design patterns, python programming, python tutorial advanced, python tips, learn python programming, clean code python, clean code principles, tuesday tips, tech tip tuesday, arjancodes, arjancodes python, arjancodes design patterns
Id: DpMGEhwuuyA
Channel Id: undefined
Length: 7min 11sec (431 seconds)
Published: Tue Feb 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.