Hi there,
I have a Questionnaire, which has questions. It's a very simple data model as I'm sure you can image, with a PK - FK join between the Questionnaire (parent) and Question (child) tables.
Modelling this in C#, I have a Questionnaire God-object which has static methods to Create, Get, List, Update, Delete the Questionnaire instance(s). The List method returns an IList of Questionnaires, etc.
There is now a Question object, with simple properties for the moment. The Questionnaire holds an IList of these questions and the ASP.Net page(s) use the Questionnaire and its Questions list.
Intended use: on an edit page, I want to be able to move questions up and down, add new questions, edit questions, and delete questions. Rather than save to the DB each time (something that could be done via the Question object / hooking into the IList's CRUD operations / something else), I want to persist the Question object in state until the user is ready to save, then call the Save method on the Question instance.
Here's the quandary: during the manipulation of the Questionnaire's Questions, new Questions will be added to the IList, Questions will be updated in the IList and Questions will be removed from the IList. My question is: how do I reconcile my altered IList with what's in the database?
Three separate ILists could be used: one for deletes, one for adds, and one for updates. Thinking further, the newly-added entries would not have a database-driven ID value (the default being 0), so newly-added entries would be easy to spot. That just leaves the Delete IList, but I can't think of a decent way of flagging "to be deleted" on the class, and I am loathe to introduce that sort of "State" property to the business object, e.g. Added, Deleted, Updated...
In a nutshell: how do I reconcile a list of objects with their underlying datasource in a single save operation, bearing in mind some of the objects may have been deleted?
If this was being done via LinqToSQL, it would be a lot easier (dbcontext.submitchanges), but that's not an option.
Mike K.