supermarin / ObjectiveRecord

ActiveRecord-like API for CoreData
MIT License
1.29k stars 194 forks source link

Rethink mappings #88

Open stephencelis opened 10 years ago

stephencelis commented 10 years ago

I see a few issues with the way mappings are currently implemented:

  1. They are automatically, categorically added to all NSManagedObject classes.
  2. They are coupled to the NSManagedObject+ActiveRecord category.
  3. They are unidirectional (there's no way to cast local properties back into remote ones).
  4. They are declarative (and with @kattrali's #66, the dictionary can become quickly bloated).
  5. They do a lot by default (auto-camelizing, for example).
  6. They are a memory leak (we cache every unique remote key that we check for).

Let's explore ways to address these issues.

Proposal 1: Protocol-based Mappings

One thought is that we could provide a protocol, like NSCoding, that could be used to serialize/deserialize these mappings. E.g.,

@protocol ORCoding <NSObject>
+ (NSDictionary *)dictionaryFromRemoteDictionary:(NSDictionary)dict;
- (NSDictionary *)remoteDictionaryRepresentation;
@end
  1. One would have to manually opt-in to the protocol.
  2. Looser coupling, using conformsToProtocol:.
  3. It would enforce bidirectional mappings.
  4. Method logic is more imperative.
  5. It's not included by default, so there are no defaults.
  6. No memory leaks (that are our fault, anyway)!
stephencelis commented 10 years ago

Relates to #68.

stephencelis commented 10 years ago

/cc @supermarin

dzenbot commented 10 years ago

+1 That approach looks really nice

supermarin commented 10 years ago

@stephencelis I think that makes sense