Static class for Application Session Variables, bad idea?

0
0

I made a static class for my Application.Session look ups like this.

public static class AppSession
{

public static IConfig Config { get { return (IConfig)Application.Session.Config; } set { Application.Session.Config = values; } }

public static IUser User { get { return (IUser)Application.Session.User;} set {Application.Session.User = value; } }

}

then in my code event I would

private async void myForm_DisplayUser(object sender, EventArgs e)
{
var user = AppSession.User;
lblUserName.Text = User.Username;
Application.Update(this);
}

Would putting your Application Session variables in a static class a bad idea?  I thought I saw in an example this was how you could change static variables to session variables.

Thanks,

Devin

  • You must to post comments
0
0

Hi Alaa,

There is no requirement, I can just use

Application.Session.MyVar = “value”;

But I saw this example in your migration documentation like this:

// Session is a dynamic object
Application.Session.MyVar1 = 1;
Application.Session.MyVar2 = “Test”;
// Statics moved to session
MyClass.Static1 = “Hello”;
// Becomes
Statics.MyClass.Static1 = “Hello”;
// It’s an easy search/replace.
Statics {
MyClass { get {
return Application.Session.MyClass; }}}

Wrapping your Session variables in a static class is just a little easier as the Variables are type casted and you don’t have to remember what they were you cant just type “Statics.” in VS and all your Session variables show up.

But since it’s a static class I was wondering about thread safety, maybe bad idea.

Thanks,

Devin

 

 

 

 

  • You must to post comments
0
0

Hi Devin,

I’m not sure I understand the requirement, why would you “need” to wrap everything in a Static class?

The Application.Session is a dynamic class, you can have anything assigned to it.

“I thought I saw in an example this was how you could change static variables to session variables.” We strongly recommend using Session variables instead of static classes, not the other way around as it makes more sense in a web application.

HTH,
Alaa

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.