Skip to content

Instantly share code, notes, and snippets.

@renatoinline
Created October 1, 2014 16:26
Show Gist options
  • Select an option

  • Save renatoinline/fe659345a601a4390418 to your computer and use it in GitHub Desktop.

Select an option

Save renatoinline/fe659345a601a4390418 to your computer and use it in GitHub Desktop.
Classe utilitária para ajudar a trabalhar com sessões. O principal objetivo é simplificar o código e evitar null exceptions.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
/// <summary>
/// Classe utilitária para ajudar a trabalhar com sessões.
/// O principal objetivo é simplificar o código e evitar null exceptions.
/// </summary>
public static class SessionUtil
{
private static HttpSessionState mySession = HttpContext.Current.Session;
public static Boolean GetSessionAsBoolean(string key)
{
return Convert.ToBoolean(mySession[key]);
}
public static byte GetSessionAsByte(string key)
{
throw new NotImplementedException();
}
public static void GetSessionAsShort(string key)
{
throw new NotImplementedException();
}
/// <summary>
/// Retorna o valor armazenado na sessão como int.
/// </summary>
/// <param name="key">Nome da sessão</param>
/// <returns>Conteúdo da sessão como int.</returns>
public static int GetSessionAsInt(string key)
{
try
{
return Convert.ToInt32(mySession[key]);
}
catch(System.InvalidCastException)
{
return -1;
}
}
public static long GetSessionAsLong(string key)
{
try
{
return Convert.ToInt64(mySession[key]);
}
catch (System.InvalidCastException)
{
return -1;
}
}
public static float GetSessionAsFloat(string key)
{
throw new NotImplementedException();
}
public static double GetSessionAsDouble(string key)
{
throw new NotImplementedException();
}
public static decimal GetSessionAsDecimal(string key)
{
throw new NotImplementedException();
}
public static char GetSessionAsChar(string key)
{
throw new NotImplementedException();
}
public static string GetSessionAsString(string key)
{
return Convert.ToString(mySession[key]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment