tags:

views:

475

answers:

4

Currently i have a NotInheritable class in App_Code that hold some variables that need to be access thur-out the application but i don't think it's a good way to manage global variables.

+2  A: 

Usually, this kind of thing calls for a Singleton. However, I'd recommend never coding a singleton yourself, and using a Dependency Injection/IoC framework to handle the life-cycle of services.

The other thing you have to remember with ASP.NET is that the ASP.NET process automatically recycles itself every now-and-then, so you'll need to persist changes to permenant storage (such as file-system or database)

David Kemp
ASP.NET doesn't randomly recycle itself! And for many (many!) apps, DI/IOC is off the chart crazy.
rp
DI/IOC is a solved problem, so why not use it?
David Kemp
A: 

You should use the Application Settings. Just go to the Application property window, choose the settings tab and add your variables. Than on the code just use the strong typed class. For instance, if I've created a Global variable named PortalName I just use: Settings.Default.PortalName

Bruno Shine
I don't see the setting tab. Are you talking about setting tab with web application project? I'm working on asp.net website.
Jack
I think he's talking about the property window that you pull up in IIS.Writing directly to the <AppSettings> will do the same thing.
Min
Sorry. The settings tab is only available when using web applications.
Bruno Shine
A: 

I believe the Application object is a built-in way to use global variables where you can store various values in the object as needed.

JB King
A: 

As an alternative to Application state, consider just making your variable static. It is basically going to amount to the same thing, with the added benefit of being strongly typed.

Personally, I'd go with what David suggested and use a singleton scoped service that is managed by an IoC container.

remotefacade
Static variables are shared by all users of an ASP.NET app. If you follow this suggestion, do so for read-only values only.
rp