基于元数据驱动模型架构在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 ,如需转载请自行联系原作者

相关文章
|
22天前
|
机器学习/深度学习 自然语言处理 并行计算
大模型开发:什么是Transformer架构及其重要性?
Transformer模型革新了NLP,以其高效的并行计算和自注意力机制解决了长距离依赖问题。从机器翻译到各种NLP任务,Transformer展现出卓越性能,其编码器-解码器结构结合自注意力层和前馈网络,实现高效训练。此架构已成为领域内重要里程碑。
24 2
|
1月前
|
消息中间件 Cloud Native Java
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
【Spring云原生系列】SpringBoot+Spring Cloud Stream:消息驱动架构(MDA)解析,实现异步处理与解耦合
|
1天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
27天前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
771 0
|
29天前
|
设计模式 前端开发 数据处理
MVC架构中,控制器和模型之间是如何交互的
MVC架构中,控制器和模型之间是如何交互的
9 0
|
29天前
|
存储 设计模式 前端开发
请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
【2月更文挑战第26天】【2月更文挑战第89篇】请解释 Web 应用程序的 MVC(模型-视图-控制器)架构。
|
1月前
|
机器学习/深度学习 人工智能 缓存
Griffin模型的主要架构和特点
【2月更文挑战第16天】Griffin模型的主要架构和特点
73 2
Griffin模型的主要架构和特点
|
2月前
|
机器学习/深度学习 人工智能 自然语言处理
Stable Diffusion 3深夜横空出世!模型与Sora同架构
【2月更文挑战第4天】Stable Diffusion 3深夜横空出世!模型与Sora同架构
36 4
Stable Diffusion 3深夜横空出世!模型与Sora同架构
|
2月前
|
存储 缓存 并行计算
DP读书:鲲鹏处理器 架构与编程(四)内存顺序模型与内存屏障
DP读书:鲲鹏处理器 架构与编程(四)内存顺序模型与内存屏障
35 1
|
3月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
38 0