Not sure if this is the appropriate forum, so please feel free to move.
I have a problem with a web based application. In the global.ascx application_beginRequest event I have an object called UserSettingsManager which grabs the user settings on each request. The problem is that the application_beginRequest event does not have access to session data. Consequently, I store the userId and companyId in the session object when the user logs in. So when a aspx page is called I want to update the UserSettingsManager object to include and filter the data based on the userId and companyId contained within the session object. So I created a method called SetSessionData (SetSessionData(int companyId, int userId) on the userSettingsManager object which should update the userId and companyId properties for the UserSettingsManager. This works, however, any object I call uses the UserSettingsManager data prior to the the SetSessionData rather than using the data after calling the SetSessionData method. I'm not sure what is the appropriate way to handle this. Was looking for any insight as I'm sure this must be a common problem. Here is example code:
public class MyClass
{
public static void Main()
{
Test t = new Test(2,3);
t.OutputBefore();
t.SetSessionData(3, 4);
t.OutputAfter();
Console.ReadLine();
}
}
public class Test
{
public int appId = -1;
public int pageId = -1;
private int userId = -1;
private int companyId = -1;
public int UserId
{
get{return this.userId;}
set{this.userId = value;}
}
public int CompanyId
{
get{return this.companyId;}
set{this.companyId = value;}
}
public Test(int appId, int pageId)
{
this.appId = appId;
this.pageId = pageId;
AppManager app = new AppManager(this.userId);
app.Output();
}
public void OutputBefore()
{
Console.WriteLine("");
Console.WriteLine("Output Before SetData");
Console.WriteLine("value of appId: " + this.appId);
Console.WriteLine("value of pageId: " + this.pageId);
Console.WriteLine("value of companyId: " + this.companyId);
Console.WriteLine("value of userId: " + this.userId);
}
public void OutputAfter()
{
Console.WriteLine("");
Console.WriteLine("Output After SetData");
Console.WriteLine("value of appId: " + this.appId);
Console.WriteLine("value of pageId: " + this.pageId);
Console.WriteLine("value of companyId: " + this.companyId);
Console.WriteLine("value of userId: " + this.userId);
}
public void SetSessionData(int companyId, int userId)
{
this.companyId = companyId;
this.userId = userId;
}
}
// This is where the problem occurs, AppManager uses the initial data rather than the data after SetData is called
public class AppManager
{
private int userId;
public int UserId
{
get{return this.userId;}
set{this.userId = value;}
}
public AppManager(int userId)
{
this.userId = userId;
}
public void Output()
{
Console.WriteLine("AppManager userId: " + this.userId + " AppManager userId should be 4 not -1");
}
}
If anyone can offer a better solution or suggestion, it would be greatly appreciated.