CodeSmith模板(生成实体类)

CodeSmith是一款与数据库相关的工具,只要与数据库相关的类都可以通过它编写模板来批量实现。

<%@ Template Language="C#" TargetLanguage="C#" %>
<%@ Assembly Name="SchemaExplorer"%>
<%@ Import Namespace="SchemaExplorer"%>
<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema"%>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
   public class <%=GetClassName()%>
                                                                                                                                               
    {
         <%foreach(ColumnSchema column in this.SourceTable.Columns)%><%{%>
    <%=GetCSDataType(column)%> <%=ToCamel(column.Name)%>;
        public <%=GetCSDataType(column)%> <%=ToPascal(column.Name)%>
        {
            get { return <%=ToCamel(column.Name)%>; }
            set { <%=ToCamel(column.Name)%> = value; }
        }
        <%}%>
    }
}
<script runat="template">
//Pascal命名法(将首字母大写)
public string ToPascal(string s)
{
    return s.Substring(0,1).ToUpper()+s.Substring(1);
}
//骆驼命名法(将首字母小写)
public string ToCamel(string s)
{
    return s.Substring(0,1).ToLower()+s.Substring(1);
}
//得到类的名字(由表名而来)
public string GetClassName()
{
    string s=this.SourceTable.Name;//取到表名
    //判断表名是不是以S结尾,如果是去掉S
    if (s.EndsWith("s"))
        {
             return ToPascal(s.Substring(0,s.Length-1));
        }
        return ToPascal(s);
}
//得到C#的数据类型(将基本常用的数据类型意逐个转换)
public static string GetCSDataType(ColumnSchema column)
        {
            if (column.Name.EndsWith("TypeCode")) return column.Name;
            switch (column.DataType)
            {
                case DbType.AnsiString: return "string";
                case DbType.AnsiStringFixedLength: return "string";
                case DbType.Binary: return "byte[]";
                case DbType.Boolean: return "bool";
                case DbType.Byte: return "byte";
                case DbType.Currency: return "decimal";
                case DbType.Date: return "DateTime";
                case DbType.DateTime: return "DateTime";
                case DbType.Decimal: return "decimal";
                case DbType.Double: return "double";
                case DbType.Guid: return "Guid";
                case DbType.Int16: return "short";
                case DbType.Int32: return "int";
                case DbType.Int64: return "long";
                case DbType.Object: return "object";
                case DbType.SByte: return "sbyte";
                case DbType.Single: return "float";
                case DbType.String: return "string";
                case DbType.StringFixedLength: return "string";
                case DbType.Time: return "TimeSpan";
                case DbType.UInt16: return "ushort";
                case DbType.UInt32: return "uint";
                case DbType.UInt64: return "ulong";
                case DbType.VarNumeric: return "decimal";
                default:
                    {
                        return "__UNKNOWN__" + column.NativeType;
                    }
            }
        }
        public override string GetFileName()
        {
            return this.GetClassName()+".cs";
        }
</script>

这是一个为指定的某一张表生成实体类的模板。

在编译运行之后就有以下结果:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
   public class StudentExamQuestionAnswer
                                                                                                                                  
    {
    int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
    int examId;
        public int ExamId
        {
            get { return examId; }
            set { examId = value; }
        }
    int studentId;
        public int StudentId
        {
            get { return studentId; }
            set { studentId = value; }
        }
    int questionId;
        public int QuestionId
        {
            get { return questionId; }
            set { questionId = value; }
        }
    string answer;
        public string Answer
        {
            get { return answer; }
            set { answer = value; }
        }
    int examQuestionId;
        public int ExamQuestionId
        {
            get { return examQuestionId; }
            set { examQuestionId = value; }
        }
    int examStudentId;
        public int ExamStudentId
        {
            get { return examStudentId; }
            set { examStudentId = value; }
        }
    }
}

跟我们平时一句一句的写代码是一模一样。怎么样?批量生成不错吧!还有更美的呢!

j_0047.gifAjax的姑娘,加油!j_0047.gif

本文出自 “Ajax的姑娘” 博客,谢绝转载!

你可能感兴趣的:(数据库,实体类,自动生成,codesmith)