0

Let's say I wish to store an instance of the following in Application State, to be accessed very often.

public class Example {
  public string A;
  public string B;
  public bool C;
  public int D;
  // ...
}

I can't decide whether to store the whole class together as Application["Example"], or to store its properties individually as Application["ExampleA"] etc.

My thinking is that ((Example)Application["Example"]).A might have to copy the whole class into memory just to access one property - is that right? Or am I mistaken?

James
  • 6,911
  • 9
  • 40
  • 80

3 Answers3

1

I would use a static global variable, slightly better performance, type safe and will make your code easier to read. For more info see...

ASP.NET Application state vs a Static object

Community
  • 1
  • 1
Woody
  • 763
  • 4
  • 14
  • Application state doesn't need to be serialized/deserialized, it is simply an in memory dictionary. You are right that a static will be quicker to access tho as it doesn't need to look up the item in the collection. Are there any potential threading issues that using application would solve for free which you may have to code for with a static? – DoctorMick Jul 28 '11 at 10:36
  • Yeh, your right no serialization/deserialization required. The performance benefits I suspect are slight as no dictionary read required or cast. As far as threading goes I think they are the same. – Woody Jul 28 '11 at 11:26
0

you are right but....

you don't need to copy the whole object if you just need the value of one of its property. Conceptually if we are talking about a value object(you don't need an identity or a particular object) you can store just the property. If you need to know what is the value of the property for one particular object(imaging a user's password) you should store the whole object.

Massimiliano Peluso
  • 24,915
  • 6
  • 54
  • 67
0

The application state is stored in memory anyway so I can't see a significant overhead with retrieving the class. I'm fairly sure, although could be wrong, that the classes wouldn't be serialised/deserialised with each request.

DoctorMick
  • 6,472
  • 24
  • 24