A question about dependency injection: I understand the idea of initialising a controller with a pointer to a data model when the controller is created:
Controller *controller = [[Controller alloc] initWithDataModel:[self dataModel]];
But I was curious to read an answer on here by TechZen that mentioned passing a m开发者_开发问答odel between controllers.
TechZen "Dependency Injection relies on passing the data model object from view controller to view controller as needed." Cocoa touch connection and data design pattern
Does this simply refer to the fact that each controller is initialised with a pointer on creation, or is it referring to assigning / swapping the data model at a later date maybe using a pointer to the new model passed in via a method?
- (void)connectToModel:(id)newModel;
The quote is not really clear to me, but the basic principle of Dependency Injection is that the controllers get the model provided by somebody else (= injected), they do not seek it themselves. The model may be supplied when the controller gets created (in the initializer) or it may be supplied later, through a setter. Both scenarios are equal as far as the basic idea of DI goes.
精彩评论