Well, I haven't fully implemented it yet. Right now the class works like this:
Create a new manager object passing a database connection and control in the constructor.
The user then passes a few arguments (or not) to one of the 4 methods described above and the manager retrieves the data from the database, formats it in a way that is compatable with the passed control and populates the control with the data (the control may be a ComboBox, ListBox, ListView or TreeView).
This is what the manager does on initialization. On postback, (this is a web application) the manager is instantiated with the database connection and control the same as before, but depending upon what caused the postback, the manager will either add a new item to the collection of things used to populate the control, delete an item from the collection or modify an existing item in the collection. The data is held in a transient state until the SaveAll method of the manager is called. This will then commit the objects in the collection to the database. Whether the items are saved is based upon the user's actions and the postback values.
Because this is a web application, all data is transient and exists only for the duration of the execution of the server-side code, and long enough to populate the control with the data that is displayed to the user.
I probably don't need to be as complex as I want to make these objects, but I will certainly be using this class in another application in the future, thus the ability to edit objects in the collection is of importance. Also, I will very likely be adding additional properties and methods to the objects in the collection, so creating a derived class, inherited from List<T> is also probably the way I want to go with this, not to mention that I will likely be using this class in other data object managers.
So, lets just start at the beginning.
I have an object that is created from data in the database. We will call this object "Shift" (remember this is a medical staffing and patient manager).
For each building, there may be many shifts that need to be created and displayed to the user in a ListBox, ComboBox, or perhaps a DataGrid. To keep all of the shifts in one location, I will create a "ShiftCollection" that holds all of the "Shift" objects. This collection is a simple List<Shift> and thus I am able to add/edit/delete any of the "Shift" objects in "ShiftCollection".
In my "ShiftManager" class, I will have a private variable of "ShiftCollection" (lets call it _shiftCollection). The private variable _shiftCollection will be exposed through an Items property. Now, I can instantiate my ShiftManager class with the control and database connection, then fill the _shiftCollection object with one of the four methods described in my earlier post. This will load all of the Shift objects from the database, placing each into the _shiftCollection object.
Once I have ShiftManager instantiated and filled, I can then edit the ShiftCollection through the Items property of the ShiftManager, adding, deleting or editing the contents as needed.
The only part that I am unclear with now, is how can I call the private method in ShiftManager to clear all items from the control and populate it with the new collection once the collection or an item in the collection has changed? Is there an event that is fired when the collection is modified or do I have to create my own events (for example OnShiftEdit event, then bubble up to the collection which fires an OnShiftCollectionModified event that the manager class can catch and call the correct function to update the control)?
Oh, and one final thing, if I make Items the default property, can I then access the items in the List<Shift> by like this:
shift Items[int index]
{
get
{
return _shiftCollection[index];
}
set
{
_shiftCollection[index] = value;
}
}
shift thisShift = ShiftManager.Item[n];