现在的位置: 首页 > 综合 > 正文

代码自动生成工具MyGeneration之三

2013年09月20日 ⁄ 综合 ⁄ 共 4510字 ⁄ 字号 评论关闭

 接前面的文章继续。

  代码自动生成工具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.         /// <summary>
  26.         /// Required designer variable.
  27.         /// </summary>
  28.         private System.ComponentModel.IContainer components = null;
  29.         /// <summary>
  30.         /// Clean up any resources being used.
  31.         /// </summary>
  32.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  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.         /// <summary>
  43.         /// Required method for Designer support - do not modify
  44.         /// the contents of this method with the code editor.
  45.         /// </summary>
  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

抱歉!评论已关闭.