目前流行的asp.net架構很多,有開源的有模式與實踐(Microsoft patterns & practices)小組的開源項目Web Service
Factory,Nhibernet, Nbear ORM, Petshop等架構; 下面我又介紹另一種基於中繼資料(XML)架構,在ASP.net2.0的程
序應用,而且這種架構目前很多 IT公司使 用較少,它的特點靈活度較高, 簡單高效,方便的IOC依賴注入,對象 間
解偶性好,開發效率較高,可以結合微軟企業庫進行 高效率的儲存。 我在微軟互聯星空項目中,微軟有很好的成功案例。
總體思想是採用XML(Template模板)進行許可權控制,參考:
首先在Global.asax設定全域對象,系統啟動後會把相關持久對象裝入記憶體,提高系統運行速度:
相關代碼如下:
<%@ Application Language="C#" %>
<%@ Import Namespace="Microsoft.XXXXX.Advertising.SystemFrameWork" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
SystemVM VM = new SystemVM();
Application.Lock();
Application["VM"] = VM;
Application.UnLock();
}
void Application_End(object sender, EventArgs e)
{
// 在應用程式關閉時啟動並執行代碼
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時啟動並執行代碼
}
void Session_Start(object sender, EventArgs e)
{
// 在新會話啟動時啟動並執行代碼
}
void Session_End(object sender, EventArgs e)
{
// 在會話結束時啟動並執行代碼。
// 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為
// InProc 時,才會引發 Session_End 事件。如果會話模式設定為 StateServer
// 或 SQLServer,則不會引發該事件。
}
</script>
第二,建立SystemVM.cs驅動模型類,包括一些相關屬性和裝載相關Template模板:
部分邏輯流程如下:
簡略代碼如下:
public class SystemVM
{
//角色模板
private SystemService m_system = null;
private OrderService m_order = null;
private QueryService m_query = null;
private FoundationService m_found = null;
/// <summary>
///
/// </summary>
public XmlNode XmlDimension
{
get
{
if (HttpContext.Current.Cache["dimension"] == null)
{
LoadXmlDimension();
}
return (XmlNode)HttpContext.Current.Cache["dimension"];
}
}
/// <summary>
/// 查詢範本
/// </summary>
public XmlNode XmlQueryList
{
get
{
if (HttpContext.Current.Cache["querylist"] == null)
{
LoadXmlQueryList();
}
return (XmlNode)HttpContext.Current.Cache["querylist"];
}
}
/// <summary>
/// JavaScript模板
/// </summary>
public XmlNode XmlJavaScript
{
get
{
if (HttpContext.Current.Cache["javascript"] == null)
{
LoadXmlJavaScript();
}
return (XmlNode)HttpContext.Current.Cache["javascript"];
}
}
/// <summary>
/// 省分模板
/// </summary>
public XmlNode XmlProvince
{
get
{
if (HttpContext.Current.Cache["province"] == null)
{
LoadXmlProvince();
}
return (XmlNode)HttpContext.Current.Cache["province"];
}
}
#region 多角色模板
/// <summary>
/// 系統角色模板
/// </summary>
public XmlNode XmlTemplate
{
get
{
XmlNode xmlNode = null;
if (HttpContext.Current.Request.Cookies["user"] != null)
{
string sWebSiteType = HttpContext.Current.Request.Cookies["user"]["WebSiteType"];
if (sWebSiteType == "0")
{
xmlNode = XmlCenterTemplate;
}
if (sWebSiteType == "1")
{
xmlNode = XmlProvinceTemplate;
}
if (sWebSiteType == "2")
{
xmlNode = XmlCityTemplate;
}
}
return xmlNode;
}
}
public XmlNode XmlCenterTemplate
{
get
{
if (HttpContext.Current.Cache["CenterTemplate"] == null)
{
LoadXmlCenterTemplate();
}
return (XmlNode)HttpContext.Current.Cache["CenterTemplate"];
}
}
public XmlNode XmlProvinceTemplate
{
get
{
if (HttpContext.Current.Cache["ProvinceTemplate"] == null)
{
LoadXmlProvinceTemplate();
}
return (XmlNode)HttpContext.Current.Cache["ProvinceTemplate"];
}
}
public XmlNode XmlCityTemplate
{
get
{
if (HttpContext.Current.Cache["CityTemplate"] == null)
{
LoadXmlCityTemplate();
}
return (XmlNode)HttpContext.Current.Cache["CityTemplate"];
}
}
/// <summary>
/// 加栽角色權限範本
/// </summary>
private void LoadXmlCenterTemplate()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "CenterTemplate.Config";
doc.Load(sPath);
XmlNode xmlNode = doc.DocumentElement;
HttpContext.Current.Cache.Insert("CenterTemplate", doc.DocumentElement,
new CacheDependency(sPath));
}
private void LoadXmlProvinceTemplate()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "ProvinceTemplate.Config";
doc.Load(sPath);
XmlNode xmlNode = doc.DocumentElement;
HttpContext.Current.Cache.Insert("ProvinceTemplate", doc.DocumentElement,
new CacheDependency(sPath));
}
private void LoadXmlCityTemplate()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "CityCenterTemplate.Config";
doc.Load(sPath);
XmlNode xmlNode = doc.DocumentElement;
HttpContext.Current.Cache.Insert("CityTemplate", doc.DocumentElement,
new CacheDependency(sPath));
}
#endregion
/// <summary>
/// 系統服務
/// </summary>
public SystemService SystemService
{
get
{
return m_system;
}
}
/// <summary>
/// 查詢服務
/// </summary>
public QueryService QueryService
{
get
{
return m_query;
}
}
/// <summary>
/// 基礎資訊
/// </summary>
public FoundationService FoundationService
{
get
{
return m_found;
}
}
/// <summary>
/// 訂單管理
/// </summary>
public OrderService OrderService
{
get
{
return m_order;
}
}
public SystemVM()
{
LoadBusinessObj();
}
/// <summary>
/// 根據模板類型,返回系統可以選擇的模板種類。
/// </summary>
/// <param name="sType"></param>
/// <returns></returns>
public XmlNodeList GetXmlJavaScriptByType(string sType)
{
XmlNodeList xmlNodeList = XmlJavaScript.SelectNodes("//項[@類型='" + sType + "']");
if (xmlNodeList.Count<0)
{
throw new Exception("找不到類型為["+sType+"]的節點!");
}
return xmlNodeList;
}
/// <summary>
/// 根據模板名稱,擷取JavaScript模板
/// </summary>
/// <param name="sName"></param>
/// <returns></returns>
public string GetXmlJavaScriptTpl(string sName)
{
StringBuilder sJsTemplate = new StringBuilder("");
XmlNode xmlNode = XmlJavaScript.SelectSingleNode("//項[@名稱='" + sName + "']");
if (xmlNode == null)
{
throw new Exception("找不到名稱為[" + sName + "]的節點!");
}
string sPath = ToolFunc.GetXmlString(xmlNode, "@模版");
sPath = AppDomain.CurrentDomain.BaseDirectory + sPath;
using (TextReader sr = File.OpenText(sPath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
sJsTemplate.AppendLine(line);
}
}
return sJsTemplate.ToString();
}
public string GetUrlFromJavaScript(string sName)
{
string sUrl = "#";
XmlNode xmlNode = XmlJavaScript.SelectSingleNode("//項[@名稱='" + sName + "']");
if (xmlNode == null)
{
throw new Exception("找不到名稱為[" + sName + "]的節點!");
}
XmlElement el = (XmlElement)xmlNode;
sUrl = el.GetAttribute("Url");
return sUrl;
}
private void LoadXmlProvince()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "Province.Config";
doc.Load(sPath);
XmlNode xmlNode = doc.DocumentElement;
HttpContext.Current.Cache.Insert("province", doc.DocumentElement,
new CacheDependency(sPath));
}
private void LoadXmlJavaScript()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "JavaScript.Config";
doc.Load(sPath);
HttpContext.Current.Cache.Insert("javascript", doc.DocumentElement,
new CacheDependency(sPath));
}
private void LoadXmlDimension()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "Dimension.Config";
doc.Load(sPath);
HttpContext.Current.Cache.Insert("dimension", doc.DocumentElement,
new CacheDependency(sPath));
}
/// <summary>
///
/// </summary>
private void LoadXmlQueryList()
{
string strBasePath = AppDomain.CurrentDomain.BaseDirectory;
XmlDocument doc = new XmlDocument();
string sPath = strBasePath + @"Template\" + "QueryList.config";
doc.Load(sPath);
HttpContext.Current.Cache.Insert("querylist", doc.DocumentElement,
new CacheDependency(sPath));
}
/// <summary>
/// 載入業務對象
/// </summary>
private void LoadBusinessObj()
{
m_system = new SystemService(this);
m_order = new OrderService(this);
m_found = new FoundationService(this);
m_query = new QueryService(this);
}
此時可以根據資料庫的角色記錄,通過中繼資料模型進行許可權與模板匹配,從而達到非常棒的架構設計。