简体   繁体   English

ASP.NET中的类将采用什么方式?

[英]What will be the way to go with classes in ASP.NET?

In my asp.net web app. 在我的asp.net网络应用中。 I have three classes (inside app_code folder) under namespace WPP which are as follows: 我在命名空间WPP下有三个类(位于app_code文件夹中),如下所示:

1. wpp_Common
2. wpp_SQL
3. wpp_Admin

All these classes contains different functions which I am using in my application to accomplish different tasks. 所有这些类都包含我在应用程序中用来完成不同任务的不同功能。 Such as, wpp_common contains a function which make my URL's SEO'd, and wpp_SQL have some functions which I am using to get details from database. 例如, wpp_common包含一个使我的URL进行wpp_common的功能,而wpp_SQL具有一些用于从数据库获取详细信息的功能。

Now, I am using these classes on different web pages web pages and in web controls. 现在,我在不同的网页网页和Web控件中使用这些类。 To use these classes I am creating instances of all three classes on the page where I am using them. 要使用这些类,我将在使用它们的页面上创建所有三个类的实例。

protected WPP.wpp_Common ObjCommon = new WPP.wpp_Common();
protected WPP.wpp_SQL ObjSQL = new WPP.wpp_SQL();
protected WPP.wpp_Admin ObjAdmin = new WPP.wpp_Admin();

So, I want to know, is this a better and only way to access my classes by making seprate instances at every page, is this method have any performance constraints. 因此,我想知道,这是通过在每个页面上创建单独的实例来访问我的类的更好且唯一的方法,该方法是否具有任何性能约束。

Or is there a better and logical way to access my classes from ASP.net web pages and web controls. 还是有一种更好的逻辑方法从ASP.net网页和Web控件访问我的类。

Thanks. 谢谢。

If these classes don't encapsulate anything mutable, it may be worth making the key methods utilized static. 如果这些类没有封装任何可变的东西,则可能有必要使使用的关键方法静态化。 Then, you don't even need an instance of the class. 然后,您甚至不需要该类的实例。 This seems to make sense for your SEO class. 这对于您的SEO课程似乎很有意义。 The SQL class you may want a shared instance as it may contain a reference to some SQL connection/class, but this could also be a parameter in a static method. 您可能需要共享实例的SQL类,因为它可能包含对某些SQL连接/类的引用,但这也可以是静态方法中的参数。

What you're doing seems okay to me, though. 不过,您的操作对我来说似乎还可以。

Really the answer boils down to the complexity of the class. 答案确实归结为课堂的复杂性。 If your classes are lightweight and have low initialization overhead, then you are probably OK with instatiating objects every time. 如果您的类是轻量级的并且初始化开销很低,那么您每次可能都可以使用实例化对象。 (Heck, even the ASP.NET runtime creates a new instance of your page object every time in classic ASP.NET - not sure about MVC). (糟糕的是,即使ASP.NET运行时每次也都在经典ASP.NET中创建页面对象的新实例-不确定MVC)。

But if your object has non-trivial initialization time (intensive processing in constructor or upon first method call), then you'll probably want to look at two options: 但是,如果您的对象有很短的初始化时间(在构造函数中或在第一个方法调用时进行大量处理),那么您可能需要查看两个选项:

One) Storing the object in the session - be careful here as your object may behave differently depending on the backend session store (memory, sql, etc). 一)将对象存储在会话中-在这里要小心,因为根据后端会话存储(内存,sql等),对象的行为可能有所不同。

MyHeavyObj obj = (MyHeaveyObj) Session["cachedObj"];

if (obj == null)
{
    obj = new MyHeavyObj();
    obj.Init();
    Session["cachedObj"] = obj;
}

obj.DoSomething();

Two) Writing your own Windows Service that serves up your objects via remoting and takes care of initiatlization internally. 二)编写自己的Windows服务,该服务通过远程处理为对象提供服务并在内部进行初始化。 The drawback here is you need to make sure your service scales as your traffic grows. 这里的缺点是您需要确保服务随着流量的增长而扩展。

MyHeavyObj obj = GetHeavyObjViaRemoting();

obj.DoSomething();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net 5自定义类中的依赖注入,正确的方法是什么? - Dependency Injection in asp.net 5 custom classes, what is the correct way? 为asp.net创建自定义框架扩展的最佳方法是什么? - What is the best way to go about creating a custom framework extension for asp.net? ASP.NET核心2.2:在框架类之间共享特定于请求的数据最安全的方法是什么? - ASP.NET core 2.2: what is the safest way of sharing request-specific data between framework classes? 使用类从asp.net中的数据库填充下拉列表的方法是什么? - What is the way to populate a dropdownlist from a database in asp.net by using classes? ASP.NET类问题 - ASP.NET Classes Question 在ASP.NET Core(以前的ASP.NET 5)中嵌套路由的简便方法是什么? - What is a convenient way of nesting routes in ASP.NET Core (previously ASP.NET 5)? 在ASP.NET中动态生成PNG的好方法是什么? - What's a good way to dynamically generate PNGs in ASP.NET? 将数据从ASP.NET传递到AngularJS的最佳方法是什么? - What is the best way to pass data from ASP.NET to AngularJS? 在ASP.Net MVC中实现Unity的正确方法是什么? - What is the right way to implement Unity in ASP.Net MVC? 以编程方式触发ASP.NET控件事件的正确方法是什么? - What is the proper way to fire an ASP.NET control event programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM