ASP.NET MVC会话VS全球VS缓存 [英] ASP.NET MVC Session vs Global vs Cache

查看:144
本文介绍了ASP.NET MVC会话VS全球VS缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有写在香草ASP.NET的应用程序,我想端口到ASP.NET MVC。

I have an application that was written in vanilla ASP.NET that I would like to port over to ASP.NET MVC.

但是,我感到困惑的持久化对象的正确的地方。我需要持续几个原因:

I, however, am confused about the right place to persist objects. I need to persist for a couple reasons:


  1. 我想大家有一个数据库连接,包裹在一个资源库或经理样式对象。

  2. 每个用户都有一个需要保存在每个会话的基础用户对象。

  1. I would like all to have a single database connection, wrapped in a "repository" or "manager" style object.
  2. Each user has a user object that needs to be saved on a per-session basis.

通常情况下,我会说,#1将被保存为Globals.asax一个静态项目可以使用 Global.Repository 或类似的冲击。

Normally, I would say that #1 would be saved as a static item in the Globals.asax that can be hit using Global.Repository or similar.

和我通常会说,#2应与地方的会话后备存储在基类的页面的一个属性。

And I would normally say that #2 should be a property with a session backing store somewhere in the base class of the pages.

现在我感到困惑的原因是,我听说在会议MVC已经改变,在Global.asax不再持有相同的类。此外,页面的概念已被去除,因此增加一个属性基类控制器似乎...错了。

Now the reason I am confused is that I have heard that sessions have changed in MVC, and the Global.asax no longer holds the same class. Also, the concept of pages has been removed, so adding a property to the base class of a controller seems... wrong.

此话怎讲亚勒?

推荐答案

您的数据库会去为你的控制器基类。这个基类应该扩展控制器,和所有的控制器应该扩展的基类。这里有一个小例子:

Your database would go in a base class for your controllers. This base class should extend Controller, and all your controllers should extend the base class. Here's a little example:

public class BaseController : Controller
{
    private AuthServices _auth;
    private LogHelper _log;
    private Repository _repository;

    /// <summary>
    /// <see cref="AuthServices"/>
    /// </summary>
    protected AuthServices Authorization
    {
        get { return _auth ?? (_auth = new AuthServices()); }
    }

    /// <summary>
    /// <see cref="LogHelper"/>
    /// </summary>
    protected LogHelper Log
    {
        get { return _log ?? (_log = new LogHelper()); }
    }

    /// <summary>
    /// <see cref="Repository"/>
    /// </summary>
    protected Repository Repository
    {
        get { return _repository ?? (_repository = new Repository()); }
    }
}

注意延迟实例。这使我在测试前忙里偷闲,并设置我的私人领域与嘲笑。

Notice the lazy instantiation. That allows me to sneak in before running tests and set my private fields with mocks.

对于会话,用户对象仍然可以被保存在会话就像在传统的ASP.NET应用程序。几乎所有的东西仍然存在(响应,缓存,会话等),但他们中的一些已经被包裹着,从System.Web.Abstractions类,以便它们可以模拟来测试。他们都还是行为方式相同,但你不应该在他们的传统角色使用其中的一些(例如,不Response.Redirect的,返回一个ActionResult,如执行你的重定向RedirectToRouteResult)。

As for the session, your User object can still be saved in the session just as in a traditional ASP.NET application. Almost everything is still around (Response, Cache, Session, etc), but some of them have been wrapped with classes from System.Web.Abstractions so that they can be mocked for testing. They all still behave the same way, though you shouldn't use some of them in their traditional role (e.g., don't Response.Redirect, return an ActionResult such as RedirectToRouteResult that performs your redirection).

至于您的问题背后的推理......

As for the reasoning behind your questions....

不要单一的数据库连接上强调。根据您的实现它甚至可能是一个糟糕的主意,因为请求可能彼此一步。只要打开你连通的,使用它,配置和/完成后关闭它。

Don't stress on a single db connection. Depending on your implementation it may even be a bad idea, as requests may step on each other. Just open your connex, use it, and dispose/close it when done.

此外,那MVC带来的最大的变化之一是有状态模型的排斥传统ASP.NET试图把Web开发。所有这些框架和视图状态不存在了(不注重幕后的人)。你不放那么复杂和更强大的Web应用程序较少状态。试试吧,你可能会喜欢它。

Also, one of the biggest changes that MVC brings is the rejection of the stateful model that traditional ASP.NET attempted to bring to web development. All that framework and viewstate doesn't exist anymore (pay no attention to the man behind the curtain). The less state you hold onto the less complex and more robust your web application is. Try it, you might like it.

这篇关于ASP.NET MVC会话VS全球VS缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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