Lets say we have these classes:
class Teacher {
public int Id { get; set; }
public string FullName { get; set; }
public List<Course> Courses { get; set; }
}
class Course {
public int Id { get; set; }
public string CourseName { get; set; }
}
Now lets say I have selected one Teacher instance from the list of teachers and I’ve been redirected to a page where I can edit what courses this teacher can teach. So there’s a list of all the courses she can teach at the momemt (taken from the database) with a “remove” option and a dropdownlist of all available courses that a user can select; when a user selects a course from the dropdownlist, it gets added to a “she can teach” list. At the end is a “save” button.
But, when a users removes a course from the list or adds a course, I don’t want to save it to the database until pressed “save” button. My question is: where to hold Teacher object and its child Course objects while being edited and before saved to database?
So far I’ve been using Session to hold the edited object; adding a new course just adds a child object to its collection, and removing a courses removes a child object from Session. Is that the best way to go on or is there something better (or at least, cleaner)?
Session should be fine, as long as you’re ok with keeping all this in memory, and potentially losing it if the session is lost prematurely.
But if you’re looking for persistence, I’d probably suggest a “pending” database table, where you can keep the pending transactions (adds and deletes) until they’re ready to be saved. When you’re on this screen, you combine the real data with the pending data, and when you save, you apply the pending changes to the real data and kill the pending changes.
If they cancel, all you have to do is delete the pending records.