I want to use Parse (parse.com) in my app. Parse uses PFObject models. I’d like to use my own models throughout my code (so that it doesn’t depend on parse). If possible I’d like to design my app so that I can replace parse with another cloud service as seamlessly as possible if I wanted to.
Is this a good idea? What’s the best way to abstract the model storage so that there is no (or minimal) traces of Parse code in my app?
Perhaps use the adapter design pattern to map parse objects to my own objects? Should this be an independent class or part of the model logic?
If anyone has tried something like this I’d like to hear your thoughts. Should I just use parse models directly in my code? Or perhaps a singleton factory to generate my models based on parse objects?
Any tips/thoughts/comments ?
I’ve found relatively clean way to manage this.
Basically I’ve created a protocol called
NPDictionaryRepresentingwhich classes can conform to in order to specify how they should be converted into a dictionary or initialized from a dictionary.Each of my models that I need stored in Parse will conform to this and implement their own custom behaviour. This protocol is abstracted through the use of dictionaries so it doesn’t depend on Parse in any way.
Then I’ve implemented a NPNetworkAdapter base class to handle all network storage. I’ve also implemented an NPParseNetworkAdapter class which inherits from NPNetworkAdapter. This is the only class which knows anything about Parse. Its interface deals with objects that conform to NPDictionaryRepresenting. The parse network adapter is able to create PFObjects by extracting dictionary representations of my objects. Conversely it’s able to fetch PFObjects and give me back my own models by instantiating them using dictionaries.
The drawback with this implementation is it doesn’t work very well with object relationships (but I’m working on it).
If anyone has any comments on this approach I’d love to hear them.