翻新手机怎么看:代码自动生成工具MyGeneration之三

来源:百度文库 编辑:中财网 时间:2024/05/04 08:45:43

代码自动生成工具MyGeneration之三

分类: C# 2008-09-10 19:21 2656人阅读 评论(3) 收藏 举报

  接前面的文章继续。

  代码自动生成工具MyGeneration之二

  前面讲了MyGeneration的使用,以及如何自己写模板显示UI,现在开始讲如何将数据库的信息显示到UI上。
在MyGeneraion脚本系统中,有一个全局变量,叫做MyMeta,他是dbRoot类型的。通过这个全局变量,我们可以获得数据库相关的信息。这个变量在Interface Code 和Template Code中都可以使用。
  从上节代码来看,我们建立Form窗口的时候,就把这个变量给传给了Form窗口
  如下代码所示:
  MyForm form = new MyForm(MyMeta, input);
   那么dbRoot类型的MyMeta变量都有哪些功能,有哪些函数,属性可以使用呢?
我们可以查找帮助,通过MyGeneration菜单 “File – Help – MyMeta API Reference”可以打开其帮助,里面有dbRoot的详细介绍,如下图所示:



  大概看一下API,然后我们就可以修改我们的模板了,将其修改如下:

  1.         private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             //获取数据库,传给ComboBox
  4.             comboBox1.DataSource    = this.myMeta.Databases;
  5.             //ComboBox显示数据库名
  6.             this.comboBox1.DisplayMember = "Name";  
  7.             
  8.             if(this.myMeta.DefaultDatabase != null)
  9.             {
  10.                 //选中默认的数据库
  11.                 this.comboBox1.SelectedIndex = this.comboBox1.FindStringExact(this.myMeta.DefaultDatabase.Name);
  12.                 //通过IDatabase 的Tables属性取得数据库里面所有的数据表
  13.                 //作为数据源传给ListBox
  14.                 this.listBox1.DataSource = this.myMeta.DefaultDatabase.Tables;
  15.                 //ListBox显示数据表的名字
  16.                 this.listBox1.DisplayMember = "Name";
  17.             }   
  18.         }
  19.         

  就算没有注释,这代码也很好懂吧。
  呵呵,由此可见,我们可以通过MyMeta得到IDataBases,然后获得ITable,在通过ITable
获得数据表的信息……反正呢,照这条线找下去,基本上数据库里有哪些表,表里有哪些字段,有哪些存储过程,任何信息都可以很容易的得到。当然了,在自己写模板之前,最好先大概看一下“MyMeta API Reference”,这样就写模板就会更得心应手了。
  下面接下来看Form点击OK之后的代码吧

  1.          private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             IDatabase database = this.comboBox1.SelectedValue as IDatabase;
  4.             ITable    table    = this.listBox1.SelectedValue as ITable;
  5.             
  6.             this.zeusInput["databaseName"] = database.Name;
  7.             this.zeusInput["tableName"]    = table.Name;
  8.             
  9.             this.DialogResult = DialogResult.OK;
  10.             this.Close();
  11.         }
  这段代码得重点讲一下了,因为Interface Code是关于UI的,那么UI执行完了之后,我们就需要用到Template Code了,那么Template Code和Interface Code是如何联系在一起的呢? 我们在Template Code里面如何知道Interface Code显示的UI中用户输入了什么,选择了什么?
  这用到了MyGeneration的一个非常重要的类IZeusInput。
  改接口的详细信息请看帮助 “File – help – Zeus API Reference”。
  在这里我们简单的把UI上选中的数据库的名字和数据表的名字存在了IZeusInput变量中。大家注意看下Form的IZeumInput是如何来的吧。
  这样,我们就完成了该模板的Interface Code的代码,最后形成的Interface Code就如下所示:

  1. <%#REFERENCE System.Windows.Forms.dll, System.Drawing.dll %>
  2. <%#NAMESPACE System.Windows.Forms, System.Drawing %>
  3. public class GeneratedGui : DotNetScriptGui
  4. {
  5.     public GeneratedGui(ZeusContext context) : base(context) {}
  6.     //-----------------------------------------
  7.     // The User Interface Entry Point
  8.     //-----------------------------------------
  9.     public override void Setup()
  10.     {
  11.         // ** UNCOMMENT CODE BELOW TO SEE UI **
  12.         //ui.Width  = 100;
  13.         //ui.Height = 100;
  14.         //GuiLabel lblDemo = ui.AddLabel("lblDemo", "Demo", "Demo Tooltip");
  15.         //ui.ShowGui = true;
  16.         Form1 form = new Form1(MyMeta, input); 
  17.         if (form.ShowDialog() != DialogResult.OK) 
  18.         {
  19.             ui.IsCanceled = true;
  20.         }
  21.     }
  22. }
  23.     public class Form1:Form
  24.     {
  25.         /// 
  26.         /// Required designer variable.
  27.         /// 
  28.         private System.ComponentModel.IContainer components = null;
  29.         /// 
  30.         /// Clean up any resources being used.
  31.         /// 
  32.         /// true if managed resources should be disposed; otherwise, false.
  33.         protected override void Dispose(bool disposing)
  34.         {
  35.             if (disposing && (components != null))
  36.             {
  37.                 components.Dispose();
  38.             }
  39.             base.Dispose(disposing);
  40.         }
  41.         #region Windows Form Designer generated code
  42.         /// 
  43.         /// Required method for Designer support - do not modify
  44.         /// the contents of this method with the code editor.
  45.         /// 
  46.         private void InitializeComponent()
  47.         {
  48.             this.comboBox1 = new System.Windows.Forms.ComboBox();
  49.             this.listBox1 = new System.Windows.Forms.ListBox();
  50.             this.button1 = new System.Windows.Forms.Button();
  51.             this.SuspendLayout();
  52.             // 
  53.             // comboBox1
  54.             // 
  55.             this.comboBox1.FormattingEnabled = true;
  56.             this.comboBox1.Location = new System.Drawing.Point(22, 24);
  57.             this.comboBox1.Name = "comboBox1";
  58.             this.comboBox1.Size = new System.Drawing.Size(233, 20);
  59.             this.comboBox1.TabIndex = 0;
  60.             this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
  61.             // 
  62.             // listBox1
  63.             // 
  64.             this.listBox1.FormattingEnabled = true;
  65.             this.listBox1.ItemHeight = 12;
  66.             this.listBox1.Location = new System.Drawing.Point(22, 50);
  67.             this.listBox1.Name = "listBox1";
  68.             this.listBox1.Size = new System.Drawing.Size(233, 196);
  69.             this.listBox1.TabIndex = 1;
  70.             // 
  71.             // button1
  72.             // 
  73.             this.button1.Location = new System.Drawing.Point(180, 252);
  74.             this.button1.Name = "button1";
  75.             this.button1.Size = new System.Drawing.Size(75, 23);
  76.             this.button1.TabIndex = 2;
  77.             this.button1.Text = "OK";
  78.             this.button1.UseVisualStyleBackColor = true;
  79.             this.button1.Click += new System.EventHandler(this.button1_Click);
  80.             // 
  81.             // Form1
  82.             // 
  83.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  84.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  85.             this.ClientSize = new System.Drawing.Size(284, 293);
  86.             this.Controls.Add(this.button1);
  87.             this.Controls.Add(this.listBox1);
  88.             this.Controls.Add(this.comboBox1);
  89.             this.MaximizeBox = false;
  90.             this.MinimizeBox = false;
  91.             this.Name = "Form1";
  92.             this.Text = "ZhouKai's BLL Class";
  93.             this.Load += new System.EventHandler(this.Form1_Load);
  94.             this.ResumeLayout(false);
  95.         }
  96.         #endregion
  97.         private System.Windows.Forms.ComboBox comboBox1;
  98.         private System.Windows.Forms.ListBox listBox1;
  99.         private System.Windows.Forms.Button button1;
  100.                 
  101.                 
  102.         private dbRoot myMeta;
  103.         private IZeusInput zeusInput;
  104.         public Form1(dbRoot myMeta, IZeusInput zeusInput)
  105.         {
  106.             this.myMeta    = myMeta;
  107.             this.zeusInput = zeusInput;
  108.             
  109.             InitializeComponent();
  110.         }   
  111.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  112.         {
  113.             IDatabase database = this.comboBox1.SelectedValue as IDatabase;
  114.             if(database != null)
  115.             {
  116.                 this.listBox1.DataSource = database.Tables;
  117.                 this.listBox1.DisplayMember = "Name";
  118.             }   
  119.         }
  120.         private void Form1_Load(object sender, EventArgs e)
  121.         {
  122.             //获取数据库,传给ComboBox
  123.             comboBox1.DataSource    = this.myMeta.Databases;
  124.             //ComboBox显示数据库名
  125.             this.comboBox1.DisplayMember = "Name";  
  126.             
  127.             if(this.myMeta.DefaultDatabase != null)
  128.             {
  129.                 //选中默认的数据库
  130.                 this.comboBox1.SelectedIndex = this.comboBox1.FindStringExact(this.myMeta.DefaultDatabase.Name);
  131.                 //通过IDatabase 的Tables属性取得数据库里面所有的数据表
  132.                 //作为数据源传给ListBox
  133.                 this.listBox1.DataSource = this.myMeta.DefaultDatabase.Tables;
  134.                 //ListBox显示数据表的名字
  135.                 this.listBox1.DisplayMember = "Name";
  136.             }   
  137.         }
  138.         
  139.          private void button1_Click(object sender, EventArgs e)
  140.         {
  141.             IDatabase database = this.comboBox1.SelectedValue as IDatabase;
  142.             ITable    table    = this.listBox1.SelectedValue as ITable;
  143.             
  144.             this.zeusInput["databaseName"] = database.Name;
  145.             this.zeusInput["tableName"]    = table.Name;
  146.             
  147.             this.DialogResult = DialogResult.OK;
  148.             this.Close();
  149.         }
  150.     }

  下面来看Template Code如何写吧。目前的模板默认的Template Code如下所示:

  1. <%
  2. public class GeneratedTemplate : DotNetScriptTemplate
  3. {
  4.     public GeneratedTemplate(ZeusContext context) : base(context) {}
  5.     //---------------------------------------------------
  6.     // Render() is where you want to write your logic    
  7.     //---------------------------------------------------
  8.     public override void Render()
  9.     {
  10.         %>
  11.         You can toggle in out of script like this
  12.         <%
  13.         output.writeln("Hello world.");
  14.     }
  15. }
  16. %>

  C# Template Code的语法和Asp的语法十分的类似<%=%>表示绑定某一个字段或属性
  <%%>表示脚本段,我们可以在这里写符合C#语法的任何语句
  其他的内容不进行解析直接用output写到最后的结果里

  修改Template Code,改成如下所示:

  1. <%
  2. public class GeneratedTemplate : DotNetScriptTemplate
  3. {
  4.     public GeneratedTemplate(ZeusContext context) : base(context) {}
  5.     public override void Render()
  6.     {
  7.         //设置输出语言...
  8.         MyMeta.Language = "C#";
  9.         MyMeta.DbTarget = "SQLClient";
  10.         //获得执行完UI后,我们说选中的数据库名,数据表名
  11.         string databaseName = input["databaseName"].ToString();
  12.         string tableName    = input["tableName"].ToString();
  13.             
  14.         IDatabase database = MyMeta.Databases[databaseName];
  15.         //获得数据表的接口
  16.         ITable table = database.Tables[tableName];
  17.         
  18.         %>
  19.         
  20. namespace BLL
  21. {
  22.     using System;
  23.     using System.Data;
  24.     
  25.     using System.Collections;
  26.     using System.Collections.Generic;
  27.         
  28. <%      
  29.         //数据表的名称作为BLL类的类名
  30.         output.writeln("    public partial class " + tableName );
  31.         output.writeln("    {   ");
  32.         
  33.         //遍历数据表中所有字段    
  34.         foreach (IColumn column in table.Columns)
  35.         {
  36.             //将字段名第一个字母转为小写
  37.             string tmpColumnName = DnpUtils.SetCamelCase(column.Name);
  38.             output.writeln("        private " + column.LanguageType + " _" + tmpColumnName + ";");
  39.             output.writeln("        public " + column.LanguageType + " " + column.Name);
  40.             output.writeln("        {");
  41.             output.writeln("            get { return _" + tmpColumnName + ";    }");
  42.             output.writeln("            set { _" + tmpColumnName + " = value;   }");
  43.             output.writeln("        }");
  44.             output.writeln("");     
  45.         }   
  46.         output.writeln("    }   ");     
  47.         output.writeln("}");
  48.     }
  49. }
  50. %>
  这代码也非常简单,基本不用解释都能看懂,不用几分钟就能自己写出来了。
  运行一下该模板,看下是不是类似有如下输出呢?
  1. namespace BLL
  2. {
  3.     using System;
  4.     using System.Data;
  5.     
  6.     public partial class User
  7.     {   
  8.         private int _id;
  9.         public int ID
  10.         {
  11.             get { return _id;   }
  12.             set { _id = value;  }
  13.         }
  14.         private string _userName;
  15.         public string UserName
  16.         {
  17.             get { return _userName;     }
  18.             set { _userName = value;    }
  19.         }
  20.     }   
  21. }
  好了,最简单的一个生成BLL类的模板就完成了。
  本来还想写下如何用MyGeneration来生成执行存储过程的函数,如何用MyGeneration来生成测试存储过程函数的代码的。我觉得完成了这个模板,基本上就可以自己查找MyGeneration的帮助中的API完成这些了,应该不用继续下去了。
继续意淫一下,假如写数据库相关的程序,有了MyGeneration,我们的工作就简单多了,我们可以自己写模板,自动生成BLL类,DAL代码,自动生成测试代码……多棒啊