基于元数据驱动模型架构在ASP.Net的应用研究

简介:
目前流行的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);
        }
 
此时可以根据数据库的角色记录,通过元数据模型进行权限与模板匹配,从而达到非常棒的架构设计。
 
 

 

本文转自 

高阳 51CTO博客,原文链接:http://blog.51cto.com/xiaoyinnet/196248 ,如需转载请自行联系原作者

相关文章
|
.NET
走向ASP.NET“.NET研究”架构设计——第六章:服务层设计(中篇)
  Façade设计模式   在SOA客户端的设计中,最常用的模式就是Façade模式了。Façade模式简化了复杂子系统的调用接口,也就说,Façade隐藏了子系统之间的复杂关系,给客户端一个简单的调用接口。
1015 1
|
Web App开发 缓存 负载均衡
大型高性能ASP.NET“.NET研究”系统架构设计
  大型动态应用系统平台主要是针对于大流量、高并发网站建立的底层系统架构。大型网站的运行需要一个可靠、安全、可扩展、易维护的应用系统平台做为支撑,以保证网站应用的平稳运行。    大型动态应用系统又可分为几个子系统: Web前端系统 负载均衡系统 数据库集群系统 缓存系统 分布式存储系统 分布式服...
1867 0
|
.NET 数据库
一起谈.NET技术,走向ASP.NET架构设计——第六章:服务层设计(前篇)
  本篇主要是为后文做铺垫,所以理论的东西相对而言比较的多一点!   服务层的概述   首先解释一下什么是”服务Service”,从广义来讲:只要是你使用了别人的东西,那么你就在使用别人提供的服务。在这里,服务就是指可能被一个或者多个系统使用的核心的业务逻辑,我们可以把服务简单的想象成为一些可供调用的API。
1123 0
|
.NET
一起谈.NET技术,走向ASP.NET架构设计——第六章:服务层设计(中篇)
  Façade设计模式   在SOA客户端的设计中,最常用的模式就是Façade模式了。Façade模式简化了复杂子系统的调用接口,也就说,Façade隐藏了子系统之间的复杂关系,给客户端一个简单的调用接口。
790 0
|
.NET
一起谈.NET技术,走向ASP.NET架构设计——第七章:阶段总结,实践篇(中篇)
  服务层(中篇)   上一篇文章中,我们已经讲述了业务逻辑层和数据访问层层的设计和编码,下面我们就来讲述服务层的设计。如我们之前所讨论的:服务层想客户端暴露简单易用的API.   如下图所示:   在上图中: 1. ASPPatterns.Chap6.EventTickets.Contract: 这个类库中定义了服务层的接口契约。
817 0
|
架构师 .NET 测试技术
一起谈.NET技术,走向ASP.NET架构设计——第一章:走向设计
  前言:很多做开发的人都在不断的摸索着,积极的学习,试图找出一条走向架构设计的成功法则。每当有人问起我们的职业,我们也常常在说:”软件设计”。有时,我就在想:”设计”,这个已经被我们嚼烂了的词,到底有多少人真正懂”设计”的含义。
1282 0
|
.NET
走向ASP.NET架构设计——第七章:阶段总结,“.NET研究”实践篇(上篇)
  示例说明   本篇的例子的是一个在线订票的服务系统。这个系统向外界暴露了一些可以通过Http协议访问的API,在这个订票服务下面允许任意多个隶属机构来使用服务API进行真正的售票活动。如下图所示:   就好比银行外面的那些自动取款机(对应图中的Affiliate A, B, C),可以把它们看成是银行系统的隶属机构,我们就是通过这些取款机来进行存取活动的,其实这些取款机是调用了银行系统的一些服务来进行数据操作,当然我们也可以直接到银行柜台(对应图中的Ticket Shop)去进行存取款操作。
753 0
|
Web App开发 SQL 前端开发
一起谈.NET技术,鲜为人知的ASP.NET MVC 2.0框架高效之谜
  要想建立开发环境,你需要安装Visual Studio 2008/2010 Beta 2,以及SQL Express 2005(可免费从MSDN下载)和MVC 2.0框架。我把本文中的示例Web应用命名为“Employee Master Information”。
987 0
|
.NET
详解ASP.NET页面的asp“.NET研究”x扩展
  需求:某网站因业务扩展,需拆分出另一个站点,新旧站点具有相同的内容,但具体栏目表现形式上不一样。原网站运行多年,有大量的图片,这些图片也会在新站上使用。任务是:   保证两个网站图片内容同步,即原来的站点增加一个图片,新站点即可使用这个图片。
698 0