首页 > 代码库 > DataUml Design 教程5-代码模板介绍(甚于T4模板技术)

DataUml Design 教程5-代码模板介绍(甚于T4模板技术)

       DataUml Design 代码模板完全基于C#语言来编写的。不懂写模板的可以请教作者,随时欢迎。下面是一段模板代码,这段代码可以获取一个类结构的所有信息。
            <#@ template language="C#" HostSpecific="True" #>
            <
            NetUmlTemplateCodeHost host = (NetUmlTemplateCodeHost)(Host);
            #>
            命名空间:<#= host.ClassProperty.Namespace #>
            类名:<#= host.ClassProperty.ClassName #>
            类中文名:<#= host.ClassProperty.ClassOtherName #>
            表名:<#= host.ClassProperty.TableName #>
            可访问性:<#= host.ClassProperty.Accessibility==null?"":host.ClassProperty.Accessibility #>
            修饰符:<#= host.ClassProperty.Modifier==null?"":host.ClassProperty.Modifier #>
            用户:<#= host.ClassProperty.User==null?"":host.ClassProperty.User #> 
            备注:<#= host.ClassProperty.Remarks #>
            数据库类型:<#= host.ClassProperty.DataAccessType #>
            语言:<#= host.ClassProperty.ProjectLanage #>
            字段:
            <# foreach(var f in host.ClassProperty.Fields)
            {
                WriteLine("    属性名:"+f.PropertyName);
                WriteLine("    属性别名:"+f.PropertyOtherName);
                WriteLine("    属性类型:"+f.DataType);
                WriteLine("    修饰符:"+f.Modifier);
                WriteLine("    可访问性:"+f.Accessibility);
                WriteLine("    是否空类型:"+f.IsNullType);
                WriteLine("    初始值:"+f.InitialValue);
                WriteLine("    是否只读:"+f.IsReadable);
                WriteLine("    是否可写:"+f.IsWritable);
                WriteLine("    字段名:"+f.FieldName);
                WriteLine("    字段类型:"+f.FieldType);
                WriteLine("    长度:"+f.FieldLength);
                WriteLine("    是否为空:"+f.IsNull);
                WriteLine("    是否主键:"+f.IsPrimaryKey);
                if(f.IsPrimaryKey)
                {
                    WriteLine("    主键类型:"+f.PrimaryKeyType);
                }
                WriteLine("    是否外键:"+f.IsForeignKey);
                WriteLine("    默认值:"+f.DefaultValue);
                WriteLine("    备注信息:"+f.Remarks);
                if(f.Attributes.Count>0)
                {
                    WriteLine("    属性:");
                    foreach(var p in f.Attributes)
                    {
                     WriteLine("        属性名:"+p.AttributeName);
                     WriteLine("        属性值:"+p.AttributeValue);
                    }
                }
                WriteLine("    ----------------");
            }
            #>

            类成员:

            表键信息:
            <# foreach(var k in host.ClassProperty.FieldKeyPropertys)
            {
                WriteLine(" 键名:"+k.KeyName);
                WriteLine(" 键类型:"+k.KeyType);
                WriteLine(" 字段:"+k.KeyFieldName);
                WriteLine(" 字段别名:"+k.KeyFieldOtherName);
                if(k.KeyType==KeyType.FOREIGN_KEY)
                {
                      WriteLine(" 约束名:"+k.ConstraintName);
                      WriteLine(" 约束表名:"+k.ConstraintTable);
                      WriteLine(" 约束表别名:"+k.ConstraintTableOtherName);
                      WriteLine(" 约束字段:"+k.ConstraintFields);
                      WriteLine(" 约束字段别名:"+k.ConstraintFieldsOtherName);      
                }
                WriteLine("----------------");
            }
            #>

            关联的类信息:
            <# foreach(RelationClass c in host.ClassProperty.RelationClasss)
            {
                WriteLine(" 关联类名:"+c.ClassProperty.ClassName);
                WriteLine(" 关联名称:"+c.RelationName);
                WriteLine(" 关联类型:"+c.TableRelationType);
                WriteLine(" UML关联类型:"+c.UmlRelationType);
                WriteLine(" IsConnectionLineStart:"+c.IsConnectionLineStart);
                WriteLine(" IsConnectionLineEnd:"+c.IsConnectionLineEnd);
                foreach(var f in c.RelationFields)
                {
                    WriteLine(" 关联字段:"+f[0]+""+"="+f[1]);        
                }
                WriteLine(" 多重性:"+c.Multiplicity[0]+","+c.Multiplicity[1]);
                WriteLine(    "----------------");
            }
            #> 
讲解
            <#@ template language="C#" HostSpecific="True" #>
            <
            NetUmlTemplateCodeHost host = (NetUmlTemplateCodeHost)(Host);
            #>
            这段代码是必须的,它申明模板是采用哪种语言。在模板里可以完全访问NetUmlTemplateCodeHost类里面的信息,这个类里面有个属性ClassProperty,这个属性包括模型中一个类的所有信息。
          < #>所有语法代码都在这个符号里写
          host.ClassProperty.Fields中Fields属性是个集合,它包括所有字段信息
          下面是循环一个类中所有字段 

          <# foreach(var f in host.ClassProperty.Fields)
          {
          }
           #>