1

I have an Invoice object wich may contain several FacturedServices objects stord as a list.

In may interface, the user can edit an Invoice object, delete add or edit its FacturedServices list.

Whe the user hit the save button, the updates made to Invoice object should be passed to the database as well.

The problem is the FacturedServices list will contain heterogenous data, because some of its elements are new, others changed and some of them have been deleted from the list.

What is the best way to implement hte update of the Invoice object in the database ?

I was thinking to add a dirty property on every FacturedService object, whenever it is changed, or set the property to "new" when its newely created and then do some if else in my controller :\

If you think my question should be rephrased, please do it

EDIT : Say I have an object Invoice I , It has 3 FacturedServices FS1, FS2, FS3

In the interface, the user edited FS1, deleted FS2, and added FS4 to the list

now the list contains edited FS1, unchanged FS3 and newly created FS4.

in my databse, I have an InvoiceTable and a FactureServicesTable whith a referencial inetgrety to the invoice id.

my problem is in my controller there must be a way to know which FS is new which one is updated and which one was deleted, so I can execute the correct SQL commands depending on the situation.

I know this is not something new, in every desktop application at some point there should be something similar to my problem. so what is the best way to do this.

  • So are you looking for a method to help you manage updating of the an Invoice object when a FacturedServices is updated? – Elliot Blackburn Dec 15 '14 at 17:08
  • see my edit :) ! – user3147619 Dec 15 '14 at 17:17
1

I would add a duplicate() method to invoice, so that the edited invoice is always a copy of the original.

If the user cancels editing, the duplicate gets discarded and nothing else happens.

If the user submits their changes, then I would compare the edited invoice against the original invoice to figure out what fields should be updated on the database, and I would also walk the respective lists of features to see which features have been added, which heve been modified, and which have been deleted.

Bonus:

An optional implementation of the comparison function which does not do any saving to the database can also be used to calculate at any given moment the enabled state of the 'ok', 'apply', and 'revert' buttons of the invoice editing form, as well as the text of the (always enabled) 'cancel' button.

When the edited invoice is identical to the original, the 'ok', 'apply' and 'revert' buttons should be disabled, while the text of the 'cancel' button should be "close".

When the edited invoice differs from the original, the 'ok', 'apply' and 'revert' buttons should be enabled, while the text of the 'cancel' button should be "cancel".

1

I just will save the opearations made by the user and the concerned objects in a Map <String, List<FacturedServices>>. If a user delete a FacturedServices, it will be added to the map with a "DELETE" key. Then I will iterate over the map and execute delete, update or create commands according to keys. A more professional solution would be to implement a command pattern, but this is just fine for the moment, if you think of something better let me know, I will be pleased to discuss it.

Thank you for your time

1

Why not just pass the entire list into your database (restructured as a user-defined table type)? Then you can iterate over it in the stored procedure and take the appropriate action? If the row exists in the database, update it. If it exists for that invoice in the new table but not in the database, insert it. If it exists in the database but not in the new table, delete it.

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.