Lazycoder

20Apr/041

Udi on strongly typed collections

Udi Dahan – The Software Simplist: Code breaks, automated code breaks automatically

Good point Udi,

My concern about the memory usage was not only the memory used by the static collection itself but also the fact that you end up creating the same collection in memory twice. Once when you create the DataSet/DataReader and then again when you create the strongly typed object for each row in your dataset. Since I’m working in a web based environment I end up creating everything on each page load unless I cache the collection. This works when the contents of the collection are shared by every user of my system, say if I cache the strongly-typed collection that is the data source for a drop-down control. If the contents of the collection are specific to a user, in my example a collection of patients that the user is allowed to see, then I have to cache each collection seperately.

I explain myself a little better in this comment.

  • http://TheSoftwareSimplist.com Udi Dahan – The Software Simplist

    A datareader does not take up a lot of memory – it’s size is the same
    regardless of the amount of rows returned, or the number of columns. Datareaders
    are like the firehoses used pre .Net.

    Datasets obviously take up memory proportionately to both the amount of rows and columns.
    This is why I avoid them when using custom collections.

    Now, as to the comment you point to, who says you have to cache all related objects as well ?
    Cache what has low volatility and is used frequently. In order to prevent caching related
    objects / collections of other objects ( lab results per patient ), have the property
    accessor go get the data and return it without storing it in your patient object.

    If your looking for more information on intelligent caching schemes take a look at some
    of the older items on my blog. Conversely, you could contact me by email.