CodeSmith 使用,模板代码生成

很好用的一款代码生成工具,分享一个我自己用的model代码生成模板

 

<%@ Template Language="C#" TargetLanguage="Text" %>

<%@ Property Name="ClassNamespace" Type="System.String" Default="Z.Shop.Model" %>

<%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Context" Description="Table that the object is based on." %>
<%@ Map Name="CSharpAlias" Src="System-CSharpAlias" Description="System to C# Type Map" %>
<%@ Assembly Name="SchemaExplorer" %>
<%@ Import Namespace="SchemaExplorer" %>

<script runat="template">
// My methods here.
        public string GetTableName(TableSchema source)
        {
            
            return source.Name;
            
        }

        public string GetSqlDbType(DataObjectBase column)
        {

            switch (column.NativeType)

            {

                case "bigint": return "BigInt";

                case "binary": return "Binary";

                case "bit": return "Bit";

                case "char": return "Char";

                case "datetime": return "DateTime";

                case "decimal": return "Decimal";

                case "float": return "Float";

                case "image": return "Image";

                case "int": return "Int";

                case "money": return "Money";

                case "nchar": return "NChar";

                case "ntext": return "NText";

                case "numeric": return "Decimal";

                case "nvarchar": return "NVarChar";

                case "real": return "Real";

                case "smalldatetime": return "SmallDateTime";

                case "smallint": return "SmallInt";

                case "smallmoney": return "SmallMoney";

                case "sql_variant": return "Variant";

                case "sysname": return "NChar";

                case "text": return "Text";

                case "timestamp": return "Timestamp";

                case "tinyint": return "TinyInt";

                case "uniqueidentifier": return "UniqueIdentifier";

                case "varbinary": return "VarBinary";

                case "varchar": return "VarChar";

                default: return "__UNKNOWN__" + column.NativeType;

            }

        }


</script>

using Job.Core;
using Job.Core.Module;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;


namespace <%= this.ClassNamespace %>
{
    
    [Serializable]
    public  class <%= this.SourceTable.Name %> : BusinessSql<int, <%= this.SourceTable.Name %>>
    {
        public <%= this.SourceTable.Name %>()
        {
            PrimaryKey = "Id";
            DataBaseName = DataBaseEnum.JobDataMain;
        }
        
        #region Model
        
        <% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
        
        private <%= CSharpAlias[column.SystemType.FullName] %> _<%= StringUtil.ToCamelCase(column.Name) %>;

        public <%= CSharpAlias[column.SystemType.FullName] %> <%= StringUtil.ToPascalCase(column.Name) %>
        {
            get { return _<%= StringUtil.ToCamelCase(column.Name) %>; }
            set { _<%= StringUtil.ToCamelCase(column.Name) %> = value; }
        }

        <% } %>
        
        
        
        #endregion
        
        #region Methods Helper
        private static List<SqlParameter> _listParms;
        /// <summary>
        /// 获得模型的所有列的 DataParameter  的集合
        /// </summary>
        /// <returns></returns>

        public override List<SqlParameter> GetParmsList()
        {
            if (_listParms == null)
            {
                SqlParameter[] list = new SqlParameter[] {
                    <% foreach (ColumnSchema column in this.SourceTable.Columns) {  %>
                    new SqlParameter("@<%= StringUtil.ToPascalCase(column.Name) %>",
                    SqlDbType.<%= GetSqlDbType(column) %>,<%= column.Size %>,"<%= StringUtil.ToPascalCase(column.Name) %>"),
                    <% } %>                
                    };
                _listParms = list.ToList<SqlParameter>();

            }
            return _listParms;
        }
        #endregion
        
        
        
        
        #region Overrides Methods

        /// <summary>
        /// 
        /// </summary>
        protected override bool DataUpdate()
        {
            return Operation().Update(this);
        }

        /// <summary>
        /// 
        /// </summary>
        protected override bool DataCreate()
        {
            return Operation().Add(this) > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        protected override bool DataDelete()
        {
            return Operation().Delete(this.Id) > 0;
        }
        protected override <%= this.SourceTable.Name %> DataLoad(Guid Id)
        {
            return Operation().GetModel(Id);
        }
        protected override SearchResult<<%= this.SourceTable.Name %>> DataSearch(SearchQuery Query)
        {
            return Operation().GetResult(Query);
        }
        protected override bool DataDelete(Guid Id)
        {
            return Operation().Delete(Id) > 0;
        }
        #endregion
        
    }
    
}

 

 

相比部分dbFirst的工具灵活性更强,可以根据框架自己做调整

 

posted on 2020-11-04 21:22  DaivdHu  阅读(216)  评论(0编辑  收藏  举报

导航