nsscreencast / comments

Public comments on NSScreencast.com
0 stars 0 forks source link

Dependency Inversion Principle #166

Open subdigital opened 3 years ago

subdigital commented 3 years ago

Written on 01/17/2018 10:51:36

URL: https://nsscreencast.com/episodes/316-solid-dip

subdigital commented 3 years ago

originally written by Roopesh Manjunatha on 01/08/2018 06:07:25

Hi Ben, There's no code uploaded on Github for DIP. Please do. Thanks.

subdigital commented 3 years ago

originally written by Ricardo on 01/26/2018 17:50:03

Hey Ben, great video and series!

What do you think on having property injection but at the same time the property has assigned a default value to the singleton.

class TodoViewController: UIViewController {
var todoStorage: TodoStorage = TodoManager.shared
...

It still has an implicit dependency, which is not good. But at least is easily replaced for something else in tests or whatever.

subdigital commented 3 years ago

originally written by subdigital on 01/29/2018 08:59:56

You hit the nail on the head. The concrete dependency is still there. That said, this is a good first step to enable testing. If you can, I think it's worth it to go one step farther and try to program to an abstraction.

subdigital commented 3 years ago

originally written by subdigital on 01/29/2018 09:01:40

Sorry Roopesh, I missed this comment! I have uploaded the source now. Thanks for letting me know.

subdigital commented 3 years ago

originally written by Ricardo on 01/29/2018 09:27:58

Not sure I understand. I'm actually programming to an abstraction, right? The var type is `TodoStorage`, which is the abstraction. All the code in that class will reference the abstraction. But it has a default value of the specific type... I'm not sure at all about this approach but it is so easy to do, that I like it. I get a code that could get dependency injection but I don't really need to pass the dependency around through all the layers of my app just to get to this VC.

The main problem I see is that the dependency is implicit, so it's kind of hidden from the users of the class...

Anyway, thanks!

subdigital commented 3 years ago

originally written by subdigital on 01/29/2018 09:33:43

Not entirely no -- the var type is the abstraction, but you've made it so that you cannot ship a version of this class without a working version of the singleton. If these were part of a separate framework, for instance, then this could be an important distinction.

The problem of passing around dependencies is a good topic for another screencast. There's pain/annoyance here that leads us to using singletons, when managed instances would be preferable. I have had some good success by using protocols and passing dependencies through tab bar controllers and navigation controllers with default implementations.

With all of that said, sometimes the pragmatic approach is to use the singleton like you describe to take a step closer to decoupling things, but I try to avoid singletons wherever possible.

subdigital commented 3 years ago

originally written by Ricardo on 01/29/2018 09:38:04

Aha. I understand now. A video on the problem of passing around dependencies without making the method signatures too long and crowded would be great then ;)