C# - Making Multiple instances from a STATIC class

STATIC classes are basically meant to be like global references in an application - created once and used for the lifetime of the application. 

The primary advantage of this - apart from the obvious small footprint, is the fact that you don't need to instantiate a new instance of the class whenever you want to reference any of its members. 

One of the requirements for using a STATIC class is that all the publicly accessible members must also be qualified with the STATIC reserved word. 

Essentially, it means only one copy of the method is maintained throughout the life cycle of the application. In order to ensure some level of sanity, it is recommended that one does not pass parameters by reference to STATIC methods but rather, pass in your parameters by values, perform any processed needed and return any value - if needed, by value as well. 

Sometimes though, you want to leverage on the simplicity offered by the STATIC class usage in terms of no instance - new class type declaration - required - but at the same time afraid of information leakage. Your best recourse might be a combination of a STATIC class and session state combined. 

If you need to access a STATIC class but you want the STATIC class to be solely unique to a Session - you can make that possible using a combination of dynamic session variable reference as well as  Generics. 

To access the unique STATIC CLASS for a session as opposed to the application's  session state- create a static property that returns a session variable - if it exists or a default value specified in the Getter wrapper.

Usage will be something like this :

GlobalSession.UserName ---> GlobalSession is a STATIC class

UserName is a property of the STATIC class - GlobalSession.

 

Internal static string UserName{

get {return   assertSessionExists<string>(SessionEnum.cUserName);}

set {setSessionValue(SessionEnum.cUserName,value);}

SessionEnum is enum type used in defining the session variable name like so -

 

enum  SessionEnum {

cUserName

}


This is a  Generic method that is used to assert if a session variable exists already and so - return the value or if it does not - create it with the default value.


private static T assertSessionExists <T>(SessionEnum aSessionNameType,T aDeFaultValue = Default(T))

{

 string vIndexer = aSessionNameType.ToString();

HttpSessionState vSession =  HttpContext.Current.Session;

if ( !(vSession != null && vSession[vIndexer] != null vSession[vIndexer] is  T)

vSession[vIndexer] = aDeFaultValue;

}

return (T) vSession[vIndexer];

}

With the Getter using a Generic method in combination with some enum member to check if the session variable actually exists or not. In addition, the Getter calling the  Generic method will pass in default value if one is required.


For the Setter - call this method - setSessionValue - 

private static void setSessionValue(SessionEnum aSessionName, object value )

{

 HttpSessionState vSession =  HttpContext.Current.Session;

if (  vSession != null )

vSession[aSessionName.ToString()] = value;

}

The basic advantage of using this approach to manage Session variables is that you would not need to check or assert if a Session Variable exists before trying to reference it. 

In addition, your session variables will be managed in just one place - so - maintenance would not be a nightmare.

Comments

Popular posts from this blog

Decompiling Delphi - 3

Decompiling Delphi - 2

Artificial Intelligence, Oxymoron and Natural Intelligence