Is it best practice to use a non-persistent PageContext to manage UI state and sync it with persistent UserPreferences?

0
Hi everyone, I'm working on a use case where I want to store user preferences like selected values for fields  in the database, so they persist across sessions. Currently, I use a non-persistent entity (PageContext) to manage the selected values and UI state during a session. Now I'm introducing a persistent entity (UserPreferences) to store the same values long-term. This means some attributes (like selected team, language, etc.) exist in both entities. I understand that UserPreferences might eventually include settings that aren't relevant to the UI context, but for now, there's a lot of overlap. My question is: Is it okay to duplicate these fields and associations between PageContext and UserPreferences? Or is there a cleaner pattern for syncing UI state with persistent user settings?   Thanks in advance for your insights!
asked
2 answers
0

Hi Sebastiaan,

 

As Michiel mentioned above, it is better to only use persistent object in this case, so if user selects something, user will save it and that is it, if the user does not want something, he can cancel changes. So no need for additional non-persistent entity.

answered
0

Hello Sebastiaan ;) 

 

In my opinion this is a great pattern you've described. 

 

The two entities, while sharing some data, serve fundamentally different purposes and have different lifecycles. Trying to merge them into a single entity can lead to a messy and fragile system.

 

This approach follows the Separation of Concerns principle, which is key to clean software architecture.

  • PageContext (The "Working Copy"): Its job is to manage the current state of the user interface. It's a short-lived, in-memory object. It needs to be highly responsive and reflect immediate user actions, like selecting a different item from a dropdown, even if that choice isn't meant to be saved yet.

  • UserPreferences (The "Saved Master Copy"): Its job is to provide long-term persistence for a user's default settings. It's fetched from the database and only changes when the user explicitly saves their preferences.

image.png

answered