I am working on a .net project that relies on pulling some information from a database upon login and persisting the information while the person is logged in.
The site is complex, but I feel that overuse of the Session variable may be occurring. I will right now say that I'm using session to store about 5 int values and 3 or 4 string values. I don't have any complex objects stored in it. Every page of my site utilizes these values multiple times, and I feel that posting them each time would be ridiculous. I don't think that viewstate is any better than session at this point either, but i'm open to suggestions.
Example current usage of Session variable
this.Session["vendorCategoryId"]
this.Session["ftp_directory_name"]
this.Session["VendorCodeOptionRefId"]
I thought about implementing a struct in a class and having a set method there that I use from Global.asax.cs in the Session_Start()
event might be one solution...
(EDIT: This solution is designed to avoid having to hammer the db every time any piece of code wants to access the variables).
In VendorSessionData.cs class
public struct VendorData
{
public int VendorCategoryId;
public int NKIAccountingCode;
public int OptionCodeRefId;
public string FtpDirName;
}
/// <summary>
/// The set vendor session data.
/// </summary>
public class VendorSessionData
{
#region Public Methods
public static VendorData GetVendorData(Guid vendorGuid)
{
VendorData retVal = new VendorData();
using (NKIDBDataContext db = new NKIDBDataContext())
{
vendorInfo vendorRefs = (from vendorInfo in db.vendorInfos where vendorInfo.guid == vendorGuid.ToString() select vendorInfo).SingleOrDefault();
if (vendorRefs != null)
{
retVal.VendorCategoryId = vendorRefs.category_id;
retVal.NKIAccountingCode = vendorRefs.nki_vendor_id;
retVal.OptionCodeRefId = vendorRefs.option_ref_id;
retVal.FtpDirName = vendorRefs.ftp_directory_name;
}
}
return retVal;
} ......
And in global.asax.cs
public class Global : HttpApplication
{
public static VendorData CurrentVendorData;
public void Session_Start(object sender, EventArgs e)
{
/////////////////FYI tmpVendorGuid is set in code above this point
Guid tmpVendorGuid;
if (Guid.TryParse(vendorGuid, out tmpVendorGuid))
{
CurrentVendorData = VendorSessionData.GetVendorData(tmpVendorGuid);
}
Would it be better to try to attack this using hidden fields on the master page? Every search I've done says "don't use session, don't use globals.." basically any idea I have, there is some page stating its the worst idea ever :)
Any suggestions would be appreciated.