Public api:
getClients / getClientById - returns a json object like:
{ clientid: 1, name: "Client1" }
getProjects / getProjectById - returns a json object like
{ projectid: 5, name: "MyProject", clientid: 1}
Question: How do you design a library and set of objects to interact with this API specifically with the problem of how project contains the clientId. What I keep clashing against is that the project object contains a clientid so do I build my project object with just an ID or also a client object i.e
class Project
{
int projectId ...
string name ....
int clientId....
Client client // Not sure about this pattern as it wont always be initialized?
}
As a couple of solutions/ideas you could:
- Force Project to take a Client object in the constructor but now you force a hit to the API before creating a project.
- Not have the Client member in Project but now to use the library but does this make the object system harder to use?
- Have the project object go grab the client from the API as needed
Concerns
- Every time you are in a project object having to do a trip to the public API to get the client name.
- When you are in a project object the client reference could be null (if you include it).
- If the project knows how to get a client via the API the object now our code is becoming a jumbled mess.
How I handle this up to now
Keep the client and project objects as close to the API definitions as possible and in my API wrapper class build in some caching so I am not as bothered about calling it to get the client everytime I need it. Code is still not great though because it ends up looking like:
var project = apiWrapper.getProject(5)
out("This project belongs to client " + apiWrapper.getClient(project.clientId)
// note this call to getClient has cahcing in my implemtation