Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

No More Session Variable Misspellings

0.00/5 (No votes)
29 Jun 2011 1  
I came with another one. I happen to love Lambda expressions. The idea is to extend this base class:public class SafeSessionBase{ HttpSessionState _session; public SafeSessionBase(HttpSessionState session) { _session = session; } protected TResult...
I came with another one. I happen to love Lambda expressions. The idea is to extend this base class:
public class SafeSessionBase<TSafeSessionImpl>
{
  HttpSessionState _session;
  public SafeSessionBase(HttpSessionState session)
  {
    _session = session;
  }
  protected TResult Get<TResult>(
    Expression<Func<TSafeSessionImpl, TResult>> property)
  {
    var propertyName = GetMemberName(property);
    return (TResult)(_session[propertyName] ?? default(TResult));
  }
  protected void Set<TValue>(
    Expression<Func<TSafeSessionImpl, TValue>> property, TValue value)
  {
    var propertyName = GetMemberName(property);
    _session[propertyName] = value;
  }
  private static string GetMemberName(Expression expression)
  {
    // This method extracts a member name from its
    // expression. From: x => x.F returns "F"
    // Implementaion show at the end
  }
}

Your session then would look like this:
class MySafeSession : SafeSessionBase<MySafeSession>
{
  ...
  public string StringProperty1
  {
    get { return Get(s => s.StringProperty1); }
    set { Set(s => s.StringProperty1, value); }
  }
  public int IntProperty1
  {
    get { return Get(s => s.IntProperty1); }
    set { Set(s => s.IntProperty1); }
  }
  ...
}

Any property, of any type would work with this technique. Cast is included. This way supports refactoring. Unfortunately the framework session doesn't implement IDictionary (nor any other useful interface) thus, this implementation would only work for sessions. Remember, there is no parameterless contructor.

To use it:
private void Page_Load(object sender, EventArgs e)
{
  ...
  _safeSession = new MySafeSession(Session);

  if (_safeSession.HelloSafeSession == null)
    _safeSession.HelloSafeSession = "Have a nice day. :)!!"
}


The almost forgotten method:
private static string GetMemberName(Expression expression)
{
  LambdaExpression lambda = expression as LambdaExpression;
  if (lambda == null)
    throw new ArgumentNullException("expression");
  MemberExpression memberExpr = null;
  if (lambda.Body.NodeType == ExpressionType.Convert)
  {
    memberExpr = ((UnaryExpression)lambda.Body).Operand as MemberExpression;
  }
  else if (lambda.Body.NodeType == ExpressionType.MemberAccess)
  {
    memberExpr = lambda.Body as MemberExpression;
  }
  if (memberExpr == null)
    throw new ArgumentException("expression");
  return memberExpr.Member.Name;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here