Exemplo n.º 1
0
        /// <summary>
        /// 使用模板生成代码
        /// </summary>
        /// <param name="tplContent">模板文件的内容</param>
        /// <returns></returns>
        public string Builder(string tplContent)
        {
            Columns = DbToCS.DbtoCSColumns(Columns, DbType);
            if (!string.IsNullOrWhiteSpace(tplContent))
            {
                #region 模板生成
                //var template = FileHelper.Read(@"D:\工作\GitHub\Dos.Tools\bin\Debug\Template\实体类_最新.tpl");
                var        primarykeyColumns = Columns.FindAll(col => col.IsPK);
                var        identityColumn    = Columns.Find(col => col.IsIdentity);
                StringPlus plus = new StringPlus();
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine("// <auto-generated>");
                plus.AppendLine("//     此代码由工具生成。");
                plus.AppendLine("//     运行时版本:" + Environment.Version.ToString());
                plus.AppendLine("//     Website: http://ITdos.com/Dos/ORM/Index.html");
                plus.AppendLine("//     对此文件的更改可能会导致不正确的行为,并且如果");
                plus.AppendLine("//     重新生成代码,这些更改将会丢失。");
                plus.AppendLine("// </auto-generated>");
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine();
                var result = Razor.Parse(tplContent, new
                {
                    ClassName         = ClassName,
                    TableName         = TableName,
                    Columns           = Columns,
                    NameSpace         = NameSpace,
                    PrimaryKeyColumns = primarykeyColumns,
                    IdentityColumn    = identityColumn,
                    i1 = 1,
                    i2 = 1,
                    i3 = 1
                });
                return(plus.ToString() + result);

                #endregion
            }
            return("请使用模板生成方式");
        }
Exemplo n.º 2
0
        private string BuilderMethod()
        {
            StringPlus plus = new StringPlus();


            plus.AppendSpaceLine(2, "#region Method");


            //只读
            if (IsView)
            {
                plus.AppendSpaceLine(2, "/// <summary>");
                plus.AppendSpaceLine(2, "/// 是否只读");
                plus.AppendSpaceLine(2, "/// </summary>");
                plus.AppendSpaceLine(2, "public override bool IsReadOnly()");
                plus.AppendSpaceLine(2, "{");
                plus.AppendSpaceLine(3, "return true;");
                plus.AppendSpaceLine(2, "}");
            }

            Model.ColumnInfo identityColumn = Columns.Find(delegate(Model.ColumnInfo col) { return(col.IsIdentity); });
            if (null != identityColumn)
            {
                plus.AppendSpaceLine(2, "/// <summary>");
                plus.AppendSpaceLine(2, "/// 获取实体中的标识列");
                plus.AppendSpaceLine(2, "/// </summary>");
                plus.AppendSpaceLine(2, "public override Field GetIdentityField()");
                plus.AppendSpaceLine(2, "{");
                plus.AppendSpaceLine(3, "return _." + identityColumn.ColumnName + ";");
                plus.AppendSpaceLine(2, "}");
            }

            List <Model.ColumnInfo> primarykeyColumns = Columns.FindAll(delegate(Model.ColumnInfo col) { return(col.IsPK); });

            if (null != primarykeyColumns && primarykeyColumns.Count > 0)
            {
                plus.AppendSpaceLine(2, "/// <summary>");
                plus.AppendSpaceLine(2, "/// 获取实体中的主键列");
                plus.AppendSpaceLine(2, "/// </summary>");
                plus.AppendSpaceLine(2, "public override Field[] GetPrimaryKeyFields()");
                plus.AppendSpaceLine(2, "{");
                plus.AppendSpaceLine(3, "return new Field[] {");
                StringPlus plus2 = new StringPlus();
                foreach (Model.ColumnInfo col in primarykeyColumns)
                {
                    plus2.AppendSpaceLine(4, "_." + col.ColumnName + ",");
                }
                plus.Append(plus2.ToString().TrimEnd().Substring(0, plus2.ToString().TrimEnd().Length - 1));
                plus.AppendLine("};");
                plus.AppendSpaceLine(2, "}");
            }



            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 获取列信息");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public override Field[] GetFields()");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "return new Field[] {");
            StringPlus plus3 = new StringPlus();

            foreach (ColumnInfo col in Columns)
            {
                plus3.AppendSpaceLine(4, "_." + col.ColumnName + ",");
            }
            plus.Append(plus3.ToString().TrimEnd().Substring(0, plus3.ToString().TrimEnd().Length - 1));
            plus.AppendLine("};");
            plus.AppendSpaceLine(2, "}");


            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 获取值信息");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public override object[] GetValues()");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "return new object[] {");
            StringPlus plus4 = new StringPlus();

            foreach (ColumnInfo col in Columns)
            {
                plus4.AppendSpaceLine(4, "this._" + col.ColumnName + ",");
            }
            plus.Append(plus4.ToString().TrimEnd().Substring(0, plus4.ToString().TrimEnd().Length - 1));
            plus.AppendLine("};");
            plus.AppendSpaceLine(2, "}");

            //2015-08-10注释
            //plus.AppendSpaceLine(2, "/// <summary>");
            //plus.AppendSpaceLine(2, "/// 给当前实体赋值");
            //plus.AppendSpaceLine(2, "/// </summary>");
            //plus.AppendSpaceLine(2, "public override void SetPropertyValues(IDataReader reader)");
            //plus.AppendSpaceLine(2, "{");
            //foreach (ColumnInfo col in Columns)
            //{
            //    plus.AppendSpaceLine(3, "this._" + col.ColumnName + " = DataUtils.ConvertValue<" + col.TypeName + ">(reader[\"" + col.ColumnNameRealName + "\"]);");
            //}
            //plus.AppendSpaceLine(2, "}");


            //2015-08-10注释
            //plus.AppendSpaceLine(2, "/// <summary>");
            //plus.AppendSpaceLine(2, "/// 给当前实体赋值");
            //plus.AppendSpaceLine(2, "/// </summary>");
            //plus.AppendSpaceLine(2, "public override void SetPropertyValues(DataRow row)");
            //plus.AppendSpaceLine(2, "{");
            //foreach (ColumnInfo col in Columns)
            //{
            //    plus.AppendSpaceLine(3, "this._" + col.ColumnName + " = DataUtils.ConvertValue<" + col.TypeName + ">(row[\"" + col.ColumnNameRealName + "\"]);");
            //}
            //plus.AppendSpaceLine(2, "}");


            plus.AppendSpaceLine(2, "#endregion");
            plus.AppendLine();



            plus.AppendSpaceLine(2, "#region _Field");
            plus.AppendSpaceLine(2, "/// <summary>");
            plus.AppendSpaceLine(2, "/// 字段信息");
            plus.AppendSpaceLine(2, "/// </summary>");
            plus.AppendSpaceLine(2, "public class _");
            plus.AppendSpaceLine(2, "{");
            plus.AppendSpaceLine(3, "/// <summary>");
            plus.AppendSpaceLine(3, "/// * ");
            plus.AppendSpaceLine(3, "/// </summary>");
            plus.AppendSpaceLine(3, "public readonly static Field All = new Field(\"*\",\"" + TableName + "\");");
            foreach (ColumnInfo col in Columns)
            {
                plus.AppendSpaceLine(3, "/// <summary>");
                plus.AppendSpaceLine(3, "/// " + col.DeText);
                plus.AppendSpaceLine(3, "/// </summary>");
                plus.AppendSpaceLine(3, "public readonly static Field " + col.ColumnName + " = new Field(\"" + col.ColumnNameRealName + "\",\"" + TableName + "\",\"" + (string.IsNullOrEmpty(col.DeText) ? col.ColumnNameRealName : col.DeText) + "\");");
            }
            plus.AppendSpaceLine(2, "}");
            plus.AppendSpaceLine(2, "#endregion");
            plus.AppendLine();

            return(plus.ToString());
        }
Exemplo n.º 3
0
        public string Builder(string tplContent = "")
        {
            Columns = DbToCS.DbtoCSColumns(Columns, DbType);
            if (!string.IsNullOrWhiteSpace(tplContent))
            {
                #region 模板生成
                //var template = FileHelper.Read(@"D:\工作\GitHub\Dos.Tools\bin\Debug\Template\实体类_最新.tpl");
                var        primarykeyColumns = Columns.FindAll(col => col.IsPK);
                var        identityColumn    = Columns.Find(col => col.IsIdentity);
                StringPlus plus = new StringPlus();
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine("// <auto-generated>");
                plus.AppendLine("//     此代码由工具生成。");
                plus.AppendLine("//     运行时版本:" + Environment.Version.ToString());
                plus.AppendLine("//     Website: http://ITdos.com/Dos/ORM/Index.html");
                plus.AppendLine("//     对此文件的更改可能会导致不正确的行为,并且如果");
                plus.AppendLine("//     重新生成代码,这些更改将会丢失。");
                plus.AppendLine("// </auto-generated>");
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine();
                var result = Razor.Parse(tplContent, new
                {
                    ClassName         = ClassName,
                    TableName         = TableName,
                    Columns           = Columns,
                    NameSpace         = NameSpace,
                    PrimaryKeyColumns = primarykeyColumns,
                    IdentityColumn    = identityColumn,
                    i1 = 1,
                    i2 = 1,
                    i3 = 1
                });
                return(plus.ToString() + result);

                #endregion
            }
            else
            {
                #region 老版本
                StringPlus plus = new StringPlus();
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine("// <auto-generated>");
                plus.AppendLine("//     此代码由工具生成。");
                plus.AppendLine("//     运行时版本:" + Environment.Version.ToString());
                plus.AppendLine("//     Website: http://ITdos.com/Dos/ORM/Index.html");
                plus.AppendLine("//     对此文件的更改可能会导致不正确的行为,并且如果");
                plus.AppendLine("//     重新生成代码,这些更改将会丢失。");
                plus.AppendLine("// </auto-generated>");
                plus.AppendLine("//------------------------------------------------------------------------------");
                plus.AppendLine();
                plus.AppendLine();
                plus.AppendLine("using System;");
                plus.AppendLine("using System.Data;");
                plus.AppendLine("using System.Data.Common;");
                plus.AppendLine("using Dos.ORM;");
                plus.AppendLine("using Dos.ORM.Common;");
                plus.AppendLine();
                plus.AppendLine("namespace " + NameSpace);
                plus.AppendLine("{");
                plus.AppendLine();
                plus.AppendSpaceLine(1, "/// <summary>");
                plus.AppendSpaceLine(1, "/// 实体类" + ClassName + " 。(属性说明自动提取数据库字段的描述信息)");
                plus.AppendSpaceLine(1, "/// </summary>");
                if (!_isNewModel)
                {
                    //plus.AppendSpaceLine(1, "[Serializable]");
                }
                else
                {
                    plus.AppendSpaceLine(1, "[Table(\"" + TableName + "\")]");
                }
                plus.AppendSpaceLine(1, "[Serializable]");
                plus.AppendSpaceLine(1, "public partial class " + ClassName + " : Entity ");
                plus.AppendSpaceLine(1, "{");
                if (!_isNewModel)
                {
                    plus.AppendSpaceLine(2, "public " + ClassName + "():base(\"" + TableName + "\") {}");
                    plus.AppendLine();
                }
                else
                {
                    //plus.AppendSpaceLine(2, "public " + ClassName + "() { }");
                }
                plus.AppendLine(BuilderModel());
                plus.AppendLine(BuilderMethod());
                plus.AppendSpaceLine(1, "}");
                plus.AppendLine("}");
                plus.AppendLine("");
                return(plus.ToString());

                #endregion
            }
        }