How to create Global variables, ASP.NET, global asax
I have some data layer classes that are to be used very frequently al开发者_如何学Gomost 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.asaxI am a new to asp.net, so try to give the syntax along with an explanation.
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.
"ASP.NET Application State Overview" contains an object that can be used to store data across all users, similar to the Session object in terms of being able to store various key-value pairs.
Use public structs. They are more efficient than classes and more flexible than enums.
Create a file (preferably in the '/Classes' folder) with the following code:
public struct CreditCardReasonCodes
{
public const int Accepted = 100;
public const int InsufficientFunds = 204;
public const int ExpiredCard = 202;
}
IMPORTANT: Do not put any namespace so that the structure will be seen globally across your Web Application.
To reference it across your code simply use the following syntax:
if (webServiceResult == CreditCardReasonCodes.Accepted)
{
Console.WriteLine("Authorization approved.")
}
Using "const" members also makes your values immutable at compile time with no possibility to have them modified during the execution of the application.
What's more, I strongly recommend you reading the great article https://lowleveldesign.org/2011/07/20/global-asax-in-asp-net/ to understand how the Global class in global.asax works.
精彩评论