Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Separating back-end from view controller logic

In a project I'm working on I'm using Parse.com as a back-end. I had developed several screens with various loading and saving functionality and I started thinking about how difficult it would be to replace Parse.com with another back-end solution (if the need arose). This lead me to doing a pretty hefty refactor of the existing codebase.

I'm not sure if my approach is something to emulate as this is my first iOS app, but in case it has some merit I thought I'd share it here.

My goal was trying to limit the scope of my changes on the iOS app side if I needed to move to a vanilla web server back-end or another BaaS like StackMob etc..

So I decided that all Parse.com methods should go into only one class which I called Backend. The idea would be that all the changes required would be limited to this class. So there were a few things I needed to do:

1) I had to make sure that my data model objects had a clear interface and that nothing came out of the Backend method calls as a Parse-related type, only as app-specific types that I had created and that would not change from implementation to implementation.

2) All the actual method calls that talk to Parse occur only in the Backend class. So my User class will have an instance method called "save" and that would simply call [Backend updateUser:self] or similar. All the methods were class methods as I didn't want to bother creating an instance of the Backend class.

3) In my Backend class I would have methods to convert into the Parse objects and back to my application-specific types.

4) I did make one concession to Parse that I can probably work around once I think about it more, but I do store the Parse object as a property in my application object just for the sake of having its key. I don't do anything with it in the application outside of the Backend class except to declare it as a property. The property is simply called backend and I could possibly change it to just store the Parse objectId instead - but it's a small concession which won't hurt me in a transition. To the application itself, it does not matter whether that property has a value or not.

The other good thing about having the Backend class is that it's immediately apparent to me exactly what needs to be re-implemented during transition as it can serve as it's own interface spec.

Of course, actually replacing the functionality in the back-end can be more complicated, but that's exactly why I wanted to simplify the changes on the app side.

I don't know if this resonates with anyone and I guess it could be considered a bit unnecessary, but I really hate feeling tied to a specific service. The amount of work it took to do all this at these early stages was enough that I could see it becoming impractical to try to refactor it after the app grew past a certain stage.

Anyway - for what it's worth.

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

What you are talking about is known as the Facade design pattern. There's a really good book called Cocoa Design Patterns if you are interested in refactoring code to make it maintainable. I agree with mostly everything you mentioned except if you are using the Parse SDK then you are still using their objects. If you truly wanted to decouple from Parse then you can refactor your code to use their REST API which then you could potentially swap out for any other service with minimal work.

Ah yes. I've come across Facade design before for web api solutions, but have never implemented it myself.

What you're saying about REST API makes sense, but just confirming something. If I use the REST API for Parse, then in a situation where I replace with another backend that uses a similar HTTP type of API, I simply have the change the URIs I'm using for the call. It's likely that the objects returned (probably JSON) can be modelled on the back-end so that they remain similar and can be consumed by my app with little-to-no modification. Is this what you mean?

I had a bit of a look at the docs for REST API and I think it's giving me a hint about another question I asked on the forums about decoupling the Facebook/Parse authentication and authorisation process. I am only using Facebook for sign-up/log-in and I was trying to get an idea on the approach to handle it outside Parse.

Thanks for the suggestion.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

It's exactly what I meant when I was referring to the REST API. Most services are RESTful and since you are modeling your data on a BaaS or even your own server side code then most likely the JSON will be the same. Theoretically all you might have to change is the URI and authentication.