When using "Model" Controllers and passing them between VCs using dependency injection, how to handle the fact that some controllers need to know an information from another controller?
Here's a concrete example, with a fictive app having 3 model controllers:
- ContentStore: loads and holds the content of the app, specific for the user logged in.
- UserManager: contains a User object, and logs user in/out, updates info for the logged in user, etc.
- NetworkController: the only controller to know about web service.
ContentStore and UserManager will hold a reference to the Network Controller, to call the web services without knowing their implementations (to respect the Separation of concerns and Single Responsibility principles). The AppDelegate creates the NetworkController and then passes its reference to ContentStore and UserManager:
AppDelegate - didFinishLaunchingWithOptions:
let networkController = NetworkController()
let contentStore = ContentStore(networkController: networkController)
let userManager = UserManager(networkController: networkController)
// Inject contentStore and userManager into the first VC
But let's say the ContentStore wants to load the app content tailored for a specific User logged in. It needs to know its User ID. And NetworkController will need to know the User Id as well, to pass in the web calls.
What would be a clean way to achieve this, sharing information between controllers? From the different ways I could come up with, the only one that doesn't seem too bad is:
All ContentStore and NetworkController methods have a userId parameters, and they never call these methods themselves, they are always called by a VC. That VC can then pass the user ID, since it knows about both UserManager and ContentStore.
Is there a cleanest way than this suggestion?
Aucun commentaire:
Enregistrer un commentaire