lundi 10 août 2015

Database model vs working view-model in web apps conundrum

I've been working with Angular recently and so far it's been really a revelatory and great experience, however I noticed that I'm now faced with a similar problem that I've faced in all web apps.

Is there a particular design pattern you can use to avoid constantly having to worry about the current working view-model versus the database model?

For instance, in the webapps I've worked on, I use GSON to retrieve the database-model from Java and into the Angular view-model. I expose the model to Angular exactly as the model is represented on the database and try to have the saving process be symmetrical (serial/deserialization creates identical objects). However, I'm constantly encountering the same exact design problem which makes my code become extremely complicated:

  • I retrieve some row of data which has some status (success/fail/verify)
  • I have some button on that row of data which is binded to that same model property (the one that has success/fail/verify) and directly modifies it (so that saving is simply sending that object to the backend as-is)
  • Then when I go to save, I am not sure whether the value of this attribute is a new/old value (or if it simply is just persisting from the original load)

Is there some kind of design pattern that avoids this problem? I've thought about simply creating an immutable copy of the model the second Angular gets a hold of it, and then doing some diffing of the view-model to the original immutable copy to see what actually needs to be done on the database side but that's also tough because I might mark up the Angular view-model with extra attributes for utility that the original immutable version would never have (nor should I check for it)

Sorry, this is quite a vague question, I am just not sure of a good name to give this kind of design issue but it seems to always come up in web-apps I've worked on.

Aucun commentaire:

Enregistrer un commentaire