Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

No More Session Variable Misspellings

, 29 Jun 2011
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Erich Ledesma
Software Developer SunHotels
Spain Spain
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.
 
I mainly work in web applications using C# and some Javascript. Some very few times do some Java.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by Erich Ledesma
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid