使用HttpContext.Current.Application存储简单的数据 [英] Using HttpContext.Current.Application to store simple data

查看:1000
本文介绍了使用HttpContext.Current.Application存储简单的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储在我的ASP.NET MVC应用程序的简单对象(含三根弦)的小单子。该列表是从数据库加载,它很少更新,通过在该网站的管理区编辑一些值。

I want to store a small list of a simple object (containing three strings) in my ASP.NET MVC application. The list is loaded from the database and it is updated rarely by editing some values in the site's admin area.

我想用HttpContext.Current.Application存储它的。这样我可以加载它在G​​lobal.asax:

I'm thinking of using HttpContext.Current.Application to store it. This way I can load it in the Global.asax:

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);

        HttpContext.Current.Application["myObject"] = loadDataFromSql(); // returns my object
    }

然后可以轻松地从任何控制器或意见,需要引用它。然后在事件管理方面调用updateMyObject控制器动作,我就可以更新数据库,并再次将其装入并替换 HttpContext.Current.Application [myObject的]

是否有任何缺点,这样做呢?它看起来像它会正常工作了什么,我想实现的,但是没有人知道更好的方式来做到这一点,假设有就是我已经奠定了该方法的一些主要缺点?

Are there any cons to doing this? It seems like it will work fine for what I am trying to achieve, however does anyone know of a better way to do this, assuming there is some major disadvantage to the method I've laid out?

推荐答案

在实际做的是的缓存的,这是伟大的,因为你减少调用到外部存储(数据库或文件,随你)。在权衡内存使用,当然。现在,几乎所有现代的Web框架,包括ASP.NET,包括某种缓存机制。无论你使用它,或者你使用某种全局变量。

What you actually do is Caching, and it's great, since you reduce calls to an external storage (a database or a file, whatever). The trade-off is memory usage, of course. Now, almost any modern web framework, including ASP.NET, includes some kind of a caching mechanism. Either you use it, or you use some kind of a global variable.

在ASP.NET的内置缓存对象存储的数据具有一定的优势显著,因为这种机制实际上检查内存使用情况,并根据一定的规则删除缓存的数据。

Storing data in ASP.NET's built-in Cache object has some significant advantages, since this mechanism actually checks the memory usage and removes the cached data according to some rules.

不过,如果你想缓存中的数据在应用程序被大量使用,其规模不宜过大(比如,小于1 MB),您可能希望它存储为一个全局变量。

However, if the data you want to cache is intensively used across the application, and its size is not too large (say, smaller than 1 MB), you may want to store it in as a global variable.

在ASP.NET中,全局变量或者通过使用应用的对象,就像你在你的问题描述的实现,或者通过在内部写的公共静态属性/领域/公共类。

In ASP.NET, global variables are achieved by either using the Application object, like you described in your question, or by writing public static properties/fields in an internal/public class.

下面是我的解决方案,以静态属性。请注意,我用的锁定对象,以防止损坏内部数据。它看起来是这样的:

Here's my solution to static properties. Note that I use a locking object, to protect the inner data from corruption. It looks like this:

public class WhateverClass
{
  private static object theLocker = new object();
  private static YourDataType theData;
  public static YourDataType TheData
  {
    get
    {
      lock (theLocker)
      {
        return theData;
      }
    }
    set
    {
      lock (theLocker)
      {
        theData = value;
      }
    }
  }
}

的用法很简单:

第一次,在的Application_Start:

First time, in Application_Start:

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);

    WhateverClass.TheData = loadDataFromSql();
}

在任何控制器:

var myData = WhateverClass.TheData;

这方法是更好,因为你有类型安全,因为这个公共静态属性可以用确切类型进行明确声明。此外,这种存储的是更容易测试,因为它不依赖于Web上下文

This approach is better because you have type safety, since this public static property can be explicitly declared with the exact type. In addition, this kind of storage is more testable since it doesn't depend on the web context.

心连心!

这篇关于使用HttpContext.Current.Application存储简单的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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