zoukankan      html  css  js  c++  java
  • Duwamish代码分析篇一

    Duwamish代码分析篇一

    继续前面的2篇POSTDuwamish架构分析篇》Duwamish部署方案篇》,这里在代码层次上分析Duwamish  7.0范例,主要目的是解析Duwamish范例中值得推荐的编码风格和提炼出可以重用的代码或Class。

    1,读取配置文件类-SystemFramework\ApplicationConfiguration.cs

    ApplicationConfiguration类用来读取web.config文件中自定义section的配置信息,初始化一些基本设置。

    ApplicationConfiguration类实现IconfigurationSectionHandler接口,并需要实现[C#]

    object Create(

       object parent,

       object configContext,

       XmlNode section

    )方法,以分析配置节的 XML。返回的对象被添加到配置集合中,并通过 GetConfig 访问。

    部分代码片断解释:

    1Code Snippet 1 – ApplicationConfiguration. OnApplicationStart()方法

    public static void OnApplicationStart(String myAppPath)

    {

        appRoot = myAppPath;

        System.Configuration.ConfigurationSettings.GetConfig("ApplicationConfiguration");

        System.Configuration.ConfigurationSettings.GetConfig("DuwamishConfiguration");

        System.Configuration.ConfigurationSettings.GetConfig("SourceViewer");      

    }

    ConfigurationSettings 类还提供了一个公共方法ConfigurationSettings.GetConfig() 用于返回用户定义的配置节的配置设置,传入的参数section name,如"ApplicationConfiguration",表示要读取的配置节。

    NameValueCollection nv=new NameValueCollection();

    //实例化NameValueCollection 类对象

    nv=(NameValueCollection)ConfigurationSettings.GetConfig("ApplicationConfiguration ");

    //返回用户定义的配置节的设置

    return nv["SystemFramework.Tracing.Enabled"].ToString();

    //返回特定键值,如SystemFramework.Tracing.Enabled

    不过,ConfigurationSettings.GetConfig()方法在调用时,自动调用Create()方法,可以看到ApplicationConfiguration.Create()方法正是用来读取指定section的配置,并初始化设置参数。

    Global.asax 的 Application_OnStart 事件处理程序向 SystemFramework 的ApplicationConfiguration 类 OnApplicationStart 方法发出调用,正是上述的代码片断。

    2Code Snippet 2 Global.asaxApplication_OnStart()方法

    void Application_OnStart()

    {

        ApplicationConfiguration.OnApplicationStart(Context.Server.MapPath( Context.Request.ApplicationPath ));

        string configPath = Path.Combine(Context.Server.MapPath( Context.Request.ApplicationPath ),"remotingclient.cfg");

        if(File.Exists(configPath))

            RemotingConfiguration.Configure(configPath);

    }

    该方法肩负二大任务:(1)调用ApplicationConfiguration.OnApplicationStart()方法,并传入application的根目录(Root Directory)。(2)检测Client端的remoting配置文件是否存在(其实是web server端),如果存在,则读取并初始化remoting配置信息,如配置通道Channel等等,详见《Duwamish部署方案篇》。

    2,读取web.configDuwamish相关的一些配置-Common\DuwamishConfiguration.cs

    Common\DuwamishConfiguration.cs也实现IconfigurationSectionHandler接口,与SystemFramework\ApplicationConfiguration.cs类相似。

    DuwamishConfiguration配置节包括如下一些配置信息:

    Database connection string(Database连接串)Duwamish.DataAccess.ConnectionString,是否允许页面缓存Duwamish.Web.EnablePageCache,页面缓存过期时间Duwamish.Web.PageCacheExpiresInSeconds,是否允许SSL连接Duwamish.Web.EnableSsl等等。

    如上所述,调用DuwamishConfiguration Class 是由SystemFramework\ApplicationConfiguration.cs的OnApplicationStart()方法完成的:

    System.Configuration.ConfigurationSettings.GetConfig("DuwamishConfiguration");

    看看页面缓存配置在web page中如何使用的(web\book.aspx.cs文件为例):

    //

    // If everything succeeded, then enable page caching as indicated

    // by the current application configuration.

    //

    if ( DuwamishConfiguration.EnablePageCache )

    {

        //Enable  Page Caching...

        Response.Cache.SetExpires ( DateTime.Now.AddSeconds(DuwamishConfiguration.PageCacheExpiresInSeconds));

        Response.Cache.SetCacheability(HttpCacheability.Public);

    }

    在Page_Load事件中最后判断是否允许页面缓存。

    3,验证数据合法性类-SystemFramework\ApplicationAssert.cs

    SystemFramework\ApplicationAssert.cs Class用来进行错误检测,并调用SystemFramework\ApplicationLog.cs Class记录错误日志。

    学习其中的部分代码片断:

    1Code Snippet 1 – Check Method

    [ConditionalAttribute("DEBUG")]

    public static void Check(bool condition, String errorText, int lineNumber)

    {

        if ( !condition )

        {

            String detailMessage = String.Empty;

            StringBuilder strBuilder;

            GenerateStackTrace(lineNumber, out detailMessage);

            strBuilder = new StringBuilder();

            strBuilder.Append("Assert: ").Append("\r\n").Append(errorText).Append("\r\n").Append(detailMessage);

            ApplicationLog.WriteWarning(strBuilder.ToString());

            System.Diagnostics.Debug.Fail(errorText, detailMessage);

        }

    }

    [ConditionalAttribute("DEBUG")]定义Check()方法为conditional method,如果预处理符号(preprocessor symbol)没有定义,compiler不仅忽略该方法,而且忽略对该方法的调用,和#if DEBUG / #else / #endif有些类似。

    该方法用来判断条件condition是否为true,如果为false,则调用SystemFramework\ApplicationLog.WriteWarning()方法记录错误日志。

  • 相关阅读:
    布隆过滤器原理与应用场景
    【转】程序员的世界真的很难懂~
    IDEA 2019.2.4 破解安装教程
    【转】只有程序员才能看得懂的段子
    Linux 正则表达式
    【转】雷军做程序员时写的博客,很强大!
    如何同步 Linux 集群系统时间
    百度共享云盘
    Shell 脚本 test 命令详解
    Linux 命令大全
  • 原文地址:https://www.cnblogs.com/Spring/p/422554.html
Copyright © 2011-2022 走看看