Tracking Collection Changes: Introducing SmartList<T>
Yesterday, I posted about needing to be able to track changes to generic collections. I've been diving really hard-core into CodeSmith, and right now I'm working on a set of templates that will generate an entire provider model-based website architecture from a set of database tables.
I know some people are going to suggest programs like LLBLGenPro or WilsonOrMapper... but I've never liked the architecture of either system. I wanted to go back to the attribute-based serialization framework from XHEO, but they're not releasing it anymore, so it's unsupported. So instead of trying to learn a new model for this down-and-dirty prototype, I decided to roll my own system.
But I digress. Anyways, in order to simplify the process of saving information to the database, I needed to be able to know which parent and children items had been added or removed. That way, I could call ParentManager.Save(Parent), and if any child objects have been added, removed, or changed, they'd be saved automagically as well.
Enter SmartList
Dim list As New SmartList(Of String)(False)
list.Add("test1")
list.Add("test2")
list.Add("test3")
list.TrackChanges = True
list.Remove("test2")
Response.Write(list.ToString())
Adding (False) to the constructor tells the SmartList not to track any changes to the generic collection. This is so you can load it up with data, and then give it a simple "state" saying it's loaded.
Below is the output from the above code:
Removed: test2 Items: test1 test3
It's probably not the best implementation in the world, but it works well and is relatively performant. I tried digging into List
You can download the source code here. If you guys have any improvements, please let me know.