Daily Archives: July 31, 2012

c# .net list anonymous read only store dynamic types in session

If you’re new to .Net 4.0 “dynamic” type, then you may or may not be surprised to find this is reserved for unique object types with read-only properties, as compared to its previous counterpart the “object” type.

In general, this is not an issue that many will encounter, at least initially, since many implementations will probably be focused on one-way databinding to controls on pages which in turn update the database directly instead of the in-memory object storing the values from the initial data results.

However, keep in mind that as cool as dynamic is, this immutable nature makes for a mess in circumstances that require two-way binding, such as with a session variable (example below). If you are keen on using it in these scenarios, never fear, I have worked up your solution. ;)

A typical practice sometimes utilized for Session variables is to use a common class for getting/setting session values, such as (you can skip ahead if you’re familiar with this aspect):

The immutable Session scenario

public static class SessionVars {

public static object MySessionValue{get {return getval("MySessionValue");} set {setval("MySessionValue",value);}}

public static List<object> MyListOfSomething { get { return (List<object>)getval("MyListOfSomething"); } set { setval("MyListOfSomething",value); } }

        private static object getval(string key)
        {
            try
            {
                return HttpContext.Current.Session[key];
            }
            catch (Exception ex)
            {
                //YourErrorClass.HandleError(ex);
                //return "Error retrieving value";
return null;
            }
        }

        private static void setval(string key, object value)
        {
            try
            {
                HttpContext.Current.Session[key] = value;
            }
            catch (Exception ex) {
                //YourErrorClass.HandleError(ex);
                //return "Error setting value";
            }
        }
}

The above could then be utilized such as:

SessionVars.MyListOfSomething =somevalue;
//or
SessionVars.MyListOfSomething [0]=somevalue;
//or
yourdatabindingcontrol.DataSource = SessionVars.MapNames;
yourdatabindingcontrol.DataBind(); //etc..

This of course makes Session usage much easier, however, our third line of code in the above example would not be possible if it were of type dynamic, which otherwise generally replaces object.

If MyListOfSomething were instead List of dynamic, you would have to use the following:

Solution

//for list
   var removei = SessionVars.MyListOfSomething [index];
                SessionVars.MyListOfSomething .Remove(removei);
                string newvalue = "somenewvalue";
                var addi = new { id = removei.id, name = removei.name, value= newvalue };
                SessionVars.MyListOfSomething .Add(addi);

//for single value
   var removei = SessionVars.MySessionValue;
                string newvalue = "somenewvalue";
                var addi = new { id = removei.id, name = removei.name, value= newvalue };
                SessionVars.MySessionValue=addi;

References
MSDN (Immutable anonymous types), c# .net list anonymous read only store dynamic types in session
Store List to Session (StackOverflow), http://stackoverflow.com/questions/1259934/store-list-to-session

Follow

Get every new post delivered to your Inbox.