KnowDotNet NetRefactor

Net Sessions - Another New Product From KnowDotNet

Automatically Manage Your ASP.NET Session Variables

Net Sessions is an Add-in for Visual Studio .NET that provides a simple and complete methodology for creating, using, and maintaining Session Variables in ASP.NET applications. Net Sessions provides for using one of four supported mechanisms for accessing a Session Variable.  Net Sessions is available for purchase or Free 30 Day Trial.  Click here to download.

Net Sessions supports ASP.NET projects that use either, or both, VB.NET and C#.NET in exactly the same way. All menu options of Net Sessions are invoked from the context menu of the active code window. Net Sessions supports selection of and pasting of Session Variables from a docked Tool Window. Non String Variable options can be coded in-line by the use of Intellisense without the use of the Tool Window. Although the Tool Window was designed for use with String Session Variables, the Tool Window Paste feature will paste in the format selected as the primary mechanism for Session Variable maintenance. See the Tool Window Topic for details of pasting variables.

Net Sessions supports and suggests four different mechanisms for using and accessing Session Variables. At least one of these mechanisms (Embedded Strings), although probably not the best and easiest to use, is well known and obvious. We say that it is not necessarialy the easiest to use because of the potential for creating un-defined references that will not be caught by the VB.NET or C# compilers. Another ot these mechanism has been noted by well known ASP.NET developers. It may well be that all four mechanisms are in use by developers. In any case, Net Sessions provides the developer a choice of the access mechanism that the developer likes the best.

The four mechanisms supported by Net Sessions are listed below. Click the associated link to see the details of how each mechanism works.


Net Sessions Tool Window


Net Sessions has a dockable Tool Window that works with all four types of Session Variables supported by Net Sessions. You have Intellisense available with all methods except String Session Variables. Therefore, the Tool Window is probably most useful with the String method of using Session Variables.

Dockable Tool Window


You can load the Dockable Tool Window by selecting the Show Variables Window from the Net Sessions context menu. Figure 1 shows the Tool Window after it has been docked. If you load the Tool Window, Net Sessions will remember it was loaded and will load it the next time your project is opened. It will then be automatically docked.


Figure 1 - Docked Tool Window

Net Sessions has a dockable Tool Window that works with all four types of Session Variables supported by Net Sessions. You have Intellisense available with all methods except String Session Variables. Therefore, the Tool Window is probably most useful with the String method of using Session Variables.

Tool Window

To paste a selection variable, at the point of the cursor, simply double-click the desired variable in the Tool Window. If you want it Cast (CTyped in VB.NET), select the variable by clicking on it and then right-click the selected variable. Finally, select Cast & Paste menu option to paste the variable casted. Figure 1 shows the selection menu and it also shows the results of a paste in the code window. Since Net Sessions cannot always be successful in determining the type of cast to make, the Cast & Paste option will always paste the variable cast as Object. You can also paste a selected variable by clicking the Paste button at the top of the Tool Window, if a variable is selected.

Refreshing Variables from the Tool Window


You can automatically refresh the collection of Session Variables in the Tool Window by clicking the Refresh button (second button) at the top of the Tool Window.

Loading and Changing Solutions


If NET Sessions is set to be loaded when Visual Studio .NET is loaded, the Tool Window.  will be loaded, if it was laoded the last time Visual Studio was opened. However, the Tool Window combo box will be cleared because the Solution is not loaded at the time the Tool Window is loaded. Once the new solution is opened, simply click the Refresh Button on the top of the Tool Window to refresh the variables from the newly loaded soluton.
When changing solutions while NET Sessions is connected, and the Tool Window is loaded, its combo box will contain the names of the variables from the previous project.. Once the new solution is loaded, simply click the Refresh Button to load the variables from the current solution.

Refresh Session Variables Dialog



Maintenance of Session Variables is accomplished through the use of the Refresh Session Variables Dialog which can be invoked from the Net Sessions Context Menu.

Figure 1 - Refresh Variables Dialog

Maintenance Dialog


Using the Refresh Variable Dialog


The Refresh Variable Dialog is used to select the method for maintaing Session Variables in your ASP.NET application. Variables can be maintained as Properties, Enum Values, Public Const (VB.NET or Static Const in C#, and Quoted Strings. You can choose any one of the four. Your selection will be saved in the in the project file so that Net Sessions always remembers the format used by each ASP.NET project, even if you use different formats in different projects.

Once the desired format is selected, if the selection is either Property or Enum, you must enter the name of your Enum or Property Class, in which the names will be created. It is best to enter a short (1-3) character unique name, such as "SV" (for Session Variables), to reduce the amount of keystrokes that you are required to enter before getting Intellisense to show you a list of your Session Variables.

If you are going to use Enum values as your selected format, then you must create a Public Enum (public enum in C#) as shown below. Additionally, you must enter the name of the Enum. Before clicking the Create Session Variables button, place te cursor inside the empty Enum.

   Public Enum SV ' the name is your choice but should be short
   End Enum  

If you are using Property Methods for maintaining your Session Variables, you must create a Public Class (Container for encapsulating the Property Methods). Additionally, you must enter the name of the Class. Before clicking the Create
Session Variables button, place the cursor in the empty class. A typical Class for this is shown below:

   Public Class SV ' The name is your choice but should be short
   End Class

Figure 2 shows the Refresh Variable Dialog with the Property Option selected.

Figure 2 - Property Option Session Varaibles

Maintenance Dialog


Once you have selected the type of method for maintaining your Session Variables, click the Create Session Variables button.

After the scan of Session Variables is completed, all of the variables will be created in the format as specified by your selection. To see how the variables are saved, see the respective topics for the type selection that you choose.

Not only can Net Sessions create the Property, Const, or Enum variables for you automatically from whatever type of variables you are currently creating, it will also automatically convert all references to Session Variables in your project to the newly created format. Simply click the Convert to Selected Format button, which will be enabled after the Create function is executed.

Undefined and Unreferenced Session Variables


Net Sessions will not only show all variables that are defined and referenced, it will also detect errors in the definition and referencing of Session Variables. Once the Refresh Session Variables Dialog loads, all modules in the project have been scanned and all Session Variables have been analyzed for errors. Clicking the Variable Warnings tab of the dialog will show the warnings as displayed in Figure 3.

Figure 3 - Variable Warnings.

Maintenance Dialog


In Figure 3, you will see rows colored in Red. This means that those variables are referenced, but not never defined. If you reference such a variable, it will not produce a compiler error, but it will not retrieve a value at run-time. In other words, somewhere in your program is a line of code like the following:

   test = Session["Undefined"];

However, there is no line of code which Sets the "Undefined" Session Variable. In Figure 3, you will also note there is a line that is colored Yellow. That means that the Session Variable, "DefinedNotUsed" has been set somewhere in your project, but that Session Variable is never referenced. Although, by itself, this does not mean that you have an error, unless you defined a Session Variable and meant to use it later, and never did. It could also mean that you simply misspelled either the reference or the definition of the Session Variable. In either of these cases, you have an error that will only show up at run-time, assuming that you find it in your debugging process. If you don't, you could put an application into production with a bug in it.

All Session Variable References


The third tab on the dialog shows all instances of references to all Session variables.  Double-clicking on any variable in the list view will go to the selected variable reference.  If you are having trouble with the contents of a Session variable, this will help you locate and examine the context of the variable.  Figure 4 shows this tab.

Figure 4 - All Session Variable References

All Variables




Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing