Let's say we have a
Book that has a
Authors. Order of
Authors is important.
We then have the update page, where user can 1) add new author, 2) remove existing author or 3) change the order. What I have as the input is the list of new authors.
I find this problem often - there is often a lot of models with this kind of relationship. Sometimes the order is not important (so we use
Set) but more-less is like this.
In my business method, I need to:
- detect new
Authors, so I can insert them
- detect removed
Authors, so I can remove them
- assume everything else is updated.
I can not simply reapply authors (delete and then insert) since there is additional data related to a book and author.
Doing this manually every time is error-prone. Is there a better way to track changes in related collections?
Should I track changes on UI instead (as this is where they happens) and then provide more input then just one list of all users: for example, list of changes, list of removals? In that case my business method would take more parameters, i.e. lists for each type of change (removal, etc).
Finally, I was thinking into storing set of events per update, so I can repeat them on model easily.
Should I handle this margining in model or in my service method?
Any wisdom on this?