This is why dependency injection is useful

  Рет қаралды 31,904

Web Dev Cody

Web Dev Cody

Ай бұрын

Become a YT Members to get extra perks!
www.youtube.com/@webdevcody/join
My Products
🏗️ WDC StarterKit: wdcstarterkit.com
📖 ProjectPlannerAI: projectplannerai.com
🤖 IconGeneratorAI: icongeneratorai.com
Useful Links
💬 Discord: / discord
🔔 Newsletter: newsletter.webdevcody.com/
📁 GitHub: github.com/webdevcody
📺 Twitch: / webdevcody
🤖 Website: webdevcody.com
🐦 Twitter: / webdevcody

Пікірлер: 134
@tomaszchmielnicki8676
@tomaszchmielnicki8676 16 күн бұрын
This feels like a workaround - I mean, you've separated the deletePasswordResetToken, and updatePassword functions, but then you're wiring them together with the tx argument, so they're kind of decoupled, but not really, because they still need to know that they can be ran inside a transaction, so they feel like leaky abstractions. I feel like you could just create a separate changePassword function with its own database calls inside a transaction and it would be fine to have a little bit of code duplication there.
@WebDevCody
@WebDevCody 16 күн бұрын
Yeah that’s a good point
@domizianoscarcelli1936
@domizianoscarcelli1936 Ай бұрын
I was in a very similar situation that I didn't know could be resolved with dependency injection, nice!
@shaked1233
@shaked1233 27 күн бұрын
This is awesome, I never thought about that use case with drizzle. your content is superb, keep em coming!!
@RhettNewton-r7b
@RhettNewton-r7b 25 күн бұрын
This is awesome! Love seeing clever and clean solutions like this. Thanks for posting this!
@JGB_Wentworth
@JGB_Wentworth 24 күн бұрын
Also DI enables unit testing to prevent hardwired classes being instantiated in the constructor so that mock classes can be passed in as needed for testing purposes
@krajekdev
@krajekdev 20 күн бұрын
Not necessarily related, but after years of studying and expirimentation I can suggest that mocking should be the exception in ones automatic testing approach. I understand that some classes are really impractical in automatic tests suite, but otherwise one should test mostly at the API level and use as much "real" classes as possible.
@liftingisfun2350
@liftingisfun2350 11 күн бұрын
@@krajekdev In unit tests, you want to mock everything but the actual unit that's being tested. That may be a single method or a single class. In integration testing, you want to test the interaction of a unit with another. A class with something else. So you won't use a mock in that case. Why? Because the goal of the unit test is to prove that just the logic within the thing you're testing works as expected, *given everything else is working as expected*. Integration tests will provethat the interaction between two units, *which are assumed to be tested already*, behaves as expected e2e tests will cover the whole system's interactions. You want tons of unit tests, reasonable amount of integration, and sparingly use e2e tests
@krajekdev
@krajekdev 11 күн бұрын
@@liftingisfun2350 again, i think that this mantra of tests pyramid is ineffective and i practice approach of reversed testing pyramid. My mocking is very limited. I know that youtube comment is not a place for the lecture. I just provide it, because years ago i needed the trigger to free myself from false "test pyramid" paradigm.
@tjakevladze
@tjakevladze 23 күн бұрын
You can also solve this with the Unit of Work pattern. You add all the operations to the unit of work and when you call the Commit() method, it will create a transaction and process the database queries. What is your opinion on this solution?
@vitorfigueiredomarques2004
@vitorfigueiredomarques2004 29 күн бұрын
One interesting thing is that with React context we can easily have an Angular-style DI container so we can control the lifecycle of the injected object instead of just having everything global or passing all dependencies from function to function. Create a context whose default value is a WeakMap. Now in your application, you can create many providers for this Context that add dependencies to the WeakMap. Create a hook so the components can access the WeakMap and pass the WeakMap to your functions. Now, for instance, if we want a dependency that needs to load after something shows on the UI and part of the UI needs to show a loading spinner while it is loading, create a Provider that adds that dependency only after it loads (you can use the "use" hook to force some loading screen on the provider).
@hebersolano5093
@hebersolano5093 29 күн бұрын
Interesting, you need to make a video about this
@brockherion1275
@brockherion1275 28 күн бұрын
TKDood (maintainer of React Query) writes about this a lot on his blog. He did an excellent article on how to use React Query as your async state manager and React Context as your DI container to use that data elsewhere in your app. It's a pattern I use quite a bit!
@twitchizle
@twitchizle 23 күн бұрын
U should write an example about this.
@tmanley1985
@tmanley1985 29 күн бұрын
Among other things, one thing that dependency injection does is to force you to confront and make explicit your dependencies. When your function signature begins to grow, you may start to notice that it's either too big or data starts to clump together often which usually means that there's either another abstraction you're missing, or perhaps the function has too many responsibilities.
@wazzadev7209
@wazzadev7209 Ай бұрын
Thanks for also answering my question 😁👍
@ShivGamer
@ShivGamer 29 күн бұрын
Good catch, I've faced issue regarding this, I was deleting some sessions without checking if the other query was done successfully or not. I had deleted 60ish items from my db in this case while testing, luckily it was stage db so everything was fine😅
@loorinho
@loorinho Ай бұрын
Hadn't gotten much into drizzle and so i didn't even know that they had transactions inbuilt(i guess i need to read the docs beyond making queries). Thank you for the insights. Much love from Uganda "mentor"
@bibblebabl
@bibblebabl 28 күн бұрын
Prisma alternative also has it
@tomaszchmielnicki8676
@tomaszchmielnicki8676 16 күн бұрын
You could also put the database calling functions inside a service object factory with db as an argument, so you'd be able to use dependency injection in every db calling function without modifying its signature, but you'd have to instantiate new objects for transactions
@brockherion1275
@brockherion1275 28 күн бұрын
Another banger, I'm learning a ton from videos like this! And as I apply these techniques to my own codebase, I've notices my code not only becomes clearer and more maintainable, but I'm also simply writing less code. Keep 'em comin!
@luishenriqueandradepellizz680
@luishenriqueandradepellizz680 Ай бұрын
So I am working on a transaction feature where it will create a new user account for auth, then create the user own document, in this case an apprentice which will have its own props, then send a registration email. I didn’t know how to make this totally clean using SOLID and all this guru stuff then I ended up creating my Transaction class with createTransaction method, which then I Dep Injection the MongoDB session, but then some others dbs do in different way, while Mongo use session, others use different way to manage transactions. It worked anyway 😅
@bandekhoda7801
@bandekhoda7801 Ай бұрын
Wow this is super useful
@alderry6447
@alderry6447 15 күн бұрын
I think best and clean way is using async local storage to shared async context between those methods
@Burak-ls5yd
@Burak-ls5yd 13 күн бұрын
Can you please tell me what is the add-on that shows your Git history on Code?
@reallylordofnothing
@reallylordofnothing 10 күн бұрын
At the heart of dependency injection is "providers can be switched" by simply injecting their dependencies without the called code knowing nothing about providers being switched. In this case, the only switch you can do is either pass the database connection yourself or get it passed by a parent caller. This is more like "pass a default if an object is not passed". for it to truly be dependency injection, the trx parameter should have worked lets say "to change password in a redis DB and a Mysql Db via the same methods. If the API for change password and delete tokens is agnostic and providers( redis, MYSQL) can be injected, in my book, that is dependency injection.
@daphenomenalz4100
@daphenomenalz4100 Ай бұрын
We can have transactions for both queries as they are both running on a db, but what do i do if first query is an upload to S3, and then second is adding the metadata returned from S3 to the db. If the S3 one fails, that's fine. but if the second db one fails, do we have to make another endpoint for uploading the metadata back to db and asking the client to perform this action again. Since, S3 upload is already successful and we def do not want to rollback that. Or is there a better way possible?? Btw love your videos!!
@puckwang6850
@puckwang6850 Ай бұрын
first thing comes up on my mind is retry and put it to a dead-letter queue(if that's not overkill) for the second part of the transaction, but ideally, we want to know the exceptions/errors that cause the failure🤔
@WebDevCody
@WebDevCody Ай бұрын
I'd personally do it all in a single api endpoint. presigned urls sound cool, but honestly the complexity increases a lot instead of just allowing your api to accept your image and have your api upload to the bucket and also create the db object. you'll be charged more for bandwidth but the simplicity is worth that operational cost imo.
@TannerBarcelos
@TannerBarcelos 29 күн бұрын
⁠@@WebDevCodyvery much agree here. Since the operations are related, it makes sense to accept the bandwidth overhead in exchange for the simplicity and operational cost that can come out of solving this problem.
@daphenomenalz4100
@daphenomenalz4100 29 күн бұрын
Thnx!
@fennecbesixdouze1794
@fennecbesixdouze1794 28 күн бұрын
Doing it in a single API endpoint doesn't answer the actual question: it's still possible that the upload to S3 succeeds but creating the database record fails.
@DemanaJaire
@DemanaJaire Ай бұрын
I hate being this early, cause after watching a video like that I like to read what others have to say about it, but there's no comments yet and I will probably forget to come back here. 😭
@WebDevCody
@WebDevCody Ай бұрын
don't worry I'll send you a message in 8 hours and expect you to come back
@daphenomenalz4100
@daphenomenalz4100 Ай бұрын
@@WebDevCody WebDevChady
@KayandraJT
@KayandraJT Ай бұрын
Here's my own opinion on this and sort of a reminder for you. This is how I do it, even in other languages I've worked on. There's always room to pass in a transaction context and with provision of a fallback for synchronous calls.
@puckwang6850
@puckwang6850 Ай бұрын
reference it instead of instantiating it, that's pretty much the gist of DI(dependency injection)
@louisant1817
@louisant1817 Ай бұрын
built a chrome extension that reminds you to come back to registered pages
@reallylordofnothing
@reallylordofnothing 10 күн бұрын
At 2:35 when trx is not sent - as it is optional - what happens at line 75 ? await trx will be null or will db be configured as default if not passed?
@islambn8962
@islambn8962 Ай бұрын
and createTransaction function is created so the service ( use case ) does not know about drizzle right ?
@WebDevCody
@WebDevCody Ай бұрын
yeah I'm not 100% sure I got the abstraction correct because technically the trx object is a drizzle transactions and the use case can see it and know about it in the callback. I think maybe a better abstraction could be runTransaction([doA, doB]), and behind the scenes the trx object would just inject into the last or first argument.
@brokula1312
@brokula1312 Ай бұрын
Why do you await in deletePasswordToken function if there's nothing below await statement?
@mostafaelsayed3492
@mostafaelsayed3492 25 күн бұрын
updatePassword is an async function, it returns a promise, you have to use await to resolve this promise.
@gammon9281
@gammon9281 Ай бұрын
I would love a deep dive into dependency injection. I'm pretty new to the concept and I've tried refactoring code on a project using the tsyringe containers which didn't work out too well. Are there any resources or guides you can recommend how to (incrementally) adopt DI? I liked how i dont have to pass more and more arguments to my functions since the classes were automatically injected into the constructor but that also made it quite hard to split the code / move it outside of the respective classes. Is there a middle ground or a better way of doing this? Would love to hear your thoughts and how you think about implementing / adding DI to your codebases
@WebDevCody
@WebDevCody Ай бұрын
idk I always lean towards injecting with function arguments. Using IoC containers or DI frameworks feels like over engineering and tends to result in code that is hard to understand
@gammon9281
@gammon9281 Ай бұрын
@@WebDevCody Hey, thanks for the response! I was wondering how you would handle deeply nested function calls. If you have authentication and authorization functions that get called deep inside a big complex function, how would you pass all the different arguments around in that case?
@fennecbesixdouze1794
@fennecbesixdouze1794 28 күн бұрын
JavaScript is a super dynamic language so there's really no need for heavy IOC containers. Just export module-level constants that hold your authentication and authorization and import them into your other modules. Jest allows you to mock all of a modules' exports so there's no need to introduce an IOC framework to make your code unit testable even after you torn out the function arguments.
@jazzdestructor
@jazzdestructor 26 күн бұрын
this is one of the easiest explanation of dependency injection, thanks Cody, but i still do hate the decorator pattern
@derekpowersblight
@derekpowersblight 29 күн бұрын
I love overloading functions, unfortunately in js es no without arg reading
@gavipk
@gavipk 23 күн бұрын
Is this really "dependency injection"?
@null_spacex
@null_spacex Ай бұрын
I liked this ✍🏽
@reallylordofnothing
@reallylordofnothing 10 күн бұрын
This is some really clean code. Is this React or Next? Subscribed!
@haruccigr
@haruccigr 29 күн бұрын
I really like that JS devs nowadays wish JS to be more like Java than the chaos it was years ago.
@smash3689
@smash3689 Ай бұрын
Functions that can be part of an atom. Quark type? Lol. Would it be nice to have such type & have them applied to functions which would make them easier to find & notice?
@shakapaker
@shakapaker 24 күн бұрын
That's awesome
@sealone777
@sealone777 Ай бұрын
In this case, it would be nice to use decorator like @Transactional and have the domain code not even be aware transaction exist
@WebDevCody
@WebDevCody Ай бұрын
decorators? gross
@sealone777
@sealone777 Ай бұрын
@@WebDevCody I think you will like it jk. Well I don’t mind it.
@salman0ansari
@salman0ansari Ай бұрын
over complicated
@nazarshvets7501
@nazarshvets7501 Ай бұрын
no, it wouldn't
@miauw8762
@miauw8762 6 күн бұрын
This is just a fancy word for a simple solution
@Bukosaure
@Bukosaure 23 күн бұрын
What theme are you using for VSCode?
@omerelbaz7648
@omerelbaz7648 23 күн бұрын
Would like to know as well
@paulosouza449
@paulosouza449 17 күн бұрын
This seems to be One Dark Pro ++ (at least to me). I'm not sure though.
@ironhunter2219
@ironhunter2219 22 күн бұрын
Cool, I have been using dependency injection without knowing what it is or why it exists xD
@buraky16
@buraky16 22 күн бұрын
a little unrelated but, which keyboard is web dev cody using? sounds so good. might be brown switch but the sound of key strokes are so smooth
@WebDevCody
@WebDevCody 22 күн бұрын
Klack app
@Euquila
@Euquila Ай бұрын
I prefer procedural programming more where the control flow and commands are all seen together. The results in a lot more lines of code but you get less indirection and fewer symbols overall
@SeibertSwirl
@SeibertSwirl Ай бұрын
Good job babe!
@WebDevCody
@WebDevCody 29 күн бұрын
thanks beautiful!
@alecdorasandler6690
@alecdorasandler6690 29 күн бұрын
What theme are you using?
@alecdorasandler6690
@alecdorasandler6690 28 күн бұрын
??
@shakapaker
@shakapaker 24 күн бұрын
It's bearded Theme Stained blue​@@alecdorasandler6690
@geelemo
@geelemo 10 сағат бұрын
What is drizzle drizzle?
@eliotfagnon7319
@eliotfagnon7319 29 күн бұрын
Does anyone know what's his theme?
@WebDevCody
@WebDevCody 29 күн бұрын
Bearded theme stained blue
@meowmesh
@meowmesh Ай бұрын
is Saas template will be os ??
@WebDevCody
@WebDevCody Ай бұрын
yes
@sameurbenhmouda1456
@sameurbenhmouda1456 29 күн бұрын
I mostly use this kind of trick but I never knew that it was called "dependency injection". Now I know the technical word, thanks!
@federicobau8651
@federicobau8651 25 күн бұрын
Is not dependency injection..is still passing and argument..dependency injection is very different and has a different endgoal.
@thomasluk4319
@thomasluk4319 16 күн бұрын
What colour theme is it
@WebDevCody
@WebDevCody 16 күн бұрын
Bearded theme stained blue
@supercrunch3367
@supercrunch3367 29 күн бұрын
Why not just use Nest JS?
@ajzack983
@ajzack983 29 күн бұрын
Good old transactions always good for endpoints that no 2 users can hit at the same time
@WebDevCody
@WebDevCody 29 күн бұрын
it's all about risk mitigation. Will the second call to the database fail? probably not, but if it does, now your system is left in a strange state
@ajzack983
@ajzack983 29 күн бұрын
@@WebDevCody I used it once on a controller that had a lot of related operations many users can hit the same resource, I ended up with a race condition exception where multiple users try to access a locked row or table even. You need to handle that case manually with a retry or something. Usually transactions are good for short operations like 2 fast queries are probably fine.
@Skuiggly
@Skuiggly 28 күн бұрын
funny watching this after reading your tweet “hating” on dependency injection
@WebDevCody
@WebDevCody 28 күн бұрын
Keep em guessing
@ward7576
@ward7576 26 күн бұрын
@@WebDevCody typical youtuber - explains nothing with a response, just to drive interactions up when otherwise content is quite shit and cannot do it organically
@WebDevCody
@WebDevCody 26 күн бұрын
@@ward7576 I hope you have a blessed day
@mandokir
@mandokir 18 күн бұрын
Dear God, he's using JavaScript on the backend.
@berkancetinkaya3520
@berkancetinkaya3520 23 күн бұрын
I would not recommend to call trx as second argument or third one. what if I need to pass another argument into this function? it would be like token: string, trx: db, emailCode: string this is so ugly so instead have a structure to pass params for such use cases functions { deps: { trx }, input: { emailCode, token } } in this way you can easily extend without having argument hells like removeToken(token, false, null, 2, undefined)
@ivorhoulker3646
@ivorhoulker3646 Ай бұрын
This is a useful concept, but in this case with drizzle it seems not to make any difference: "The tx object passed to the transaction function is just a convenience. You can use db interchangeably, it won't make a difference. It only has a rollback() method which only throws an error. Inside the transaction, it stops execution and rollsback" I realise this is not documented well. I did link to the github issue where this quote comes from, which I guess is why youtube deleted my comment. It's issue 613.
@WebDevCody
@WebDevCody Ай бұрын
good thing to point out!
@tomaszchmielnicki8676
@tomaszchmielnicki8676 16 күн бұрын
Or maybe use partial application for those two functions and don't clutter their signatures export const deletePasswordResetTokenWithTx = (tx: typeof db) => (token: string) => ... export const deletePasswordResetToken = deletePasswordResetTokenWithTx(db)
@balduin_b4334
@balduin_b4334 Ай бұрын
bruh drizzle is so much better than prisma... I wanted to also enable this type of dependency injection, but the type of the prisma client and the transaction prisma client aren't the same. That's why It is not Possible to just say (...., db = dbClient) {...} and the call it with ( ...., tx) [EDIT]: Solution type DB = Parameters[0]; then use DB type like that ( ..., db: DB = dbClient) You can't use the following functions from the default Prisma Client: "$connect" | "$disconnect" | "$on" | "$transaction" | "$use" | "$extends"
@mohammedbageri1678
@mohammedbageri1678 26 күн бұрын
Dependency injection in functional programming 😂. That's just a fancy way to say “calling a function with parameters”. Good idea though 👍🏻.
@anothermouth7077
@anothermouth7077 Ай бұрын
Person suggesting this must be from Java background 😄
@rariber
@rariber 13 күн бұрын
Maybe I'm being nitpicky, but it seems to me what you have done was inversion of control, not exactly a DI. But awesome content anyway dude, keep it up.
@WebDevCody
@WebDevCody 13 күн бұрын
So in your opinion, what would make this dependency injection if injecting the dependencies as arguments instead of importing them is not dependency injection in your opinion. I think there’s a misconception that you have to use some type of injectable auto wiring dependency injection framework for you to achieve dependency in injection and I don’t think that’s true.
@rariber
@rariber 12 күн бұрын
@@WebDevCody Yeah, you're right, I may have understood it poorly.
@TallStack-su8sq
@TallStack-su8sq Ай бұрын
theme name?
@WebDevCody
@WebDevCody Ай бұрын
bearded theme stained blue
@TallStack-su8sq
@TallStack-su8sq Ай бұрын
@@WebDevCody thanks
@fennecbesixdouze1794
@fennecbesixdouze1794 28 күн бұрын
I know this video is supposed to be about dependency injection and not about the actual use case .... however when you think about the actual change-password use case, it's probably better to not put this in a transaction if possible. For example, if the business logic allows the user to simply request another password reset token, it's perfectly OK to have deleted the password reset token and then failed to update the password: simply present an error to the user and have them request another password reset token. Another option is to simply mark the password reset token for deletion without actually deleting it, and have another process come in later and clean up all of the tokens that have been marked for deletion. That way the client can simply retry within some reasonable timeframe (keep in mind that if you do this, you probably want to include information about the current hashed password on the reset token itself so that the same password reset token cannot be used to successfully change a password twice). In general, my advice is to avoid transactions like the plague. They really slow down the performance of a web server, and usually you can design around the need for transactions by just thinking a bit more about your high-level design. Also transactions inhibit flexibility in the future: for example if you've designed with the assumption of transactions, what happens when you need to put some logic that interacts with services outside the database into a transaction context? Just putting a bit more thought into the high level design to come up with a solution that doesn't use transactions can save you so much headache down the line, and it's really worth it in almost all cases.
@federicobau8651
@federicobau8651 25 күн бұрын
I think you are in the wrong path. Even think that transaction can slow performance down is...not that correct. Transactions ensure consistency and correctness. Just read what you wrote and it confirms this. You would avoid use transaction for "some weird reason" and possibly ending up..do a clean up later?😂 that's crazy.
@MrDevianceh
@MrDevianceh Ай бұрын
Huh I thought it's pronounced Lu-Chia
@WebDevCody
@WebDevCody Ай бұрын
🤷‍♂️ no clue
@liu-river
@liu-river 29 күн бұрын
The Italian pronunciation is luchia, so it depends.
@saburex2
@saburex2 17 күн бұрын
My eyes are bleeding
@WebDevCody
@WebDevCody 16 күн бұрын
Maybe see your dr about that one
@saburex2
@saburex2 16 күн бұрын
@@WebDevCody my doctor can't help with your code, I don't think anyone can... posting a video where you shit on SOLID and call it dependency injection...
@WebDevCody
@WebDevCody 16 күн бұрын
@@saburex2 getting away from OOP is the first dosage of medicine
@antidegenerates7449
@antidegenerates7449 7 күн бұрын
Why engineers keep creating problems on empty space and complicate everything?
@WebDevCody
@WebDevCody 7 күн бұрын
Because it’s fun
@edgarmagalhaes18
@edgarmagalhaes18 9 күн бұрын
This is not dependency injection, wtf 🤣
@WebDevCody
@WebDevCody 9 күн бұрын
Java dev spotted
@ikbo
@ikbo 28 күн бұрын
This is poor man's dependency injection. Real programmer's use DI containers.
@federicobau8651
@federicobau8651 25 күн бұрын
Thats not dependecy injection... you are simply passing an argument to a function and still is.. Simply that. Doing that manually doesnt create a dependency injection. Then tell me what's the difference between dependency injection and pass and argument around ? 😂 Dependency injection is more complex and you can do it with a language like Typescript via Interfaces (but using a simple class would do too). You are confusing what is it and what is for... Yes it may look like is passing an argument into "something" (can be a functiom/method or a class constructor) but the concept and end goal is very different
@WebDevCody
@WebDevCody 25 күн бұрын
it's called functional injection. There are different ways to achieve dep injection. people using OOP like to do constructor injection, some people using IoC containers, some use setter functions, some use a DI framework. the point of DI is that your concrete implementation doesn't know where the dependency is coming from (it's passed in somehow). the only part I'd argue that yes maybe this isn't pure DI is because I'm using a db instance as a fallback inside the persistence function, but you're wrong in saying passing the dep as a function argument is not DI. Search how people do DI in functional programming languages. Often it's passed in as a "context" object. If you're still not convinced, I took my code and passed it to GPT which responded with this: "Yes, the trx parameter in your updatePassword function can be considered a form of dependency injection. In this context, trx represents a database transaction object, and by default, it is set to database. By allowing trx to be passed as an argument, you are injecting the dependency (the transaction object) into the function."
@federicobau8651
@federicobau8651 25 күн бұрын
​​@@WebDevCody even so it should be implemented differently. The "injection" means that is the "framework" doing it "automatically"... you are not injecting it, you didnt create an interface nor your are calling "a different class" from the original ...you simply switch from the "database connector" to... "the SAME conction but is an open transaction".. so you cant state this is called DI because a poor.. almost non-existant implementation of it .and.. the CALLER IS ADDING THE DEPENDENCY!. So in not "injected".. you manually are adding it .. On top of it...you didnt create and interface (and you are using Typescript..so no excuses) and you didnt add the interface type in the parameter...which is basically the main component of DI..so even like this your example. A part of not showing real DI is even very poor example... PS: ChatGPT while being great.shouldn't (yet) being considerated a safe place to get answer...is a good start for sure an most of them are somewhat correct but..it does still LOTS of mistake so i wouldnt even brag about it as to say "ChatGPT answered this so is correct"
@federicobau8651
@federicobau8651 25 күн бұрын
​@@WebDevCody So then a question for you, tell me: What's the difference on what you did compared to .. passing variable to a function?
@WebDevCody
@WebDevCody 25 күн бұрын
@@federicobau8651 the function prior depended on a drizzle db object (implicit interface) which was used from a direct import at the top. Passing it in as a function argument allows any object that extends that implicit interface to be used as the dependency instead. An argument to a function is not a dependency, it’s just a parameter. Importing a third party library or another module from your codebase is a dependency. If the module is dynamically passed in, you’ve done dep injection
@federicobau8651
@federicobau8651 25 күн бұрын
@@WebDevCody first you are answering what i asked. Second.. emhh.. "emplicit" for the "interface of a DI"..is totally against what DI is for (one of few) and you really want to have it Explicit. Third, you are confusing a Dependency...you are calling a dependency "3rd party library" which is dependency of a Project..in DI (and SOLID/Clean code) dependency means..something else..you tottaly can have a something that is a dependecy in you code base..even on the same file XD..dependency here we mean "what the entity depends on" regardless of the source. Having said that, both your explanation and the code you wrote...while has some components used in DI..Is NOT ENOUGH and doesnt automatically make it DI. Dont take me wrong, i think your code is correct nor i am suggesting to use a typical DI (which many cases is ..overengineerng) in fact i think is the right thing to do but, nope you can't call that DI..no no no..and then if you have found this info online then..well means is a very confused and misunderstood topic. If you follow what you explain than...literally ANYTHING that is an object of some sort passed as argument of a function (or constructor) is ..dipendency injection? Common this is Ridículos. Then why isnt this already called DI on a 5th lesson of a beginner programmer of (most) programming languages when you first study how to declare and call a function? Why is a DI only found in a broader context like a library or lets say a web server? I tell you why.. because those 2 AREN'T the same thing. What you have done is pass an object to a function as argument which has a default value. Yes default value is different than the one you passed. Yes both have a method with same name but that ISNT DI. So you are telling me that, if you have a function called "count" with param fn that calls it as fn.count ..and you pass as argument an array of strings..or an object.. or a custom class thst has a method count..you call that DI :D ..common now..
@eliotfagnon7319
@eliotfagnon7319 29 күн бұрын
Owww what is your vscode theme?🥹
Dependency Injection | Prime Reacts
28:34
ThePrimeTime
Рет қаралды 310 М.
The most important function to write performant next.js apps
5:36
Web Dev Cody
Рет қаралды 16 М.
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 13 МЛН
WHAT’S THAT?
00:27
Natan por Aí
Рет қаралды 14 МЛН
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 36 МЛН
How I structure my next.js applications
23:19
Web Dev Cody
Рет қаралды 24 М.
Turborepo 2.0 - New UI + Watch Mode?!
6:09
Anthony Shew
Рет қаралды 7 М.
3 Coding Techniques To God-Tier Developer
5:31
Kantan Coding
Рет қаралды 67 М.
Every Framework Sucks Now
24:11
Theo - t3․gg
Рет қаралды 123 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 779 М.
Dependency Injection in JavaScript
9:37
Sam Meech-Ward
Рет қаралды 40 М.
How To Debug React Apps Like A Senior Developer
21:07
Web Dev Simplified
Рет қаралды 87 М.
Mastering Dependency Injection In Golang
14:29
Anthony GG
Рет қаралды 45 М.
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 13 МЛН