如何在asp.net中创建一个类的实例只有一次 [英] How to create instance of a class only once in asp.net

查看:151
本文介绍了如何在asp.net中创建一个类的实例只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一类的实例,在部分班级一个aspx page.Under的Page_Load或按钮点击的方法我试图将值设置为类的创建。但是,当每次回发发生地新的实例被创建,我失去了previous值。

 公共部分类DatabaseSelection:System.Web.UI.Page
     {
        DBProperties dbpro;
        元数据obmeta;    保护无效的Page_Load(对象发件人,EventArgs的发送)        {
            如果(!的IsPostBack)
            {
               dbpro =新DBProperties();            }


解决方案

如果您需要该实例通过应用程序把它在应用程序或使用单系列:

应用[富] =新MyClass的();
见对方的回答。

如果你需要这个单个请求(这似乎不太可能在这里):

HttpContext.Current.Items [富] =新MyClass的();

如果你需要这个跨请求的,以下是根据您的场景的所有选项:


  • 序列化为曲奇(将在每次请求,所以如果管道是一个问题不使用这个被转移)。适合每个用户的数据。

  • 存储在会话:会话[富] =新MyClass的(); //我个人,因为它往往会增加你的记忆pressure为你的用户群的增长,但如果这是小的,那么这是一个很好的选择,因为它不会增加带宽消耗和性能不喜欢这个选项。适合每个用户的存储。和持久化到数据库或内存(和其他人)可以配置。

  • 存储在高速缓存。优点是在寿命和良好的控制是否每个用户取决于你用​​什么键上有一定的灵活性。

  • 存储在ViewState中。在单个请求好,可扩展性,但​​增加了有效载荷。

  • 存放在隐藏的VAR(大致相同的ViewState)

我真的不知道我会建议Singleton模式。从技术上将单身,只要坚持围绕作为您的AppDomain有效相似于应用程序变量。

A instance of a class is created in the partial class of an aspx page.Under page_load or button click method I'm trying to set the value to the class. but when each postback takes place new instance is created and I'm losing the previous value.

public partial class DatabaseSelection : System.Web.UI.Page
     {
        DBProperties dbpro;
        Metadata obmeta;      

    protected void Page_Load(object sender, EventArgs e)

        {
            if (!IsPostBack)
            {
               dbpro = new DBProperties();

            }  

解决方案

If you need this instance by application throw it in the Application or use a Singleton collection:

Application["Foo"] = new MyClass(); See other answer.

If you need this for a single request (which seems unlikely here):

HttpContext.Current.Items["Foo"] = new MyClass();

If you need this across requests the the following are all options depending on your scenario:

  • Serialize into Cookie (will be transfered on every request so if the pipeline is an issue don't use this). Good for per user data.
  • Store in Session: Session["Foo"] = new MyClass(); // I personally don't like this option because it tends to grow your memory pressure as your user base grows, but if this is small then this is a good option as it does not increase bandwidth consumption and performance. Good for per user storage. And persistence to DB or memory (and others) can be configured.
  • Store in Cache. Benefits are good control over lifetime and some flexibility on whether per user depending on what keys you use.
  • Store in ViewState. Good across a single request, scalable, but increases payload.
  • Store in hidden var (about the same as ViewState)

I am not really sure I would recommend the Singleton pattern. Technically singletons will stick around as long as your AppDomain effectively being similar to the Application variable.

这篇关于如何在asp.net中创建一个类的实例只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆