Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

No More Session Variable Misspellings

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


Written By
Architect 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 --