• NFinal学习笔记 03—代码生成器


    NFinal代码生成器与其他的代码生成器不太一样,只需要运行模块下的WebComplier.aspx即可生成最终的web层代码。包括数据库的操作,Router类,

    调试文件等。附上一段代码与大家分享

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Xml;
    using NFinal.Compile;
    
    //防止.net 2.0下类自动引入Linq命名空间报错.
    namespace System.Linq
    {
        delegate void None();
    }
    namespace NFinal
    {
        public class Frame
        {
            public static string appRoot;
            public static string AssemblyTitle;
           
            public static List<NFinal.DB.ConnectionString> ConnectionStrings = new List<NFinal.DB.ConnectionString>();
    
            public Frame(string appRoot)
            {
                Frame.appRoot = appRoot;
                string[] fileNames = Directory.GetFiles(appRoot, "*.csproj");
                if (fileNames.Length > 0)
                {
                    AssemblyTitle = Path.GetFileNameWithoutExtension(fileNames[0]);
                }
                else
                {
                    string temp;
                    temp = appRoot.Trim('\');
                    AssemblyTitle = temp.Substring(temp.LastIndexOf('\') + 1);
                }
            }
    
            /// <summary>
            /// 把基于网站根目录的绝对路径改为相对路径
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static string MapPath(string url)
            {
                return appRoot + url.Trim('/').Replace('/', '\');
            }
    
            public void GetDB()
            {
                //获取WebConfig中的连接字符串信息
                string configFileName = appRoot + "Web.config";
                if (File.Exists(configFileName))
                {
                    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                    doc.Load(configFileName);
                    System.Xml.XmlNode root = doc.DocumentElement.SelectSingleNode("/configuration");
                    System.Xml.XmlNodeList nodeList = root.SelectNodes("connectionStrings/add[@connectionString]");
                    Frame.ConnectionStrings.Clear();
                    if (nodeList.Count > 0)
                    {
                        System.Xml.XmlElement ele = null;
                        foreach (System.Xml.XmlNode node in nodeList)
                        {
                            ele = node as System.Xml.XmlElement;
                            var connectionString = new NFinal.DB.ConnectionString();
                            connectionString.name = ele.Attributes["name"].Value;
                            connectionString.value = ele.Attributes["connectionString"].Value;
                            connectionString.provider = ele.Attributes["providerName"].Value;
                  
                            if (connectionString.provider.ToLower().IndexOf("mysql") > -1)
                            {
                                connectionString.type = NFinal.DB.DBType.MySql;
                            }
                            else if (connectionString.provider.ToLower().IndexOf("sqlclient") > -1)
                            {
                                connectionString.type = NFinal.DB.DBType.SqlServer;
                            }
                            else if (connectionString.provider.ToLower().IndexOf("sqlite") > -1)
                            {
                                connectionString.type = NFinal.DB.DBType.Sqlite;
                            }
                            else if (connectionString.provider.ToLower().IndexOf("oracle") > -1)
                            {
                                connectionString.type = NFinal.DB.DBType.Oracle;
                            }
                            else
                            {
                                connectionString.type = NFinal.DB.DBType.Unknown;
                            }
                            Frame.ConnectionStrings.Add(connectionString);
                        }
                    }
                }
                //读取数据库信息
                NFinal.DB.Coding.DataUtility dataUtility = null;
                if (Frame.ConnectionStrings.Count > 0)
                {
                    NFinal.DB.ConnectionString conStr;
                    NFinal.DB.Coding.DB.DbStore.Clear();
                    for (int i = 0; i < Frame.ConnectionStrings.Count; i++)
                    {
                        conStr = Frame.ConnectionStrings[i];
                        if (conStr.type == NFinal.DB.DBType.MySql)
                        {
                            dataUtility = new NFinal.DB.Coding.MySQLDataUtility(conStr.value);
                            dataUtility.GetAllTables(dataUtility.con.Database);
                            NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                        }
                        else if (conStr.type == NFinal.DB.DBType.Sqlite)
                        {
                            dataUtility = new NFinal.DB.Coding.SQLiteDataUtility(conStr.value);
                            dataUtility.GetAllTables(dataUtility.con.Database);
                            NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                        }
                        else if (conStr.type == NFinal.DB.DBType.SqlServer)
                        {
                            dataUtility = new NFinal.DB.Coding.SQLDataUtility(conStr.value);
                            dataUtility.GetAllTables(dataUtility.con.Database);
                            NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
                        }
                        else if (conStr.type == NFinal.DB.DBType.Oracle)
                        {
    #if NET2
    #else
                            dataUtility = new NFinal.DB.Coding.OracleDataUtility(conStr.value);
                            dataUtility.GetAllTables(dataUtility.con.Database);
                            NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
    #endif
                        }
                    }
                }
            }
    
            /// <summary>
            /// 创建主路由
            /// </summary>
            /// <param name="apps"></param>
            public void CreateMain(string[] apps)
            {
                VTemplate.Engine.TemplateDocument doc = null;
                doc=new VTemplate.Engine.TemplateDocument(MapPath("NFinal/Template/Main.tpl"), System.Text.Encoding.UTF8);
                doc.SetValue("project", NFinal.Frame.AssemblyTitle);
                doc.SetValue("apps", apps);
                doc.SetValue("ControllerSuffix", "Controller");
                doc.RenderTo(MapPath("NFinal/Main.cs"), System.Text.Encoding.UTF8);
            }
    
            /// <summary>
            /// 获取所有的应用模块
            /// </summary>
            /// <returns></returns>
            public string[] GetApps()
            {
                string webConfigFileName= MapPath("/Web.config");
                XmlDocument doc = new XmlDocument();
                doc.Load(webConfigFileName);
                XmlNode appsNode = doc.DocumentElement.SelectSingleNode("appSettings/add[@key='Apps']");
                string[] Apps = null;
    
                if (appsNode != null && appsNode.Attributes.Count > 0 && appsNode.Attributes["value"] != null)
                {
                    Apps = appsNode.Attributes["value"].Value.Split(',');
                }
                else
                {
                    Apps =new string[] {"App"};
                }
                return Apps;
            }
            
        }
    
    }
    View Code
  • 相关阅读:
    VIP邮箱用哪个好?163VIP邮箱怎么绑定到iPhone上?
    电子邮箱系统注册哪家好?163邮箱联系人怎么管理?
    企业邮箱域名是什么?外贸邮箱垃圾邮件怎么避免?
    宅在家里不香嘛?必玩H5休闲游戏推荐合辑
    最安全的邮箱排名是哪个?163VIP邮箱垃圾邮件怎么处理?
    外贸邮件怎么群发?群发邮件的电子邮箱有哪些?
    好用的外贸邮箱有哪些?公司企业邮箱申请哪个好?
    专业外贸企业邮箱怎么使用?外贸邮件开发信怎么写?
    现象看本质!爆款H5小游戏的衍生之路
    Java面试题总结之JDBC 和Hibernate
  • 原文地址:https://www.cnblogs.com/xiaolu6t/p/4619118.html
Copyright © 2020-2023  润新知