Skip to main content

Extension methods for the HttpSessionState class.

using System.Web.SessionState;

namespace Extensions {

    public static class HttpSessionStateExtensions {
        /// <summary>
        /// Returns a typed value from the ASP.NET session state.
        /// </summary>
        /// <typeparam name="T">The generic type to be returned.</typeparam>
        /// <param name="self">The session state.</param>
        /// <param name="key">The session state key.</param>
        /// <returns>The session state value.</returns>
        /// <example>
        /// <code>
        /// public List&lt;string&gt; StringValues
        /// {
        ///     get { return this.Session.Ensure&lt;List&lt;string&gt;&gt;("StringValues"); }
        ///     set { this.Session.Set("StringValues", value); }
        /// }
        /// </code>
        /// </example>
        public static T Get<T>(this HttpSessionState self, string key)
        {
            return self.Get(key, default(T));
        }

        /// <summary>
        /// Returns a typed value from the ASP.NET session state or the
        /// provided default value.
        /// </summary>
        /// <typeparam name="T">The generic type to be returned.</typeparam>
        /// <param name="self">The session state.</param>
        /// <param name="key">The session state key.</param>
        /// <param name="defaultValue">The default value to be returned.</param>
        /// <returns>The session state value.</returns>
        /// <example>
        /// <code>
        /// public List&lt;string&gt; StringValues
        /// {
        ///     get { return this.Session.Ensure&lt;List&lt;string&gt;&gt;("StringValues"); }
        ///     set { this.Session.Set("StringValues", value); }
        /// }
        /// </code>
        /// </example>
        public static T Get<T>(this HttpSessionState self, string key, T defaultValue)
        {
            var value = self[key];
            return (T) (value ?? defaultValue);
        }

        /// <summary>
        /// Ensures a specific key to be either already in the ASP.NET session
        /// state or to be newly created.
        /// </summary>
        /// <typeparam name="T">The generic type to be returned</typeparam>
        /// <param name="self">The session state.</param>
        /// <param name="key">The session state key.</param>
        /// <returns>The session state value.</returns>
        /// <example>
        /// <code>
        /// public List&lt;string&gt; StringValues
        /// {
        ///     get { return this.Session.Ensure&lt;List&lt;string&gt;&gt;("StringValues"); }
        ///     set { this.Session.Set("StringValues", value); }
        /// }
        /// </code>
        /// </example>
        public static T Ensure<T>(this HttpSessionState self, string key) where T : class, new()
        {
            var value = self.Get<T>(key);
            if (value != null)
            {
                return value;
            }

            value = new T();
            self.Set(key, value);

            return value;
        }

        /// <summary>
        /// Ensures a specific key to be either already in the ASP.NET session
        /// state or to be newly created.
        /// </summary>
        /// <typeparam name="T">The generic type to be returned</typeparam>
        /// <param name="self">The session state.</param>
        /// <param name="key">The session state key.</param>
        /// <param name="valueFactory">A value function to produce the instance when not found in session. This parameter will not be invoked in null.</param>
        /// <returns>The session state value.</returns>
        /// <example>
        /// <code>
        /// public List&lt;string&gt; StringValues
        /// {
        ///     get { return this.Session.Ensure&lt;List&lt;string&gt;&gt;("StringValues"); }
        ///     set { this.Session.Set("StringValues", value); }
        /// }
        /// </code>
        /// </example>
        public static T Ensure<T>(this HttpSessionStateBase self, string key, Func<T> valueFactory) where T : class, new()
        {
            var value = self.Get<T>(key);
            if (value != null)
            {
                return value;
            }

            value = valueFactory != null
                ? valueFactory()
                : new T();

            self.Set(key, value);

            return value;
        }

        /// <summary>
        /// Sets the specified value into the ASP.NET session state.
        /// </summary>
        /// <param name="self">The session state.</param>
        /// <param name="key">The session state key.</param>
        /// <param name="value">The new session state value.</param>
        /// <example>
        /// <code>
        /// public List&lt;string&gt; StringValues
        /// {
        ///     get { return this.Session.Ensure&lt;List&lt;string&gt;&gt;("StringValues"); }
        ///     set { this.Session.Set("StringValues", value); }
        /// }
        /// </code>
        /// </example>
        public static void Set(this HttpSessionState self, string key, object value)
        {
            self[key] = value;
        }
    }
}