Question

If a view has about 10+ fields and the user changes the value of a couple of them, I need to identify the changed values and only update those values in the database.

The only solution that I can think of is when the view is loaded, clone all the related properties in the view model and bind the view to these cloned properties. This way, when the user tries to save the updated values, I can compare the cloned property with the original one to see what has changed.

I cannot just use MVVM binding to the properties in the view model as I would not know what changed over time when the save command is executed.

Is there any better/elegant way to handle this?

Was it helpful?

Solution

You can combine some thinks from evented ui's and things that are famous in the ddd space when dealing with event sourcing.

Imaging your model is a series of events. So you got a model which holds the state but will be constructed each time a change happens. The advantage is you do not only know your current state you also know where you come from and you can replay each state again (if you save the events).

So think about it as a bank account. You do not see only the balance you see the history as transactions.

I think two-way-binding will not work here, but you should bind to your state-object and on click (or focus out) you add an event like CustomerMoved or TitleRenamed (with the fields which are changed plus the reason) as soon as an event occurred you calculate new state (result is an updated state object). And whenever the state-object is updated you should fire NotifiedChanged, so UI will represent new state.

I don't know if it makes sense for you or it is an overkill? The Advantage is when you store the domain events you will record a perfect audit trail. This is a different mindset but depending on your use case it can help.

I would suggest to read up on event-sourcing or cqrs. Remember those patterns are not a black or white decision. You can take parts from it whatever fits for you...

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top