CraigJSte
CraigJSte

Reputation: 932

Questions on Design of an ASP.NET 2.0 Application

As a novice - intermediate C# programmer, my debugging issues surround the most basic elements of C# Program Design. My project is a web-based, computerized trading system. Working with (2) API's, 1 for pricing and the other for Orders my problem seems to be in code design and issues associated with multiple sessions.

I try to use Delegates for all events.

My current questions regarding Windows Forms and Object

  1. Is this the correct way (or a 'proper way') to instantiate class objects & event delegates using aspx pages? (my examples often serve to confuse more than help, I realize).

    public partial class admin_Admin : System.Web.UI.Page
    {
    
     private static Downloader dl = null;
     private SendOrderDelegate sendError;
    
     protected void Page_Load(object sender, EventArgs e)
     {
          if (!Page.IsPostBack)
          {
               if (dl == null)
               {
                    Main();
               }
          }
     }
    
     protected static void Main()
     {
          dl = new Downloader();
          sendOrder = new Steury.Trading.SendOrderDelegate(dl.Order);
      }
    }
    
  2. When and How to use the Main() Method? What is the definition for using Main() in a complex Web Application with multiple programs (Pricing, Orders, BackTesting, Optimization), all running simultaneously and using different aspx pages??

Do I use Main() above for each aspx page, and again for each main Class program... Orders.cs, OrderShort.cs, BacktTest.cs, BackTestShort.cs, Optimize.cs, OptimizeShort.cs, or do I use Main Only for the 2 aspx pages that login to the Pricing and Orders Servers and then maintain static session variables that connect to the proper classes?

As you can tell, I am struggling with these concepts and have not found a resource that goes into detail for very complex scenarios. Most if not all use very simple class examples.. I have found this to be helpful Core C# and .NET http://flylib.com/books/en/4.253.1.1/1/ .

  1. Is there any good advice on when to use the Static Keyword? Should I use it for the Top Level Pages only? I am not deploying a multiple user project at this point.

Any other suggestions would be useful, constructive criticism included.

Upvotes: 2

Views: 88

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

You are trying to adapt Windows forms techniques to Web pages. That won't work. In particular, your Main function is not only pointless here, but actively harmful. By making Main static, that means it cannot access any member instances data or properties of the page, unless they too are static.

Why do you even want to mark it static? What's your reasoning? Because it's static in Windows forms apps?

You usually use static methods only when those methods do not perform any state changing functionality. A good way to get an idea of how static methods are typically used is to look at the documentation for common .NET classes, like string and see which functions they define as static.

The thing about web pages to remember is that they are demand based, and they are stateless. In Windows Forms or console apps you "run" the program and it stays running until you quit. Web apps only exist within the context of a single request, although sessions provide some continuity between requests.

So the point is, each session is it's own universe. It starts, stops, and ends each time a page is requested. You have only session data to tie seperate requests together.

As for multiple user... all web apps are multiple user. That's what the web environment is designed around. Two or more people can and do access web pages simultaneously and there's no way you can stop that.

Upvotes: 2

Abel
Abel

Reputation: 57149

When and How to use the Main() Method?

You don't. Remove it. The Main-functionality (the code you want to run the very first time your whole website is run, i.e. Application), or when a certain user accesses it the first time, i.e. Session) is moved to global.asax. By default, this file is not in your web application project, but you can add it.

Is this the correct way (or a 'proper way') to instantiate class objects & event delegates

No, it is not. Though the IsPostBack check is good (it means whether or not the page has been posted by a user, i.e., whether or not he clicked a submit button on your page, which posts back to the same page). The thing wrong here is the static main function. Consider every page a class (it is a class, really) and that the system instantiates that class for you. The place for initiation is Page_Init. But many people keep it simple and put that in Page_Load. These special methods are automatically called by the ASP.NET system when the page is loaded.

Is there any good advice on when to use the Static Keyword?

That's not easy to answer. In general, in a web page, you hardly ever use the static keyword, simply because many people and user simultaneously access your pages. However, utility classes and methods can sometimes be static. It's a total different environment than normal windows applications, which typically run in a protected environment.

and have not found a resource that goes into detail for very complex scenarios

There are many great books around on ASP.NET. Some go into very complex scenarios. But considering the current phase you're in, I'd advice you to stick to simple scenarios to begin with.

Upvotes: 2

Related Questions