Preserving the notion of inheritance in a REST-based web-app
Preserving the notion of inheritance in a REST-based web-app
We are building a new Single-Page frontend to replace the old static html frontend (with some jquery) of our web-app. While doing so we run into a problem concerning the type of objects we get back from the api.
When exporting data as JSON from our PHP backend we include an extra field in which we put the type the object had before exporting. We can use this to handle different subclasses of the resource we requested.
However sometimes we would also like to differentiate on parent classes. Consider the following scenario:
Inheritance tree:
Animal Mammal Human Dog ... Reptile Crocodile Snake ...
GET /animals/
[ {"_type": "Human", "id": 1, "food": "Ice Cream"}, {"_type": "Human", "id": 2, "food": "Steak"}, {"_type": "Human", "id": 3, "food": "Peanut Butter"}, {"_type": "Dog", "id": 4, "food": "Horse Poop"} {"_type": "Crocodile", "id": 5, "food": "Humans"} ]
When I have a certain piece of UI in which I would like to display all the animals but for instance highlight all the mammals I have a problem because I have lost the knowledge which animals are mammals.
The easiest solution we have come up with is to include the inheritance chain when exporting the objects from our backend. Possibly like this:
{ "_type": "Human", "id": 10, "food": "Ice Cream", "_parentClasses": "Animal.Mammal.Human" }
While I don't consider this a particularly nice solution it is easy to implement and use and seems robust.
Do you think this is a good/ok/bad idea and how have you tackled this problem?
Answer by ilikemypizza for Preserving the notion of inheritance in a REST-based web-app
I can see several ways of possibly achieving this. You could develop your REST server to accept URIs such as:
/animals/mammal/humans /animals/mammal/dog /animals/reptile/crocodile
Or, simply return the type of object as you have stated above. Build the object off of the type.
Just my two cents on how I may solve this problem.
Answer by Westie for Preserving the notion of inheritance in a REST-based web-app
Not sure if this qualifies an as answer, really (or even that the question qualifies as a question!!), but I used a similar process on a time manager a few years back; the inheritance paths done in a similar way. I found it extremely easy and more intuitive to work with than some of the alternatives which would basically cause far more work down the line.
To extend the simplicity I also kind of broke the normal rules of OOP by allowing not only the parent to know the children, but also the children to know the parent. It does add a little to the code but it's workable, and, again, makes the whole process less painful.
Answer by David L for Preserving the notion of inheritance in a REST-based web-app
It seems to me that the very thing that is causing your heartache is the flattening of your objects on output. Perhaps GET/Animals should return something more like the following:
[ {"Mammals": { {"_type": "Human", "id": 1, "food": "Ice Cream"}, {"_type": "Human", "id": 2, "food": "Steak"}, {"_type": "Human", "id": 3, "food": "Peanut Butter"}, {"_type": "Dog", "id": 4, "food": "Horse Poop"} }, {"Reptiles": { {"_type": "Crocodile", "id": 5, "food": "Humans"} } ]
If you can preserve the structure of your objects on output, when you send that complex object back to the server your structure should remain intact.
Answer by Francis Avila for Preserving the notion of inheritance in a REST-based web-app
There are many options. Here is one that I haven't seen mentioned:
Make your _type
an array instead of a string, with the first item containing the current class and the rest parent classes.
{ "_type": ["Human","Mammal","Animal"], "id": 10, "food": "Ice Cream" }
This should be easy to implement on the PHP side with this:
$_type = array_keys(class_parents($this)); array_unshift($_type, get_class($this));
Answer by Rohan for Preserving the notion of inheritance in a REST-based web-app
Many good answers, here's another option you might want to think about.
You only send the subclass to the client in JSON, the client then does a lookup whatever its baseclasses are. It moves some complexity from the serverside to the clientside and you keep your JSON messages clean.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment