mercredi 22 juin 2016

Passing data down the hierarchy


I read a lot of topics about principles for writing testable code. I'm working on using Dependency Injection in my application and I'm facing a very big problem. In the top ViewController, I'm loading an array of objects and the user can select on of them to go deeper in the hierarchy. The thing is that I need the selected object in some controllers (including the leaf controller) and for some I don't need.

In picture, it can be like this :

NeedObject    ->    don't need    ->    don't need    ->     need
TopController -> otherController1 -> otherController2 -> leafController

For now, I'm passing the selected object through all the hierarchy, but I can feel how it's bad as it does not respect the principle stating that an object should only know about what it needs. But I can't figure out how to respect this principle.

Note that the objects are all children of ManagedObject and are stocked in CoreData. I thought about adding like a boolean isSelected in the class definition and then use an object that would request CoreData for the object with selected = YES, but I'm not sure about it as the "selectability" of the object should not be something persistent, right ?

Thank you for your help


Aucun commentaire:

Enregistrer un commentaire