Dependency Injection Explained in 7 Minutes

Dependency Injection Explained in 7 Minutes

ArjanCodes

8 месяцев назад

63,485 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@estevaoyt
@estevaoyt - 27.02.2024 20:02

Beautifuly explained, clear as water! keep it up!! I think dependendy injection is one of the most important concepts in programming!

Ответить
@PanduPoluan
@PanduPoluan - 27.02.2024 20:23

The power of Python's duck-typing leads really well into dependency injection.

Just pass an object that has the required properties/methods, and done. No need to subclass from an esoteric BaseSomethingClass first.

Ответить
@Nalewkarz
@Nalewkarz - 27.02.2024 21:19

Deja vu, You have recorded almost the same video again. Arjan it's time to go deeper into more advanced examples.

Ответить
@VanosTurbo
@VanosTurbo - 27.02.2024 21:57

Agree your codebase gets such an improvement when you start using dependency injection.

Ответить
@roger_rogerthat
@roger_rogerthat - 27.02.2024 22:04

This time I felt like I've already seen this video. You should talk about an auto wiring DI library like kink

Ответить
@TheEvertw
@TheEvertw - 27.02.2024 23:32

I prefer SMTP as my email sender. Works every time, regardless of how a customer has configured his email server.

Ответить
@TheEvertw
@TheEvertw - 27.02.2024 23:52

"Dependency injection", as explained here, is probably the most simple OO design pattern. In OO, it is called the Strategy Pattern. You hide the details of an algorithm behind an (abstract) interface. The user of the strategy only knows the abstract interface, each implementation of the Strategy interface knows only the details it needs.

I don't understand why people wanted to change the name. The name "Strategy Pattern" has been in wide-spread use for 30 years. I guess people like re-inventing the wheel.

That is no critique on Arjan, he didn't coin "dependency injection". I thank him for this video, I never understood what people meant with DI. I thought it was some complex trick like the "Curiously Recurring Template" pattern. Which is similar to DI but achieves lightning fast execution & minimal memory footprint, at the cost of long compile times.

Ответить
@PabloVillegasMartín
@PabloVillegasMartín - 28.02.2024 00:14

I don't get the real advantage of it .The lines of code you reduce in the first class you have to put them in an other file, so you end with the same (probably more) lines, more files and dependencies. I would really like to understand the advantages of it to apply it more naturally.
Thanks in advance

Ответить
@ifeanyinneji7704
@ifeanyinneji7704 - 28.02.2024 00:16

Recently did something like this for work. But used a general email sending function that takes in smtp type, port etc for different email providers.

Ответить
@ShadiPL94
@ShadiPL94 - 28.02.2024 01:07

I am bit confused, since when we have to inherit explicitly from Protocols? Your Smtp, SendGrid and MailChimp classes all inherit from EmailService. Or is this just to show that those classes implements the protocol?

Ответить
@kubas89
@kubas89 - 28.02.2024 01:11

Postmark

Ответить
@DanielRodriguez-lu3uu
@DanielRodriguez-lu3uu - 28.02.2024 02:41

is decency injection only applicable on Classes?

Ответить
@berubejd
@berubejd - 28.02.2024 03:10

I've started using Resend for all of my projects after many years of using Sendgrid and really enjoy it.

Ответить
@MagnusAnand
@MagnusAnand - 28.02.2024 03:51

These are the best videos!!

Ответить
@heinereniscaicedo7510
@heinereniscaicedo7510 - 28.02.2024 04:09

There's an awesome third party library called Dependency Injection, it's so good to apply this principle to any python project

Ответить
@deViant14
@deViant14 - 28.02.2024 04:37

I've been doing this but had no idea it was a design pattern.

Ответить
@MineStrongth
@MineStrongth - 28.02.2024 06:34

Reupload? I swear I've watched this before.

Ответить
@Dyanosis
@Dyanosis - 28.02.2024 07:06

From my experience, most email services, from a back-end use scenario, feel the same. Where they really shine is how they interact with other providers. For example, emails sent through MailChimp may have different headers or be easier to format.

Ответить
@capability-snob
@capability-snob - 28.02.2024 09:06

Don't get confused when talking to Java programmers about DI. Over there, it generally means "magically inferring which service to use".

Ответить
@diegovargas3853
@diegovargas3853 - 28.02.2024 10:40

Hey Arjan, thank you for your work from Spain. Would be great if you create a video with your top python books "to improve your python skills to another level", thank you!

Ответить
@maleldil1
@maleldil1 - 28.02.2024 10:55

Why do you use Protocols if you're going to subclass that anyway? Doesn't it defeat the purpose of Protocols (structural subtyping) in the first place?

Ответить
@kvicar7419
@kvicar7419 - 28.02.2024 13:27

So in this case, how the email service specific arguments are going to be handled? For example the attachment argument? First we are going to create an instance of email service, pass the arguments to it and then use it to create an instance of EmailSender to actually send the email?

Ответить
@mirwek
@mirwek - 28.02.2024 15:13

I know only mailchimp. Now I found out about sendgrid as well.

Ответить
@Ruskialt
@Ruskialt - 28.02.2024 15:20

Yes it's a great pattern. To actually manage and reduce dependencies you need also consider where to place your abstract classes and interfaces.

Ответить
@danielschmider5069
@danielschmider5069 - 28.02.2024 15:55

Can we see the files email_service and services? You put code there and never showed them

Ответить
@Efebur
@Efebur - 28.02.2024 17:54

In the after version, do we even need the EmailSender anymore? It does nothing but redirects the send call with the same parameters to the EmailService. We could just use the EmailService directly for that.

Ответить
@alexandarjelenic2880
@alexandarjelenic2880 - 28.02.2024 20:42

I’m confused between the factory pattern and dependency injection. I watched both your videos. Is it the same?

Ответить
@ytuserln4990
@ytuserln4990 - 29.02.2024 03:06

Hello Arjan, great video as usual! I have a question why do you inherit from the protocol in a class which implements one? It's the second video in which I can see this, whereas in ABC vs Protocol you pointed out that it is not needed (duck typing) and also decouples the one who implements from the one who requires. Is there any reason for that? Thank you in advance for the response.

Ответить
@thefattysplace
@thefattysplace - 29.02.2024 18:50

The emailSender class here is not needed imo. A better approach (imo) would be to use a common interface on the sender classes, and use DI to pass the relevant sending class to the interface object. Then just call send on the interface.

Ответить
@buchi8449
@buchi8449 - 01.03.2024 02:51

One of the surprising things I encountered when I moved from Java to Python was the heavy usage of the service locator pattern.

A typical example is Flask's "request" and "d". It is still widespread in Python to put a dependency or an accessor to a dependency in the global context and reference it inside a component. Of course, Python has enough flexibility to manage tests of such less decoupled codes. However, I have always believed that clear decoupling using DI is cleaner and more straightforward.

FastAPI's DI is handly but covers only part of the DI commonly practised in Java. FastAPI supports only DI in the "request" scope. But DI frameworks in Java, like Spring, support other scopes, including the "application" scope.

The lack of DI containers supporting the application scope is why people must repeat "app.include_router(...)" etc., to construct big applications. DI realises IoC (inversion of control) at the component level. However, you still need to control the construction of an application (injection of dependencies), as demonstrated in this video.

The automated construction of applications using DI and CoC (convention over configuration), which is widely used in Java, is something we should consider in Python as well.

Ответить
@Vijay-Yarramsetty
@Vijay-Yarramsetty - 03.03.2024 20:12

we are using sendgrid in our project and your video on this really helps. thanks a ton!

Ответить
@vyacheslavkapitonov9677
@vyacheslavkapitonov9677 - 04.03.2024 15:56

How will it work if I need to pass an attachment? I still need to change send_email for all of my classes

Ответить
@danielwhiting4017
@danielwhiting4017 - 05.03.2024 12:33

Thanks for the great video. I understand that this is a bit of a contrived example but can you explain why you use an EmailSender class at all when it only wraps a method of the service object that is passed to the initialiser? In other words, why not just call the send_email method directly?

Ответить
@jumper0122
@jumper0122 - 06.03.2024 10:53

I'd really like a few more videos on this topic -- it seems really clever and useful but I don't totally understand it

Ответить
@mackymccormack8446
@mackymccormack8446 - 06.03.2024 22:23

Thanks really helpful. In this example though I couldn't see how variations in the email service protocols were catered for. The send_email method didn't show an indication of the different logic needed for different services.

Ответить
@sardarji_moneywale
@sardarji_moneywale - 26.03.2024 21:47

We use SparkPost as our primary sending service for most critical things like invoices

Ответить
@crazy_pythonist
@crazy_pythonist - 08.05.2024 19:05

hmmmm, is this actually just Strategy Pattern??

Ответить
@hello_world_zz
@hello_world_zz - 26.05.2024 08:27

Thanks Arjan

Ответить
@edgarcarrillo3814
@edgarcarrillo3814 - 26.06.2024 20:58

Thank you Arjan! Clear, concise, and to the point. I now know Dependency Injection!

Ответить
@shashishekhar----
@shashishekhar---- - 29.07.2024 03:38

Great example.

Ответить
@deepmodel-q3b
@deepmodel-q3b - 08.08.2024 02:25

Isn't using a mutable argument in Python a ROOKIE MISTAKE?

Ответить
@janetgauntt903
@janetgauntt903 - 19.08.2024 19:01

To answer your question about which email service, I'm using mailjet

Ответить
@sidg2110
@sidg2110 - 28.08.2024 18:04

I would have banged my head against a wall if not for this video.
A lot of people try to get away by just using big words. This is the first video not to do that.

Ответить
@cameronmcnz
@cameronmcnz - 11.09.2024 23:29

Simply passing an argument into a method is not dependency injection. It's passing arguments to methods, something every programmer has done since the start of time. Similarly, using an interface or abstract protocol is not dependency injection. That's simply coding to an interface and has nothing to do with dependency injection. It's no wonder so many people are confused about the term when so many tutorials about it get it wrong.

Ответить
@KonstantinPrydnikov1
@KonstantinPrydnikov1 - 02.10.2024 03:34

How I hate __init__, self, classes and of course dependency inversion/injection in the code where they don't need at all. They must be used in sending emais where 10+ types of sending and 10+ different services and users that uses that sendings. In all another cases please people uses just def and modules in Python, matherfucka without classes and that oop and solid bullshit

Ответить
@chamaocharles
@chamaocharles - 17.10.2024 03:02

Great video. Thanks Arjan.

Ответить