0

I am trying to figure what the best practice with loading data from web apis is.

Assume you have a model structure as per below:

class School {
private string name;
private List<Classroom> Rooms;
}

class Classroom {
private string Type;
private School school;
private List<Student> Students;
}

class Student {
private string Name;
private int Age;
private Classroom Room;
}

Is it better practice to load a single school entity and include all the related data (School + classrooms + students), then use the entity set at client side or is it better practice to just load the individual entities depending on what you need i.e. get School when school info needed, get Classroom when classroom needed so forth.

I can see pros and cons to both but keen to know if there is best practice.

3

Eager or Lazy Loading

If you know exactly how the data is used, you can easily decide, if eager loading (load everything at once) or lazy loading (do a second request if needed) gives better average performance.

Let the Consumer Decide

If your platform is more generic, so you don't have the information to optimize the data retrieval / transport, you should leave the decision to the API consumer.

Request Parameters

You could allow request parameters that specify, which related data is transmitted:

api/school/:name?with=rooms,rooms.students

GraphQL

Another way is GraphQL, which allows you to specify the requested data in the request body.

api/school/:name

and the body to retrieve everything

{
    school {
        name
        rooms {
            type
            students {
                name
                age
            }
        }
    }
}
2

The best practice here is going to depend entirely on what your specific requirements are.

For example, your software might perform better if everything is loaded all at once. This is especially true if the data is being retrieved from a web service, where retrieving relatively large chunks of information is preferable to enduring the overhead of multiple service calls.

On the other hand, the related data might be infrequently accessed, which means that you would be wasting precious bandwidth and clock cycles retrieving data that is hardly ever used. In that case, it's better to defer retrieving the related data until it is actually needed.

Software development is a constant exercise in tradeoffs. Seldom is there a "right way" to do anything that applies to every possible situation.

  • Appreciate i might look into my setup then and see what might be best fit – Aeseir Nov 27 '17 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.