Created
July 9, 2015 01:56
-
-
Save nsforge/bba2fb28f9d745ff2c33 to your computer and use it in GitHub Desktop.
Related Object Lookup Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Person | |
// Derived from JSON | |
@property (readonly) NSString *personID; | |
@property (readonly) NSString *name; | |
… | |
@end | |
//////////////////////////////////////// | |
@interface Company | |
// Set on object creation | |
@property (readonly, weak) PersonManager *personManager; | |
// Derived from JSON | |
@property (readonly) NSString *companyID; | |
@property (readonly) NSString *generalManagerPersonID; | |
// Convenience Methods | |
@property (readonly) Person *generalManager; // returns [self.personManager personForPersonID:self.generalManagerPersonID]; | |
@end | |
//////////////////////////////////////// | |
@interface PersonManager | |
@property (readonly) NSArray *persons; | |
// Initially, this will be a naive linear-search, requiring no extra state or configuration. | |
// If it turns out to be a performance hotspot, these query methods can be optimised by | |
// adding an NSDictionary cache (keyed by Person.personID), which will speed up all model-relation | |
// lookups that use the given method. | |
- (Person *)personForPersonID:(NSString *)personID; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment