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

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



点击(此处)折叠或打开

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Xml;
  8. using NFinal.Compile;

  9. //防止.net 2.0下类自动引入Linq命名空间报错.
  10. namespace System.Linq
  11. {
  12.     delegate void None();
  13. }
  14. namespace NFinal
  15. {
  16.     public class Frame
  17.     {
  18.         public static string appRoot;
  19.         public static string AssemblyTitle;
  20.        
  21.         public static List<NFinal.DB.ConnectionString> ConnectionStrings = new List<NFinal.DB.ConnectionString>();

  22.         public Frame(string appRoot)
  23.         {
  24.             Frame.appRoot = appRoot;
  25.             string[] fileNames = Directory.GetFiles(appRoot, "*.csproj");
  26.             if (fileNames.Length > 0)
  27.             {
  28.                 AssemblyTitle = Path.GetFileNameWithoutExtension(fileNames[0]);
  29.             }
  30.             else
  31.             {
  32.                 string temp;
  33.                 temp = appRoot.Trim('\\');
  34.                 AssemblyTitle = temp.Substring(temp.LastIndexOf('\\') + 1);
  35.             }
  36.         }

  37.         /// <summary>
  38.         /// 把基于网站根目录的绝对路径改为相对路径
  39.         /// </summary>
  40.         /// <param name="path"></param>
  41.         /// <returns></returns>
  42.         public static string MapPath(string url)
  43.         {
  44.             return appRoot + url.Trim('/').Replace('/', '\\');
  45.         }

  46.         public void GetDB()
  47.         {
  48.             //获取WebConfig中的连接字符串信息
  49.             string configFileName = appRoot + "Web.config";
  50.             if (File.Exists(configFileName))
  51.             {
  52.                 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  53.                 doc.Load(configFileName);
  54.                 System.Xml.XmlNode root = doc.DocumentElement.SelectSingleNode("/configuration");
  55.                 System.Xml.XmlNodeList nodeList = root.SelectNodes("connectionStrings/add[@connectionString]");
  56.                 Frame.ConnectionStrings.Clear();
  57.                 if (nodeList.Count > 0)
  58.                 {
  59.                     System.Xml.XmlElement ele = null;
  60.                     foreach (System.Xml.XmlNode node in nodeList)
  61.                     {
  62.                         ele = node as System.Xml.XmlElement;
  63.                         var connectionString = new NFinal.DB.ConnectionString();
  64.                         connectionString.name = ele.Attributes["name"].Value;
  65.                         connectionString.value = ele.Attributes["connectionString"].Value;
  66.                         connectionString.provider = ele.Attributes["providerName"].Value;
  67.               
  68.                         if (connectionString.provider.ToLower().IndexOf("mysql") > -1)
  69.                         {
  70.                             connectionString.type = NFinal.DB.DBType.MySql;
  71.                         }
  72.                         else if (connectionString.provider.ToLower().IndexOf("sqlclient") > -1)
  73.                         {
  74.                             connectionString.type = NFinal.DB.DBType.SqlServer;
  75.                         }
  76.                         else if (connectionString.provider.ToLower().IndexOf("sqlite") > -1)
  77.                         {
  78.                             connectionString.type = NFinal.DB.DBType.Sqlite;
  79.                         }
  80.                         else if (connectionString.provider.ToLower().IndexOf("oracle") > -1)
  81.                         {
  82.                             connectionString.type = NFinal.DB.DBType.Oracle;
  83.                         }
  84.                         else
  85.                         {
  86.                             connectionString.type = NFinal.DB.DBType.Unknown;
  87.                         }
  88.                         Frame.ConnectionStrings.Add(connectionString);
  89.                     }
  90.                 }
  91.             }
  92.             //读取数据库信息
  93.             NFinal.DB.Coding.DataUtility dataUtility = null;
  94.             if (Frame.ConnectionStrings.Count > 0)
  95.             {
  96.                 NFinal.DB.ConnectionString conStr;
  97.                 NFinal.DB.Coding.DB.DbStore.Clear();
  98.                 for (int i = 0; i < Frame.ConnectionStrings.Count; i++)
  99.                 {
  100.                     conStr = Frame.ConnectionStrings[i];
  101.                     if (conStr.type == NFinal.DB.DBType.MySql)
  102.                     {
  103.                         dataUtility = new NFinal.DB.Coding.MySQLDataUtility(conStr.value);
  104.                         dataUtility.GetAllTables(dataUtility.con.Database);
  105.                         NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
  106.                     }
  107.                     else if (conStr.type == NFinal.DB.DBType.Sqlite)
  108.                     {
  109.                         dataUtility = new NFinal.DB.Coding.SQLiteDataUtility(conStr.value);
  110.                         dataUtility.GetAllTables(dataUtility.con.Database);
  111.                         NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
  112.                     }
  113.                     else if (conStr.type == NFinal.DB.DBType.SqlServer)
  114.                     {
  115.                         dataUtility = new NFinal.DB.Coding.SQLDataUtility(conStr.value);
  116.                         dataUtility.GetAllTables(dataUtility.con.Database);
  117.                         NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
  118.                     }
  119.                     else if (conStr.type == NFinal.DB.DBType.Oracle)
  120.                     {
  121. #if NET2
  122. #else
  123.                         dataUtility = new NFinal.DB.Coding.OracleDataUtility(conStr.value);
  124.                         dataUtility.GetAllTables(dataUtility.con.Database);
  125.                         NFinal.DB.Coding.DB.DbStore.Add(conStr.name, dataUtility);
  126. #endif
  127.                     }
  128.                 }
  129.             }
  130.         }

  131.         /// <summary>
  132.         /// 创建主路由
  133.         /// </summary>
  134.         /// <param name="apps"></param>
  135.         public void CreateMain(string[] apps)
  136.         {
  137.             VTemplate.Engine.TemplateDocument doc = null;
  138.             doc=new VTemplate.Engine.TemplateDocument(MapPath("NFinal/Template/Main.tpl"), System.Text.Encoding.UTF8);
  139.             doc.SetValue("project", NFinal.Frame.AssemblyTitle);
  140.             doc.SetValue("apps", apps);
  141.             doc.SetValue("ControllerSuffix", "Controller");
  142.             doc.RenderTo(MapPath("NFinal/Main.cs"), System.Text.Encoding.UTF8);
  143.         }

  144.         /// <summary>
  145.         /// 获取所有的应用模块
  146.         /// </summary>
  147.         /// <returns></returns>
  148.         public string[] GetApps()
  149.         {
  150.             string webConfigFileName= MapPath("/Web.config");
  151.             XmlDocument doc = new XmlDocument();
  152.             doc.Load(webConfigFileName);
  153.             XmlNode appsNode = doc.DocumentElement.SelectSingleNode("appSettings/add[@key='Apps']");
  154.             string[] Apps = null;

  155.             if (appsNode != null && appsNode.Attributes.Count > 0 && appsNode.Attributes["value"] != null)
  156.             {
  157.                 Apps = appsNode.Attributes["value"].Value.Split(',');
  158.             }
  159.             else
  160.             {
  161.                 Apps =new string[] {"App"};
  162.             }
  163.             return Apps;
  164.         }
  165.         
  166.     }

  167. }

09-13 00:20