BlogEngine.NET (一)配置
来源:互联网 发布:amazon是什么软件 编辑:程序博客网 时间:2024/05/22 08:20
因为日志的配置信息比较多,而且可能每个人的配置都不相同,所以BlogEngine.NET中的 配置信息需要统一管理,日志结构图如下:
BlogBasePage类是个抽象类,是其他web页面的基类,它会使用BlogSettings获得配置信息,代码如下:
public abstract class BlogBasePage : System.Web.UI.Page
...{
private string _Theme = BlogSettings.Instance.Theme;
/**//// <summary>
/// Assignes the selected theme to the pages.
/// </summary>
protected override void OnPreInit(EventArgs e)
...{
if (Request.QueryString["theme"] != null)
_Theme = Request.QueryString["theme"];
MasterPageFile = "~/themes/" + _Theme + "/site.master";
base.OnPreInit(e);
if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["deletepost"]))
...{
if (Page.User.Identity.IsAuthenticated)
...{
Post post = BlogEngine.Core.Post.GetPost(new Guid(Request.QueryString["deletepost"]));
post.Delete();
post.Save();
Response.Redirect("~/");
}
}
}
}
...{
private string _Theme = BlogSettings.Instance.Theme;
/**//// <summary>
/// Assignes the selected theme to the pages.
/// </summary>
protected override void OnPreInit(EventArgs e)
...{
if (Request.QueryString["theme"] != null)
_Theme = Request.QueryString["theme"];
MasterPageFile = "~/themes/" + _Theme + "/site.master";
base.OnPreInit(e);
if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["deletepost"]))
...{
if (Page.User.Identity.IsAuthenticated)
...{
Post post = BlogEngine.Core.Post.GetPost(new Guid(Request.QueryString["deletepost"]));
post.Delete();
post.Save();
Response.Redirect("~/");
}
}
}
}
BlogSettings中使用单件模式:
public static BlogSettings Instance
...{
get
...{
if (blogSettingsSingleton == null)
...{
blogSettingsSingleton = new BlogSettings();
}
return blogSettingsSingleton;
}
}
...{
get
...{
if (blogSettingsSingleton == null)
...{
blogSettingsSingleton = new BlogSettings();
}
return blogSettingsSingleton;
}
}
在BlogSettings的私有构造函数中调用了 Load();方法,这个方法使用BlogService类获得配置信息,使用反射技术给BlogSettings实例对象中的属性赋值
private void Load()
...{
Type settingsType = this.GetType();
//------------------------------------------------------------
// Enumerate through individual settings nodes
//------------------------------------------------------------
System.Collections.Specialized.StringDictionary dic = Providers.BlogService.LoadSettings();
foreach (string key in dic.Keys)
...{
//------------------------------------------------------------
// Extract the setting's name/value pair
//------------------------------------------------------------
string name = key;
string value = dic[key];
//------------------------------------------------------------
// Enumerate through public properties of this instance
//------------------------------------------------------------
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
...{
//------------------------------------------------------------
// Determine if configured setting matches current setting based on name
//------------------------------------------------------------
if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
...{
//------------------------------------------------------------
// Attempt to apply configured setting
//------------------------------------------------------------
try
...{
propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
...{
// TODO: Log exception to a common logging framework?
}
break;
}
}
}
}
...{
Type settingsType = this.GetType();
//------------------------------------------------------------
// Enumerate through individual settings nodes
//------------------------------------------------------------
System.Collections.Specialized.StringDictionary dic = Providers.BlogService.LoadSettings();
foreach (string key in dic.Keys)
...{
//------------------------------------------------------------
// Extract the setting's name/value pair
//------------------------------------------------------------
string name = key;
string value = dic[key];
//------------------------------------------------------------
// Enumerate through public properties of this instance
//------------------------------------------------------------
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
...{
//------------------------------------------------------------
// Determine if configured setting matches current setting based on name
//------------------------------------------------------------
if (propertyInformation.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
...{
//------------------------------------------------------------
// Attempt to apply configured setting
//------------------------------------------------------------
try
...{
propertyInformation.SetValue(this, Convert.ChangeType(value, propertyInformation.PropertyType, CultureInfo.CurrentCulture), null);
}
catch
...{
// TODO: Log exception to a common logging framework?
}
break;
}
}
}
}
BlogService通过读取webconfig中的section同时利用反射技术获得provider的具体类
private static void LoadProviders()
...{
// Avoid claiming lock if providers are already loaded
if (_provider == null)
...{
lock (_lock)
...{
// Do this again to make sure _provider is still null
if (_provider == null)
...{
// Get a reference to the <blogProvider> section
BlogProviderSection section = (BlogProviderSection)WebConfigurationManager.GetSection("BlogEngine/blogProvider");
// Load registered providers and point _provider
// to the default provider
_providers = new BlogProviderCollection();
ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(BlogProvider));
_provider = _providers[section.DefaultProvider];
if (_provider == null)
throw new ProviderException("Unable to load default BlogProvider");
}
}
}
}
...{
// Avoid claiming lock if providers are already loaded
if (_provider == null)
...{
lock (_lock)
...{
// Do this again to make sure _provider is still null
if (_provider == null)
...{
// Get a reference to the <blogProvider> section
BlogProviderSection section = (BlogProviderSection)WebConfigurationManager.GetSection("BlogEngine/blogProvider");
// Load registered providers and point _provider
// to the default provider
_providers = new BlogProviderCollection();
ProvidersHelper.InstantiateProviders(section.Providers, _providers, typeof(BlogProvider));
_provider = _providers[section.DefaultProvider];
if (_provider == null)
throw new ProviderException("Unable to load default BlogProvider");
}
}
}
}
webconfig中的相关信息为
<configSections>
<sectionGroup name="BlogEngine">
<section name="blogProvider" requirePermission="false" type="BlogEngine.Core.Providers.BlogProviderSection, BlogEngine.Core" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/>
</sectionGroup>
</configSections>
<BlogEngine>
<blogProvider defaultProvider="XmlBlogProvider">
<providers>
<add name="XmlBlogProvider" type="BlogEngine.Core.Providers.XmlBlogProvider"/>
<add name="MSSQLBlogProvider" type="BlogEngine.Core.Providers.MSSQLBlogProvider"/>
</providers>
</blogProvider>
</BlogEngine>
<sectionGroup name="BlogEngine">
<section name="blogProvider" requirePermission="false" type="BlogEngine.Core.Providers.BlogProviderSection, BlogEngine.Core" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/>
</sectionGroup>
</configSections>
<BlogEngine>
<blogProvider defaultProvider="XmlBlogProvider">
<providers>
<add name="XmlBlogProvider" type="BlogEngine.Core.Providers.XmlBlogProvider"/>
<add name="MSSQLBlogProvider" type="BlogEngine.Core.Providers.MSSQLBlogProvider"/>
</providers>
</blogProvider>
</BlogEngine>
- BlogEngine.NET (一)配置
- BlogEngine.NET 1.1 发布
- WordPress: Akismet in BlogEngine.NET
- 配置BlogEngine的问题总结
- 一个不错的blog源码BlogEngine.NET
- BlogEngine.NET 源码点滴(1)
- BlogEngine.NET 源码点滴(2)
- WordPress:Akismet in BlogEngine.NET 2
- WordPress:Akismet ----Feel the Power of BlogEngine.NET Spam Filtering
- BlogEngine.Net架构与源代码分析系列part1:开篇介绍
- BlogEngine.Net架构与源代码分析系列part4:Blog全局设置——BlogSettings
- BlogEngine.Net架构与源代码分析系列part5:对象搜索——IPublishable与Search
- 配置BlogEngine容易出错的地方及解决办法——HTTP 错误 404.2 - Not Found
- BlogEngine.Net架构与源代码分析系列part2:业务对象——共同的父类BusinessBase
- BlogEngine.Net架构与源代码分析系列part2:业务对象——共同的父类BusinessBase
- BlogEngine.Net架构与源代码分析系列part3:数据存储——基于Provider模式的实现
- blogengine.com 开发者博客
- ibatis.net xml配置sql使用一
- ATL是通过接口映射表来实现QueryInterface
- SQL一些简单函数的总结
- QQ 静态截图程序模拟实现
- xml javascript 常用地址
- C#生成安装文件后自动附加数据库的思路跟算法
- BlogEngine.NET (一)配置
- 联想控股李嘉诚等联合收购NBA中国11%股份
- 根据用户是否使用来保持Session对象的有效性
- VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)
- 如何调试驱动程序
- xml文件不释放的问题
- 利用Repeater控件显示主-从关系数据表
- Windows XP Embedded (XPE)开发工具、升级包、中文包等微软官方下载地址
- VB.NET 进度条与状态条的使用