best practices for "data layer" in android client apps
Asked Answered
P

2

14

Here is one design/ best practices question..

I'm new to android development, and basically new to web/mobile solutions.

So, my question is - what are best practices when organizing structure of android application that get data from the remote server?

Should request to server go into one class that does communication with server (get and post requests), or should I look at my requests as data source, meaning that every data class manages it for itself?

or should I have more levels of abstraction - one level for acquiring data, other for model that uses some interfaces without knowing from what source data come from?

I'm curious how experienced android developers approach to these design issues...

Proportion answered 13/11, 2012 at 3:39 Comment(0)
F
10

Virgil Dobjanschi presentation is a good resource as pointed earlier, which basically tells you to run your requests from a background service so the activity does not get destroyed and to store your data in the database as early as possible.

For more technical details, the way I am doing it is to divide the app into three components:

1- Library to encapsulate the handling of the HTTP request and response (with ApacheHTTP), which can handle simple request/response and advanced features that might involve cookies (can be necessary for login) and modifying the HTTP header.

2- Marshal/Unmarsha layer, where I parse the server data (e.g. XML or JSON) and convert it to objects (i.e. models) that the rest of my app will deal with.

3- Persistence layer.

As per Dobjanschi's presentation, I usually make data requests run in a service not in a thread worker inside the activity.

Finite answered 13/11, 2012 at 4:6 Comment(2)
This is great video, thank you! Now, I'm trying to implement it, specially pattern B (activity-> contentProvider -> ServiceHelper -> Service -> Rest -> Processor), and now I'm having hard time figuring out how this should be done. I don't understand what is granularity of this service, for example, if I have my model objects, or database table called User, UserDetails, City, Category, etc... should I have these classes for all "data types", or specialized?Proportion
For example,should I have UserProcessor, CategoryProcessor (etc.), UserService, CategoryService (etc.) UserContentProvide, CategoryContentProvider (etc.)?Proportion
I
6

Use one of the 3 models presented at this Google I/O talk. It provides you suggestions that will help you out on the whole process of definition of your app architecture. It'll also prevent you from making common mistakes beginners use to make:

http://www.youtube.com/watch?v=xHXn3Kg2IQE

This post will also help you out:

Need sample Android REST Client project which implements Virgil Dobjanschi REST implementation pattern

Intubate answered 13/11, 2012 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.