衣服染上洗衣液的颜色:代码自动生成工具MyGeneration之二

来源:百度文库 编辑:中财网 时间:2024/04/30 08:52:44

代码自动生成工具MyGeneration之二

分类: C# 2008-09-03 18:49 4613人阅读 评论(2) 收藏 举报

  前面一篇文章讲了如何使用MyGeneration自带的模板来生成代码,现在开始讲如何自己写模板吧。

  请先阅读我的前一篇MyGeneration的文章,在阅读本文,地址如下:

  代码自动生成工具MyGeneration之一

  要用MyGeneration就必须要和各种模板打交道。我们可以使用别人写的模板来完成我们说需要的功能,但是别人写的模板不一定最适合我们的项目里面的代码,所以有时也需要自己写一些模板来使用。下面就讲如何编写模板吧
通过File – New 菜单我们可以看到,MyGeneration支持的模板包括C#,VB.Net,Jscript,VBScript,我们可以选则自己擅长的语言来写模板。
最简单的办法就是找一个功能差不多的模板,然后在这个模板的基础上进行修改了。这个也是初学的办法,可以快速的了解如何实现特定的功能。

  当然,我们要自己建一个模板,以C#模板为例吧。
  假如我们要自己生成一个数据库的数据表所对应的BLL类。
  在UI界面上需要我们选择是哪个数据库,那张数据表。最后生成BLL类。

  选择菜单 File – New – C# Zeus Template,就打开了一个新的工作区,该模板的代码是C#的。工作区有5个Tab页,Template Code, Interface Code, Template Source, Interface Source, Output.如下图所示:

  

  这些都干什么的呢?
  这得了解一下MyGeneration内部的结构了。MyGeneration通过脚本引擎执行Template Code和Interface Code中的脚本,生成Template Source和Interface Source的代码,然后执行Interface Source显示用户界面,再执行Template Source,输出结果到Output中。
  由此可见,Template Source是根据Template code生成的Interface Source是根据Interface code生成的,他们是不可编辑的。我们能用代码控制的就是Template code和Interface code了。而Interface code是主要和用户界面有关的,Template code则主要控制执行完用户界面的代码后如何输出到Output。

  默认生成的Interface Code如下所示:

  1. public class GeneratedGui : DotNetScriptGui
  2. {
  3.     public GeneratedGui(ZeusContext context) : base(context) {}
  4.     //-----------------------------------------
  5.     // The User Interface Entry Point
  6.     //-----------------------------------------
  7.     public override void Setup()
  8.     {
  9.         // ** UNCOMMENT CODE BELOW TO SEE UI **
  10.         //ui.Width  = 100;
  11.         //ui.Height = 100;
  12.         //GuiLabel lblDemo = ui.AddLabel("lblDemo", "Demo", "Demo Tooltip");
  13.         //ui.ShowGui = true;
  14.     }
  15. }

  Interface Code中编写用户界面有两种方法,一种是采用MyGeneration提供的GUI库,另外一种则是完全采用C#本身的界面代码编写。下图显示了MyGeneration的GUI库的帮助,从中可以看到其提供的各种GUI类。



  我可不想为了写个模板去学习使用MyGeneration的GUI库,这可加大了学习成本,而且所有界面都得用代码一行一行的写出来,多麻烦啊。如果能像C#那样,直接用鼠标拖拽控件就能搞定界面UI就好了。
其实,我们是可以用C#的界面库的。我的方法是这样的。先自己用VS新建一个Windows的工程,然后在窗体上摆好控件,设置各种控件的属性,并且把需要用的各种控件的事件都让VS的IDE生成好。然后把这个窗体的代码直接拷贝到MyGeneration的Interface code里面去。注意,假设我们的窗体叫Form1,我们需要拷贝Form1.Designer.cs 和Form1.cs两个文件中的代码。

  然后在Interface Code最前面加入下面两行:

  1. <%#REFERENCE System.Windows.Forms.dll, System.Drawing.dll %>
  2. <%#NAMESPACE System.Windows.Forms, System.Drawing %>

  在Setup()函数中写:
  1. Form1 form = new Form1(); 
  2. if (form.ShowDialog() != DialogResult.OK) 
  3. {
  4.     ui.IsCanceled = true;
  5. }

  最后所形成的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(); 
  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.             // 
  80.             // Form1
  81.             // 
  82.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  83.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  84.             this.ClientSize = new System.Drawing.Size(284, 293);
  85.             this.Controls.Add(this.button1);
  86.             this.Controls.Add(this.listBox1);
  87.             this.Controls.Add(this.comboBox1);
  88.             this.MaximizeBox = false;
  89.             this.MinimizeBox = false;
  90.             this.Name = "Form1";
  91.             this.Text = "ZhouKai's BLL Class";
  92.             this.Load += new System.EventHandler(this.Form1_Load);
  93.             this.ResumeLayout(false);
  94.         }
  95.         #endregion
  96.         private System.Windows.Forms.ComboBox comboBox1;
  97.         private System.Windows.Forms.ListBox listBox1;
  98.         private System.Windows.Forms.Button button1;
  99.                 
  100.                 
  101.         public Form1()
  102.         {
  103.             InitializeComponent();
  104.         }
  105.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  106.         {
  107.         }
  108.         private void Form1_Load(object sender, EventArgs e)
  109.         {
  110.         }
  111.     }

  执行一下该模板,看有什么效果吧?是不是看到了如下的UI呢?

  呵呵,这个只有UI,还没有数据,下次该讲如何给这个UI加上他说需要的数据啦。比如如何获得数据库的各个数据表啊之内的,然后如何输出自己说需要BLL代码和存储过程的代码啦。

  呵呵,发现竟然有网站转载我的文章了,心里小小高兴一下,说明写的东西还是有点用的。

  代码自动生成工具MyGeneration之三