I’m currently developing an app using Parse and I’d like to start abstracting their SDK as I don’t know if and when I’m going to replace their backend with another by other provider or by ours.
Another motivation is separating issues: all my apps code will use the same framework while I can just update the framework for any backend specifics.
I’ve started by creating some generic classes to replace their main classes. This generic classes define a protocol that each adapter must implement. Then I’d have a Parse adapter that would forward the calls to the Parse SDK.
Some problems I can predict is that this will require a lot of different classes. In some cases, e.g. Parse, they also have classes for dealing with Facebook. Or that the architecture in some parts can be so different that there’ll be no common ground to allow something like this.
I’ve actually never went so far with Stackmob as I am with Parse so I guess the first versions will share Parse’s own architecture.
- What are the best practices for something like this?
- Is there something like this out there? I’ve already searched without success but
maybe I’m looking in the wrong direction; - Should I stick with the Parse SDK just making sure that the code using
it is well identified and contained?
I’m the Developer Evangelist at Applicasa.
We’ve built a cool set of tools for mobile app developers, part of which includes offering a BaaS service that takes a bit different approach compared to Parse, StackMob, and others. I think it provides a helpful perspective for tackling the problem of abstracting away from third-party SDK APIs in a way that would allow you to replace backends by other providers or your own.
/disclaimer
While there are other BaaS providers out there that provide similar and differentiating features, I’m not aware of a product out there that completely abstracts away third-party providers in an agnostic manner.
I think you already show to be on a solid footing for getting started in the right direction.
First, you’re correct in predicting that you’ll end up with a number of different classes that encapsulate objects and required functionality in a backend-agnostic way. The number, of course, will depend on what kind of abstraction and encapsulation you’re going after. The approach you outline also sounds like the way I’d begin such a project, as well—creating classes for all the objects my application would need to interact with, and implementing custom methods on those classes (or a base class they all extend) that would do the actual work of interacting with a backend provider.
So, if I was building an app that, for example, had a
Foo,Bar, andBazobject, I’d create those classes as part of my internal API, with all necessary functionality required by my app. All app logic and functional operations would only interact with those classes, and all app logic and functionality would be data backend-agnostic (meaning no internal functionality could depend on a data backend, but the object classes would provide a consistent interface that allowed operations to be performed, while keeping data handling methods private).Then, I’d likely make each class inherit from a
BaseObjectclass, which would include the methods that actually talked to a data backend (provider-based or my own custom remote backend). TheBaseObjectclass might have methods likesaveObject,getById:,getObjects(with some appropriate parameters for performing object filtering/searching). Then, when I want to replace my backend data service in the future, I’d only have to focus on updating theBaseObjectclass methods that handle data interaction, while all my app logic & functionality is tied to theFoo,Bar, andBazclasses, and doesn’t actually care how get/save/update/delete operations work behind the scenes.Now, to keep things as easy on myself as possible, I’d build out my BaaS schema to match internal object class names (where, depending on the BaaS requirements, I could use either an
isKindOfClass:orNSStringFromClass:call). This means that if I was using Parse, I’d want to make mysavemethod get theNSStringFromClass:of the class name to perform data actions. If I was using a service like Applicasa, which generates a custom SDK of native objects for data interactions, I’d want to base custom data actions onisKindOfClass:results. If I wanted even more flexibility than that (perhaps to allow multiple backend providers to be used, or some other complex requirement), I’d make all the child classes tellBaseObjectexactly what schema name to use for data operations through some kind of custom method, likegetSchemaName. I’d probably define it as aBaseObjectmethod that would return the class name as a string by default, but then implement on child classes to customize further. So, the inside of aBaseObjectsavemethod might look something like this:Then in, say, the
Fooclass, I might implementgetSchemaNamelike so:I hope that makes sense.
Making an internal abstraction like this will be a fair amount of work up front, but it will inevitably offer a lot of flexibility to implement as you wish. You can implement CoreData, reject CoreData, and do whatever you’d like really. There are definite advantages to building internal app logic/functionality in a data-agnostic way, even if it’s to allow yourself the ease of trying out another BaaS in, say, a custom branch of your app code to see how you like another provider (or to give you an easy route to working with developing your own data solution).
I hope that helps.