0

I've been thinking about a simple software design problem. Imagine I am writing a web application to edit a tree of objects. Each node of this tree has an ID property that is filled in when the node object is POSTed to the backend. A user can create a tree hierarchy with multiple nodes before anything is sent to this backend, leaving all nodes with an empty ID field.

Now imagine that whenever I select a node of the tree in this application, the node object is passed to a method of a class that wants to associate different objects with individual nodes of a tree in an internal dictionary. Suppose I, for some reason, cannot depend on reference equality. Is there then a better way to identify unique nodes than giving them a temporary ID that the backend should ignore?

I don't see anything wrong with using temporary IDs that are ignored on the server, but I want to avoid changing our model classes. Another option I can think of in this particular context would be extracting the position of the node in the tree with some representation and using this as a key for the dictionary. While this would work, it is mildly complex to implement and not very efficient.

  • 1
    What's wrong with having a node ID? You are treating nodes, not elements within the node. You have to work at the proper level of abstraction. If you need to identify nodes within a tree, give them an id. It could be the hashcode of the element they hold or a coordinate (Level, Node). – Laiv Jun 4 at 7:37
  • The tree in the user's client is not the same tree as is held in the server. The client tree is derived from the server's tree, but it is not the same. At best it only appears to be similar. Therefore the client tree can have behaviours not found in the server tree, such as node identifiers. Similarly the server's tree can have abilities not found in the client tree, such as persisting it to a database. – Kain0_0 Jun 5 at 0:09
0

I can see many solution but at first you should separate models. One for the client side and another for the server (and some mapping). Then you can for example have a flag new used for not-saved data. Another option like this written here (hash or Id).

  • Honestly, I worked on this way too long and have reached exactly your conclusion: the cleanest workaround is to have the server model as the DTO and than at the boundary on the client side you wrap and unwrap that DTO with whatever you need by creating a wrapper class. – Joshua Schroijen Jun 6 at 12: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.