Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does HttpSessionState not implement IDictionary?

HttpSessionState appears to be a typical key -> value collection, so why does it not implement the IDictionary-Interface?

Background: I am trying to output/save the Context of my ASP.NET Website when an error occurs and wanted to do this with a recursive function, that outputs a Collection and all containing Collections. Because HttpSessionState only implements ICollection and IEnumerable, I am losing the information about the keys if I want to do it in a generic manner (= working with interfaces).

like image 747
magnattic Avatar asked Jul 18 '11 16:07

magnattic


2 Answers

IDictionary implies that the target collection is capable of quick lookups by key. (As far as I am aware) HttpSessionState is just a list of items, not a dictionary style structure. As a search of that structure would take linear time there's no reason to treat it as a dictionary. If you need a lot of quick lookups then copy the keys and values into a true dictionary. If you don't need quick lookups, then you'll just need to specialize for that class.

There are more things to an interface than just a list of method prototypes. There are semantics that need to be preserved for an interface too. Quick lookups by key is one such non-explicit assumption for (most) consumers of any IDictionary.

like image 54
Billy ONeal Avatar answered Sep 17 '22 23:09

Billy ONeal


How about writing your own IDictionary-implementing wrapper that takes an HttpSessionState object in its constructor and behaves as you want? I'm assuming you want to do this so you can swap out other kinds of name-value (IDictionary-implementing) session implementations.

Of course, as Billy points out, this is a great way to dress a poor-performing psuedo-dictionary in dictionary clothes!

like image 38
n8wrl Avatar answered Sep 21 '22 23:09

n8wrl