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. I call it a compound collection, because it contains two additional internal Lists. One contains all the added items, one contains all the removed ones. So, I can use it as follows:

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 with Reflector, and creating my own internal array structures, but it just got too complicated for my liking. Using two additional Lists did the trick.

You can download the source code here. If you guys have any improvements, please let me know.

4 Comments

  • Sounds good Robert. By the way, I just wanted to point out that the WilsonORMapper does NOT generate any code, so any code you're reading and not liking is either your own, or generated with my quickstart helper or templates, neither of which is required -- so you could be reading your own code. :)

  • getting a &quot;HTTP Error 403.1 - Forbidden: Execute access is denied.

    Internet Information Services (IIS)&quot; on the download. Can you make it a txt file?

  • Paul - Right. In the case of WilsonORMapper, I just wasn't fond of the architecture. Of course, that was a few versions back, so I could be wrong now. It's just a preference thing ;).

  • Warning: Pet peeve venting ahead



    My only suggestion would be to change the name from SmartList -- I'm really tired of reading code that uses names like this. Smart is meaningless and tells me nothing about what this object does. Its so easy to come up with a better name and its going to make everyone's life better later. How about calling it ChangeTrackingList and helping reduce my blood pressure :)

Comments have been disabled for this content.