有关 ASP.NET 2.0 应用程序设计的问题
作为一名初中级 C# 程序员,我的调试问题围绕着 C# 程序设计的最基本元素。我的项目是一个基于网络的计算机化交易系统。使用 (2) 个 API,其中 1 个用于定价,另一个用于订单,我的问题似乎在于代码设计和与多个会话相关的问题。
我尝试在所有事件中使用委托。
我当前关于 Windows 窗体和对象的问题
这是实例化类对象和对象的正确方法(或“正确的方法”)吗?使用 aspx 页面的事件代表? (我意识到,我的例子常常让人困惑而不是帮助)。
公共部分类 admin_Admin : System.Web.UI.Page { 私有静态下载器 dl = null; 私有SendOrderDelegate发送错误; protected void Page_Load(对象发送者,EventArgs e) { if (!Page.IsPostBack) { 如果(dl==空) { 主要的(); } } } 受保护的静态无效Main() { dl = 新的下载器(); sendOrder = new Steury.Trading.SendOrderDelegate(dl.Order); } }
何时以及如何使用 Main() 方法?在具有多个程序(定价、订单、回测、优化)的复杂 Web 应用程序中使用 Main() 的定义是什么,所有程序同时运行并使用不同的 aspx 页面?
我是否对每个 aspx 页面使用上面的 Main(),并再次对每个主类程序使用... Orders.cs、OrderShort.cs、BacktTest.cs、BackTestShort.cs、Optimize.cs、OptimizeShort.cs,还是使用主要仅适用于登录定价和订单服务器然后维护连接到正确类的静态会话变量的 2 个 aspx 页面?
正如您所知,我正在努力解决这些概念,并且还没有找到详细介绍非常复杂场景的资源。大多数(如果不是全部)都使用非常简单的类示例。我发现这对 Core C# 和 .NET http://flylib.com/books/en/4.253.1.1/1/ 。
- 关于何时使用 Static 关键字有什么好的建议吗?我应该只将它用于顶级页面吗?我目前没有部署多用户项目。
任何其他建议都会有用,包括建设性的批评。
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
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); } }
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/ .
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不知道。将其删除。主要功能(您想要在整个网站第一次运行时运行的代码,即应用程序),或者当某个用户第一次访问它时,即会话)被移动到global.asax。默认情况下,此文件不在您的 Web 应用程序项目中,但您可以添加它。
不,不是。尽管
IsPostBack
检查很好(这意味着该页面是否已由用户发布,即,他是否单击了页面上的提交按钮,回发 到同一页面)。这里的问题是静态 main 函数。将每个页面视为一个类(实际上它是一个类),并且系统为您实例化该类。启动的地方是Page_Init
。但许多人保持简单并将其放在Page_Load
中。当页面加载时,ASP.NET 系统会自动调用这些特殊方法。这并不容易回答。一般来说,在网页中,您几乎不会使用 static 关键字,因为许多人和用户同时访问您的页面。然而,实用程序类和方法有时可以是静态的。这是一个与通常在受保护环境中运行的普通 Windows 应用程序完全不同的环境。
。关于 ASP.NET 有很多很棒的书籍。有些涉及非常复杂的场景。但考虑到您当前所处的阶段,我建议您一开始就坚持简单的场景。
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.
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 isPage_Init
. But many people keep it simple and put that inPage_Load
. These special methods are automatically called by the ASP.NET system when the page is loaded.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.
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.
您正在尝试使 Windows 窗体技术适应网页。那是行不通的。特别是,您的 Main 功能在这里不仅毫无意义,而且还非常有害。通过将 Main 设置为静态,这意味着它无法访问页面的任何成员实例数据或属性,除非它们也是静态的。
为什么你甚至想将其标记为静态?你的理由是什么?因为它在 Windows 窗体应用程序中是静态的?
通常,仅当静态方法不执行任何状态更改功能时才使用这些方法。了解静态方法通常如何使用的一个好方法是查看常见 .NET 类(例如 string)的文档,并查看它们将哪些函数定义为静态。
关于网页要记住的一点是它们是基于需求的,并且是无状态的。在 Windows 窗体或控制台应用程序中,您“运行”该程序,它会一直运行,直到您退出。尽管会话在请求之间提供了一定的连续性,但 Web 应用程序仅存在于单个请求的上下文中。
所以重点是,每个会话都有它自己的宇宙。每次请求页面时,它都会启动、停止和结束。您只有会话数据才能将单独的请求绑定在一起。
至于多用户......所有网络应用程序都是多用户的。这就是网络环境的设计初衷。两个或更多人可以并且确实同时访问网页,并且您无法阻止这种情况。
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.