Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static class vs Session object 1

Status
Not open for further replies.

kristof

Programmer
Jun 2, 2000
234
0
0
BE
Hi,

Am I correct saying that with ASP.Net, a static class, is basically a single server object, that is active across sessions?

Meaning that if I set a value of a static class, all sessions will read the same value. But that means that if one user's session sets this value again with something new, EVERYONE will read this new value, not the old one.

What I'm getting at is that I'd like to use something along the lines of:

Code:
string text = StaticClassName.SomeValue;

I just think it's way cleaner than:
Code:
string text = Session["SomeValue"].ToString();

Particularly because I don't need the cast with a static method. But, I think the nature of a static class (as I described above) makes this impossible, as it will return 'server' values, instead of 'client' (read: session) variables.

Any ideas/suggestions about this?
 
Am I correct saying that with ASP.Net, a static class, is basically a single server object, that is active across sessions?
No.

If you create a class, that class has to be instantiated. So each user will have a version of that class. In essance, it will act the same as a session variable.

If you are looking for a way to impliment "Global" variables, there really is no way in a web app. Because sessions, and instantiations of classes, will be at the user level.

Global variables are possible on a Windwos application, not a web application.
 
here's a snippet i picked up from a training course, which i think is what your looking for.
Code:
protected void Application_Start(object sender, EventArgs e)
{
   ApplicationStartupCommand.Run();
}
Code:
public ApplicationStartupCommand
{
   private IDictionary<Type, object>() implementations;
   public ApplicationStartupCommand(IDictionary<Type, object>() implementations)
   {
      this.implementations = implementations;
   }

   public void Execute()
   {
      DependencyResolver.InitializeWith(new CustomDependencyResolver(implementations));
      implementations[typeof(IFoo)] = new Foo();
      //repeat for each object.
   }

   public static void Run()
   {
      new ApplicationStartupCommand(new Dictionary<Type, object>() implementations).Excute();
   }
}

//just to give foo meaning
public interface IFoo
{
   string GetBar();
}

public class Foo : IFoo
{
   public Foo()
   {
   }

   string GetBar()
   {
      return "BAR";
   }
}
Code:
public DependencyResolver
{
   private static IDependencyResolver realResolver;

   public static Interface GetImplementationOf<Interface>()
   {
      return realResolver.GetImplementationOf<Interface>();
   }

   public static InitializeWith(IDependencyResolver resolver)
   {
      realResolver = resolver;
   }
}
Code:
public interface IDependencyResolver
{
   Interface GetImplementatioOf<Interface>();
}
Code:
public class CustomDependencyResolver : IDependencyResolver
{
   private readonly IDictionary<Type, object>() dependencies;

   public CustomDependencyResolver(IDictionary<Type, object>() dependencies)
   {
      this.dependencies = dependencies;
   }

   public Interface GetImplementatioOf<Interface>()
   {
      return (Interface)dependencies[typeof(Interface)];
   }
}
now every user accesses the exactly one instance of the given type.
Code:
string bar = DependencyResovler.GetImplementationOf<IFoo>().GetBar();
now if I remember correctly this works because...
1. it's loaded once and only once. on the starup of the application (website)
2. because of the static fields/methods the objects are not destroyed until (1) the application scope ends. all users are off the system. which i believe by default is 20 minutes after the last request. (2) DependencyResolver.InitializeWith(null); is called. which nullifies the dependency resolver.

this can be used as the "poor mans" IoC container. for more robust IoC containers check out structure map, spring.net and Winsdor

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
If global variables are what your looking for, try using Application variables, they work the same as session variables but are global across all sessions.

The only other difference is that you have to lock the variable before you can update it.

Smeat
 
Appliation/Cache will definately work. The difference is the objects above allow for unit testing and TDD.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top