Jack
Jack

Reputation: 10047

What is the best way to manage Global variable in asp.net?

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.

Upvotes: 0

Views: 814

Answers (4)

remotefacade
remotefacade

Reputation: 359

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.

Upvotes: 0

JB King
JB King

Reputation: 11910

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.

Upvotes: 0

Bruno Shine
Bruno Shine

Reputation: 1031

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

Upvotes: 0

kͩeͣmͮpͥ ͩ
kͩeͣmͮpͥ ͩ

Reputation: 7856

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)

Upvotes: 2

Related Questions