I have some data layer classes that are to be used very frequently almost on the whole site.
I was working on a windows application previously and i used to create its object in module (vb.net) but now i m working in C# and on ASP.NET.
Now i need to do same thing so that i need not create same object multiple times on every page.
I want to use something like using a global variable.
How can i do this?
Is it done by using global.asax
can i create my objects in global.asax
I am a new to asp.net, so try to give the syntax along with an explanation.
I use Global. asax to define global variables e.g. You can set them in whichever event suits your purpose: Application_Start, Application_BeginRequest etc. You can reference them throughout the application.
In C# you cannot define true global variables (in the sense that they don't belong to any class). You can then retrieve the defined values anywhere in your code (provided it's part of the same namespace ): String code = Globals. CODE_PREFIX + value.
Global. asax is the asp.net application file. It is an optional file that handles events raised by ASP.NET or by HttpModules. Mostly used for application and session start/end events and for global error handling.
How to add global. asax file: Select Website >>Add New Item (or Project >> Add New Item if you're using the Visual Studio web project model) and choose the Global Application Class template.
You don't actually need to use the global.asax. You can make a class that exposes your objects as static
s. This is probably the most simple way
public static class GlobalVariables {
public static int GlobalCounter { get; set; }
}
You can also use the Application State or even the ASP.NET Cache because those are shared across all sessions.
However, If I were in this situation, I would use a framework like Spring.NET to manage all my Sington instances.
Here is a quick example of how you would get at your class instances using Spring.NET
//The context object holds references to all of your objects
//You can wrap this up in a helper method
IApplicationContext ctx = ContextRegistry.GetContext();
//Get a global object from the context. The context knows about "MyGlobal"
//through a configuration file
var global = (MyClass)ctx.GetObject("MyGloblal");
//in a different page you can access the instance the same way
//as long as you have specified Singleton in your configuration
But really, the bigger question here is why do you need to use global variables? I am guessing you don't really need them and there might be a better big picture soluion for you.
I would recomend you to use application state for this purpose.
I'll skip over the "should" part about using global variables in .NET and show some code I'm working in now that uses Global.asax for some "global" variables. Here's some info from that file:
public class Global : System.Web.HttpApplication
{
public enum InvestigationRole
{
Complainent,
Respondent,
Witness,
}
public static string Dog = "Boston Terrier";
}
So from the ASPX pages, you can access those members by opening up the static Global class like so:
protected void Page_Load(object sender, EventArgs e)
{
string theDog = Global.Dog;
// copies "Boston Terrier" into the new string.
Global.InvestigationRole thisRole = Global.InvestigationRole.Witness;
// new instance of this enum.
}
Buyer beware though. There are better ways of treating the concept of "global variables" in the .NET world, but the above will at least get you a layer of abstraction above repeating the same strings in all your ASPX pages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With