步步为营-60-代码生成器

Posted 逍遥小天狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营-60-代码生成器相关的知识,希望对你有一定的参考价值。

说明:代码生成器有很多,我们目前介绍的是动软代码生成器

1 打开动软,连接数据库,新建项目,选择要生成的表和地址,生成

2 自定义模板生成

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
    TableHost host = (TableHost)(Host);        
    string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
    string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName);
    ColumnInfo identityKey=host.IdentityKey;
    string returnValue = "void";
    if (identityKey!=null)
    {         
         returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);              
    }
#>
using Git.Framework.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Git.Framework.DataTypes.ExtensionMethods;
using Git.Framework.ORM;
using Git.Storage.Entity.Store;
using Git.Framework.DataTypes;
using Git.Framework.Json;
using Git.Storage.Common;
using System.Net.Http;
using Git.Framework.Resource;
using System.Transactions;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Git.Storage.Provider.Base;
using Git.Framework.Cache;
using Git.Storage.Entity.<#= host.TableName #>;
namespace Git.Storage.Provider.<#= host.TableName #>
//ToDo:注意此处的命名空间
{
   public partial class <#= host.TableName #>Provider : DataFactory
    {
        private readonly Log log = Log.Instance(typeof(<#= host.TableName #>Provider));

        public <#= host.TableName #>Provider() { }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(<#= host.TableName #>Entity entity)
        {
            entity.IncludeAll();
            int line = this.<#= host.TableName #>.Add(entity);
            if (line > 0)
            {
                CacheHelper.Remove(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE);
            }
            return line;
        }
        /// <summary>
        /// 获得所有
        /// </summary>
        /// <returns></returns>
        public List<<#= host.TableName #>Entity> GetList()
        {
            List<<#= host.TableName #>Entity> listResult = CacheHelper.Get(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE) as List<<#= host.TableName #>Entity>;
            if (!listResult.IsNullOrEmpty())
            {
                return listResult;
            }
            <#= host.TableName #>Entity entity = new <#= host.TableName #>Entity();
            entity.IncludeAll();
            listResult = this.<#= host.TableName #>.GetList(entity);
            if (!listResult.IsNullOrEmpty())
            {
                CacheHelper.Insert(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE, listResult);
            }
            return listResult;
        }
        /// <summary>
        /// 根据编号获取
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public <#= host.TableName #>Entity GetItem(string num)
        {
            List<<#= host.TableName #>Entity> listResult = GetList();
            if (!listResult.IsNullOrEmpty())
            {
                return listResult.FirstOrDefault(a => a.<#= host.TableName #>Num == num);
            }
            return null;
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        public List<<#= host.TableName #>Entity> GetPageList(<#= host.TableName #>Entity entity, ref PageInfo pageInfo)
        {
            List<<#= host.TableName #>Entity> listResult = GetList();
            if (!listResult.IsNullOrEmpty())
            {
                int rowCount = 0;
                rowCount = listResult.Where(a => a.<#= host.TableName #>Num.Contains(entity.<#= host.TableName #>Num) && a.IsDelete.Equals((int)EIsDelete.NotDelete)).Count();
                pageInfo.RowCount = rowCount;
                return listResult.Where(a => a.<#= host.TableName #>Num.Contains(entity.<#= host.TableName #>Num) && a.IsDelete.Equals((int)EIsDelete.NotDelete)).OrderBy(a => a.<#= host.TableName #>Num).Skip((pageInfo.PageIndex - 1) * pageInfo.PageSize).Take(pageInfo.PageSize).ToList();
            }
            return null;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="<#= host.TableName #>Num"></param>
        /// <returns></returns>
        public int Delete(string <#= host.TableName #>Num)
        {
            <#= host.TableName #>Entity entity = new <#= host.TableName #>Entity();
            entity.Where(a => a.<#= host.TableName #>Num == <#= host.TableName #>Num);
            int line = this.<#= host.TableName #>.Delete(entity);
            if (line > 0)
            {
                CacheHelper.Remove(CacheKey.JOOSHOW_<#= host.TableName.ToUpper() #>_CACHE);
            }
            return line;
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public int Edit(<#= host.TableName #>Entity entity)
        {
            entity.Include(a => new { <#= GetAllCol(host.Fieldlist)#>  });
            entity.Where(a => a.<#= host.TableName #>Num == entity.<#= host.TableName #>Num);
            int line = this.<#= host.TableName #>.Update(entity);
            return line;
        }
    
     } 
 }
<#+  
 private string GetAllCol(List<ColumnInfo> colInfoList){
           string value = "";
           foreach(ColumnInfo colInfo in colInfoList){
               value +="a."+colInfo.ColumnName+", ";
           }
           value=value.Replace("a.ID,",""); //去掉ID
           value=value.Trim();
           value=value.Substring(0,value.Length-1);//去掉最后一个,        
           return value;
   }
#>
自定义模板(自定义方法)

注意自定义方法必须是文件的结尾,后面连空格都不能有

<#@ template language="c#" HostSpecific="True" #>
<#@ output extension= ".cs" #>
<#
    TableHost host = (TableHost)(Host);        
    string ModelSpace = host.NameSpace+".Model."+ host.GetModelClass(host.TableName);
    string DALSpace= host.NameSpace+".DAL."+ host.GetDALClass(host.TableName);
    ColumnInfo identityKey=host.IdentityKey;
    string returnValue = "void";
    if (identityKey!=null)
    {         
         returnValue = CodeCommon.DbTypeToCS(identityKey.TypeName);              
    }
#>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Git.Framework.ORM;
//ToDo:注意此处的命名空间
namespace Git.Storage.Entity.Base
{
    [TableAttribute(DbName = "<#= host.DbName #>", Name = "<#= host.TableName #>", PrimaryKeyName = "ID", IsInternal = false)]
    public partial class  <#= host.TableName #>Entity:BaseEntity
    {
        public <#= host.TableName #>Entity()
        {
        }
        
         <# foreach (ColumnInfo c in host.Fieldlist)
        { #>          
        [DataMapping(ColumnName = "<#= c.ColumnName #>", DbType = DbType.<#= GetType(c.TypeName)#>,Length=<#=c.Length#>,CanNull=<#= c.Nullable#>,DefaultValue=<#= GetDefalut(c.DefaultVal)#>,PrimaryKey=<#=c.IsPrimaryKey#>,AutoIncrement=<#=c.IsIdentity#>,IsMap=true)]
        public .<#= GetType(c.TypeName)#> <#= c.ColumnName #> { get;  set; }

        public <#= host.TableName #>Entity Include<#= c.ColumnName #> (bool flag) 
        {
            if (flag && !this.ColumnList.Contains("<#= c.ColumnName #>"))
            {
                this.ColumnList.Add("<#= c.ColumnName #>");
            }
            return this;
        }            
        <# } #> 
     } 
 } 
<#+  
 private string GetType(string name)
        {
            string value = "string";
            switch (name)
            { 
                case "int":
                    value = "Int32";
                    break;
                case "bigint":
                    value = "Int64";
                    break;
                case "bit":
                    value = "Int16";
                    break;
                case "char":
                    value = "String";
                    break;
                case "date":
                    value = "DateTime";
                    break;
                case "datetime":
                    value = "DateTime";
                    break;
                case "datetime2":
                    value = "DateTime";
                    break;
                case "float":
                    value = "Double";
                    break;
                case "money":
                    value = "Double";
                    break;
                case "nchar":
                    value = "String";
                    break;
                case "ntext":
                    value = "String";
                    break;
                case "nvarchar":
                    value = "String";
                    break;
                case "varchar":
                    value = "String";
                    break;
                case "text":
                    value = "String";
                    break;
                
            }
            return value;
        }
        
   private string GetDefalut(string colName){
           string value = "null";
           if(colName!=""){
               value=colName;
           }
           return value;
   }
#>
自定义模板(字段的各个属性,包括字段类型在C#和数据库之间的转化)

 

以上是关于步步为营-60-代码生成器的主要内容,如果未能解决你的问题,请参考以下文章

postman 自动生成 curl 代码片段

postman 自动生成 curl 代码片段

JS代码片段:一个日期离现在多久了

前端开发工具vscode如何快速生成代码片段

前端开发工具vscode如何快速生成代码片段

Apollo Codegen 没有找到生成代码的操作或片段