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

Advertisement

About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on July 31, 2012, in Programming & Development and tagged , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: