samedi 15 septembre 2018

Design Pattern for sending API requests with parameters

I'm working on an app that interacts with a RESTful API. I'm using a library that serializes objects into json POST requests, and can think of a couple different ways of designing my code to handle creation of these requests. I was curious if anyone had an idea on what the "best" way of going about it would be.

I have a Database class that handles the actual API calls, the ways I've thought of interacting with the Database class were:

  1. Create the request object corresponding to the desired API call, and send that as an argument to the Database class in the proper method.
  2. Same as 1., but the request objects have a method to send themselves to the Database class, so the working code never interacts directly with the Database class, just with the request objects.
  3. Send each field as method arguments to the Database class, the Database class then handles creation of the request objects. Working code strictly interacts with the Database class, and never touches the request objects.

I think I like 2 or 3 better, because it simplifies interactions between different objects, but can't really think of any methods that seem clearly better than the others. Maybe there's a better way of doing it I can't think of?

Aucun commentaire:

Enregistrer un commentaire