原创  CS2中的CSContext 收藏

由于WEB请求是无状态的,在请求过程中需要保存的共享信息保存在HttpContext中,在Http请求过程中HttpContext始终贯穿整个请求过程,在CS2中相当于对HTTPContext进行了扩展,除了包含HTTPContext中的信息外,CSContext还包含了在CS的整个请求过程中需要共享的自己的数据,如当前请求的用户、Section、Post等等公用数据。
        在CS中,一个页面往往涉及到很多用户控件,每个用户控件对应一个类,类和类之间没有明显的联系,这就需要我们提供一个公用数据的类来保存在整个请求过程中的用户数据,在CS的CSContext就是这个作用,这样的好处是,在整个请求过程中公用数据在获取第一次后就保存到CSContext中了,当前请求的其他地方用的时候就不需要重复获取了。让我们来看看CSContext都保存了哪些数据。
        打开CSContext类时候会看到类中的字段都分门别类的集合在一起,可以轻易的查看到每类的相关数据,这里简单介绍一下这些数据:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Web;
using System.Collections;
using CommunityServer.Configuration;

namespace CommunityServer.Components 
{
    
public delegate bool UrlReWriterDelegate(HttpContext context);

    
/**//// <summary>
    
/// The CSContext represents common properties and settings used through out of a Request. All data stored
    
/// in the context will be cleared at the end of the request
    
/// 
    
/// This object should be safe to use outside of a web request, but querystring and other values should be prepopulated
    
/// 
    
/// Each CS thread must create an instance of the CSContext using one of the Three Create overloads. In some cases, 
    
/// the CreateEmptyContext method may be used, but it is NOT recommended.
    
/// </summary>

    public sealed class CSContext 
    
{

        Private Containers
Private Containers

        Initialize  and cnstr.
's#region Initialize  and cnstr.'s

        
/**//// <summary>
        
/// Create/Instatiate items that will vary based on where this object 
        
/// is first created
        
/// 
        
/// We could wire up Path, encoding, etc as necessary
        
/// </summary>

        private void Initialize(NameValueCollection qs, Uri uri, string rawUrl, string siteUrl)
        
{
            _queryString 
= qs;
            _siteUrl 
= siteUrl;
            _currentUri 
= uri;
            _rawUrl 
= rawUrl;
            
        }


        
/**//// <summary>
        
/// cntr called when no HttpContext is available
        
/// </summary>

        private CSContext(Uri uri, string siteUrl)
        
{
            Initialize(
new NameValueCollection(), uri, uri.ToString(), siteUrl);
        }


        
/**//// <summary>
        
/// cnst called when HttpContext is avaiable
        
/// </summary>
        
/// <param name="context"></param>

        private CSContext(HttpContext context, bool includeQS)
        
{
            
this._httpContext = context;

            
if(includeQS)
            
{
            Initialize(
new NameValueCollection(context.Request.QueryString), context.Request.Url, context.Request.RawUrl, GetSiteUrl());
        }

            
else
            
{
                   Initialize(
null, context.Request.Url, context.Request.RawUrl, GetSiteUrl());
            }

        }


        
#endregion

        Create
Create

        Core Properties
Core Properties

        Helpers
Helpers

        CS Data
CS Data

        Status Properties
Status Properties

        Common QueryString Properties
Common QueryString Properties

        State
State
    }

}

 

Private Containers :一些简单的内部字段;
Initialize  and cnstr.'s:初始化和私有的构造函数;
Create:静态的Create方法,通过这些冲载的Create方法,在最先调用的时候就构造了CSContext的一个对象;
Core Properties:一些核心的属性,这些属性提供了CSContext最常用的功能。其中Items这个的作用是在当前请求过程中,如果有那些常用的针对请求者的数据保存在此,避免在一次请求多次处理,保存在此后,当前请求的其他地方需要用到此数据时就不需要再处理了。我们可以看到在解决方案里搜索“csContext.Items”这样的关键字找到这些应用。
Helpers:为此类的其他方法提供处理程序的一组方法。
CS Data:保存CS特有的公用数据,比如User、SiteSettings、Post、Section等等,这些都是可以公用的数据,所以统一放在这里,一般在用户控件的基类会有这些数据的处理,所以在我们使用的时候调用这些公用数据很方便。
Status Properties:请求状态的一组属性。
Common QueryString Properties:通过请求参数获取一些公用的数据,这些都是在CS中非常常用的参数,如:sectionID、GroupID、postID等等,在应用过程中我们子需要直接使用这些属性就可以了。
State:一组静态属性和方法,在第一次请求的时候通过调用Create方法创建CSContext对象并将对象保存到HttpContext,当以后需要获取CSContext对象的时候再从HttpContext获取,同时CSContext也保存有HttpContext对象的引用。在这个组里还有一个很重要的方法,可以把CSContext保存到其他区域(非HttpContext的地方),这主要是为了提供非Http请求时用的,比如单元测试等等。

        CSContext在CS中的作用很重要,理解它是理解CS工作原理的前提,说到低它就是为了共享数据而出现的,在用户控件组成页面的CS中共享数据显得尤为重要,这样的设计方法借鉴到自己的项目也是个很好的选择。

发表于 @ 2006年11月09日 14:00:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:模板化控件  | 新一篇:CS2中的关键词及数据结构

  • 发表评论
  • 评论内容:
  •  
Copyright © ekun008
Powered by CSDN Blog