I'm trying to understand if there is any issue with doing a 'dynamic' static property. Say, I have an ASP.NET Webforms application that deals with Employees in a company. For the sake of this question, lets say this application is built for only one company.
Assume there is a class called Employee. It contains one static property.
public static string CompanyName = "My Company";
Now anywhere on the site if I use CompanyName, it is going to display the same company name for all users. A true static usage.
Now assume I define another property as (Lets say this is for ease of usage across the site so you can just do Employee.LoggedIn. )
public static Employee LoggedIn
{
get
{
return Session["LoggedInEmployee"] As Employee;
}
}
This will return different values for different employees based on what user is connected to the site. Does this mean there is a copy of this variable for every user? I know this works as expected but just wondering if it is safe to do this.
EDIT - i'm not looking for better ways to do this. I'm only trying to understand how the static property can hold different values. Someone commented saying the 'get' is a method, but still the property is represented as static.
public static string CompanyName = "My Company";
We don't normally use Singleton in ASP.Net except for IoC container, Logging or Caching.
public Employee static LoggedIn
{
return Session["LoggedInEmployee"] As Employee;
}
Above code won't even compile.
This will return different values for different employees based on what user is connected to the site. Does this mean there is a copy of this variable for every user? I know this works as expected but just wondering if it is safe to do this.
Based on your question, you want LoggedInEmployee available across your application.
If I understand your question correctly, LoggedInEmployee is the current logged-in employee's information. If so, it shouldn't be shared with other logged-in employee.
Since it is per logged-in employee information, I'll use the alternative approach by storing in HttpContext which is only available to current logged-in employee.
public class MyContext
{
private MyUser _myUser;
public static MyContext Current
{
get
{
if (HttpContext.Current.Items["MyContext"] == null)
{
MyContext context = new MyContext();
HttpContext.Current.Items.Add("MyContext", context);
return context;
}
return (MyContext) HttpContext.Current.Items["MyContext"];
}
}
public MyUser MyUser
{
get { return _myUser; }
set { _myUser = value; }
}
}
}
void Application_AuthenticateRequest(object sender, EventArgs e)
{
if (HttpContext.Current.User != null &&
HttpContext.Current.User.Identity.IsAuthenticated)
{
MyContext.Current.MyUser =
YOURCODE.GetUserByUsername(HttpContext.Current.User.Identity.Name);
}
}
var user = MyContext.Current.MyUser;
public static class EmployeeHelper {
public static Employee LoggedIn {
get { return HttpContext.Current.Session["LoggedInEmployee"] as Employee; }
}
}
Above approach is fine. As long as you do not make like this -
public static class EmployeeHelper {
// ===== Bad approach =====
private static HttpSessionState _session = HttpContext.Current.Session;
public static Employee LoggedIn {
get { return _session["LoggedInEmployee"] as Employee; }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With