vendredi 6 novembre 2015

Best approach for creating a generic REST response for database operations

I'm creating a REST api specifically for serving my android application requests. Some of the operations exposed by the API just queries the database and returns data to be displayed by the app. However, I would like these operations to return a generic REST response, so that the data queried from the database are displayed on the UI without the need to create a response attribute for each field.

For example, i have an api operation which returns a list of transactions from the database which are displayed inside a list view.The attributes shown inside each list view item depends on the type of the transaction. So i thought of two approaches for constructing my ResponseTransaction class:

1- Just create an attribute for each database field which needs to be displayed: id, description, amount, time, date, type ... and based on the type , android would detect which attributes to display inside each item.

2- Second approach is to tell android what to display through returning a generic response holding: HashMap(Label, Value) where values represents necessary database fields.

By using the first approach, I should update apk each time i add a new transaction type.However this is not the case in the second approach which just requires a new service deployment.

So do you think that the second approach is the best way to go for creating generic REST responses for the database operations and is it a good practice to couple REST responses to the client if we are sure that the service is only used internally by our android or web application.

Please advise.

Aucun commentaire:

Enregistrer un commentaire