buildermodel.cs

来自「通过数据库结构自动 生成三层结构代码,相当不错的一款软件」· CS 代码 · 共 122 行

CS
122
字号
namespace LTP.BuilderModel
{
    using LTP.CodeHelper;
    using LTP.IBuilder;
    using LTP.Utility;
    using System;
    using System.Collections.Generic;

    public class BuilderModel : IBuilderModel
    {
        protected List<ColumnInfo> _fieldlist;
        protected string _modelname = "";
        protected string _modelpath = "";
        protected string _namespace = "Maticsoft";

        public string CreatModel()
        {
            StringPlus plus = new StringPlus();
            plus.AppendLine("using System;");
            plus.AppendLine("namespace " + this.Modelpath);
            plus.AppendLine("{");
            plus.AppendSpaceLine(1, "/// <summary>");
            plus.AppendSpaceLine(1, "/// 实体类" + this._modelname + " 。(属性说明自动提取数据库字段的描述信息)");
            plus.AppendSpaceLine(1, "/// </summary>");
            plus.AppendSpaceLine(1, "public class " + this._modelname);
            plus.AppendSpaceLine(1, "{");
            plus.AppendSpaceLine(2, "public " + this._modelname + "()");
            plus.AppendSpaceLine(2, "{}");
            plus.AppendLine(this.CreatModelMethod());
            plus.AppendSpaceLine(1, "}");
            plus.AppendLine("}");
            plus.AppendLine("");
            return plus.ToString();
        }

        public string CreatModelMethod()
        {
            StringPlus plus = new StringPlus();
            StringPlus plus2 = new StringPlus();
            StringPlus plus3 = new StringPlus();
            plus.AppendSpaceLine(2, "#region Model");
            foreach (ColumnInfo info in this.Fieldlist)
            {
                string columnName = info.ColumnName;
                string typeName = info.TypeName;
                bool isIdentity = info.IsIdentity;
                bool isPK = info.IsPK;
                bool cisNull = info.cisNull;
                string deText = info.DeText;
                typeName = CodeCommon.DbTypeToCS(typeName);
                string str4 = "";
                if ((CodeCommon.isValueType(typeName) && !isIdentity) && (!isPK && cisNull))
                {
                    str4 = "?";
                }
                plus2.AppendSpaceLine(2, "private " + typeName + str4 + " _" + columnName.ToLower() + ";");
                plus3.AppendSpaceLine(2, "/// <summary>");
                plus3.AppendSpaceLine(2, "/// " + deText);
                plus3.AppendSpaceLine(2, "/// </summary>");
                plus3.AppendSpaceLine(2, "public " + typeName + str4 + " " + columnName);
                plus3.AppendSpaceLine(2, "{");
                plus3.AppendSpaceLine(3, "set{ _" + columnName.ToLower() + "=value;}");
                plus3.AppendSpaceLine(3, "get{return _" + columnName.ToLower() + ";}");
                plus3.AppendSpaceLine(2, "}");
            }
            plus.Append(plus2.Value);
            plus.Append(plus3.Value);
            plus.AppendSpaceLine(2, "#endregion Model");
            return plus.ToString();
        }

        public List<ColumnInfo> Fieldlist
        {
            get
            {
                return this._fieldlist;
            }
            set
            {
                this._fieldlist = value;
            }
        }

        public string ModelName
        {
            get
            {
                return this._modelname;
            }
            set
            {
                this._modelname = value;
            }
        }

        public string Modelpath
        {
            get
            {
                return this._modelpath;
            }
            set
            {
                this._modelpath = value;
            }
        }

        public string NameSpace
        {
            get
            {
                return this._namespace;
            }
            set
            {
                this._namespace = value;
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?