Below you will find pages that utilize the taxonomy term “Dependency Injection”
Posts
Contextual Dependency Injection Is a Myth
Sometimes in your daily programming life, you would want to inject different object instances based on the current route or module. For example, you want to connect to Database Foo for route /foo and Database Bar for route /bar. It seems a clever idea to do what is called a “contextual binding”, aka inject instances conditionally based on some runtime value.
In Laravel it looks like this:
<?php $this->app->when(PhotoController::class) ->needs(Filesystem::class) ->give(function () { return Storage::disk('local'); }); $this->app->when([VideoController::class, UploadController::class]) ->needs(Filesystem::class) ->give(function () { return Storage::disk('s3'); }); This code looks nice and handy at first glance, but in my experience, they are often doing more harm than any good.
Posts
Manual Injection
Came across this pic from twitter.
If I have an adequate number of audience, I would like to make poll out of it. What do you think about this piece of code?
A: It is good code. B: It is bad code. I have never done react before (Or I have, but not professional). However this piece of code is very readable to me. If I was tasked with the job to maintain this code, I would appreciate the original author for coding in this fashion.