コード例 #1
0
        void GenerateWebPageJs(EntityInfoDto entity, string webProjectPath, string namespaceName)
        {
            string folder = Path.Combine(webProjectPath, "Views", entity.Name + "s");

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            string entityDescription = entity.Name;

            if (entity.Type.HasAttribute <EntityDescriptionAttribute>())
            {
                entityDescription = entity.Type.GetAttribute <EntityDescriptionAttribute>().Description;
            }

            var pagetemplate = EmbeddedResourcesHelper.Get("LesoftWuye2.CodeGenerator", "templates.js.txt");

            //1.替换实体名
            string content = pagetemplate;

            content = content.Replace("{$NameSpace$}", namespaceName);
            content = content.Replace("{$EntityName$}", entity.Name);
            content = content.Replace("{$EntityName1$}", entity.Name.ToLower());

            string fileName = Path.Combine(folder, "Index.js");

            File.WriteAllText(fileName, content, Encoding.UTF8);
        }
コード例 #2
0
        void GenerateWebPage(EntityInfoDto entity, string webProjectPath, string namespaceName)
        {
            string folder = Path.Combine(webProjectPath, "Views", entity.Name + "s");

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            string entityDescription = entity.Name;

            if (entity.Type.HasAttribute <EntityDescriptionAttribute>())
            {
                entityDescription = entity.Type.GetAttribute <EntityDescriptionAttribute>().Description;
            }

            var pagetemplate = EmbeddedResourcesHelper.Get("LesoftWuye2.CodeGenerator", "templates.page.txt");

            //1.替换实体名
            //2.生成表格
            //3.生成新增页面
            //4.生成修改页面
            string content = pagetemplate.Replace("{$EntityDescription$}", entityDescription);

            content = content.Replace("{$NameSpace$}", namespaceName);
            content = content.Replace("{$EntityName$}", entity.Name);
            content = content.Replace("{$EntityName1$}", entity.Name.ToLower());

            string tableThead = "";
            string tableBody  = "";
            var    members    = entity.Type.GetProperties();

            foreach (var member in members)
            {
                if (member.HasAttribute <DtoAssignAttribute>())
                {
                    var dtoassignInfo = member.GetAttribute <DtoAssignAttribute>();
                    if ((dtoassignInfo.Targets & DtoAssignTargets.ListItem) == 0)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(tableThead))
                    {
                        tableThead += Environment.NewLine;
                    }
                    if (!string.IsNullOrEmpty(tableBody))
                    {
                        tableBody += Environment.NewLine;
                    }

                    string memberTitle = member.Name;
                    if (member.HasAttribute <DisplayAttribute>())
                    {
                        var displayInfo = member.GetAttribute <DisplayAttribute>();
                        memberTitle = displayInfo.Name;
                    }

                    tableThead += $"<th class='center'>{memberTitle}</th>";
                    tableBody  += $"<td class='center'>@{entity.Name.ToLower()}.{member.Name}</td>";
                }
            }



            content = content.Replace("{$TableThread$}", tableThead);
            content = content.Replace("{$TableBody$}", tableBody);



            string createModal = "";

            foreach (var member in members)
            {
                if (member.HasAttribute <DtoAssignAttribute>())
                {
                    var dtoassignInfo = member.GetAttribute <DtoAssignAttribute>();
                    if ((dtoassignInfo.Targets & DtoAssignTargets.Create) == 0)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(createModal))
                    {
                        createModal += Environment.NewLine;
                    }


                    string memberTitle = member.Name;
                    if (member.HasAttribute <DisplayAttribute>())
                    {
                        var displayInfo = member.GetAttribute <DisplayAttribute>();
                        memberTitle = displayInfo.Name;
                    }

                    bool required = member.HasAttribute <RequiredAttribute>();

                    if (member.PropertyType == typeof(string))
                    {
                        createModal += $"<div class='form-group'><label label-for='{member.Name}'>{memberTitle}{(required? "<span class='required'>*</span>":"")}</label><input class='form-control' type='text' name='{member.Name}' {(required? "required maxlength='@"+ namespaceName + ".Core."+entity.Name+ "s." + entity.Name + ".Max" + member.Name+"Length'" : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(int) || member.PropertyType == typeof(decimal))
                    {
                        required     = true;
                        createModal += $"<div class='form-group'><label label-for='{member.Name}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control' type='number' name='{member.Name}' {(required ? "required" : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(DateTime))
                    {
                        required     = true;
                        createModal += $"<div class='form-group'><label label-for='{member.Name}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control date-picker' data-date-format='yyyy-mm-dd' type='text' name='{member.Name}' {(required ? "required " : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(bool))
                    {
                        createModal += $"<div class='form-group'><div class='checkbox'><label><input type='checkbox' class='colored-blue' value='true' name='{member.Name}' data-role='excluded'><span class='text'>{memberTitle}</span></label></div></div>";
                    }
                    else
                    {
                        createModal += $"<div class='form-group'><label label-for='{member.Name}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control' type='text' name='{member.Name}' {(required ? "required maxlength='@" + namespaceName + ".Core." + entity.Name + "s." + entity.Name + ".Max" + member.Name + "Length'" : "")} ></div>";
                    }
                }
            }

            content = content.Replace("{$CreateModal$}", createModal);

            string updateModal = "";

            foreach (var member in members)
            {
                if (member.HasAttribute <DtoAssignAttribute>())
                {
                    var dtoassignInfo = member.GetAttribute <DtoAssignAttribute>();
                    if ((dtoassignInfo.Targets & DtoAssignTargets.Update) == 0)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(updateModal))
                    {
                        updateModal += Environment.NewLine;
                    }


                    string memberTitle = member.Name;
                    if (member.HasAttribute <DisplayAttribute>())
                    {
                        var displayInfo = member.GetAttribute <DisplayAttribute>();
                        memberTitle = displayInfo.Name;
                    }

                    bool   required    = member.HasAttribute <RequiredAttribute>();
                    string memberName1 = member.Name.ToCamelCase();
                    if (member.PropertyType == typeof(string))
                    {
                        updateModal += $"<div class='form-group'><label label-for='{memberName1}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control' type='text' name='{memberName1}' {(required ? "required maxlength='@" + namespaceName + ".Core." + entity.Name + "s." + entity.Name + ".Max" + member.Name + "Length'" : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(int) || member.PropertyType == typeof(decimal))
                    {
                        updateModal += $"<div class='form-group'><label label-for='{memberName1}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control' type='number' name='{memberName1}' {(required ? "required maxlength='@" + namespaceName + ".Core." + entity.Name + "s." + entity.Name + ".Max" + member.Name + "Length'" : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(DateTime))
                    {
                        required     = true;
                        updateModal += $"<div class='form-group'><label label-for='{memberName1}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control date-picker' data-date-format='yyyy-mm-dd' type='text' name='{memberName1}' {(required ? "required " : "")} ></div>";
                    }
                    else if (member.PropertyType == typeof(bool))
                    {
                        updateModal += $"<div class='form-group'><div class='checkbox'><label><input type='checkbox' class='colored-blue' value='true' name='{memberName1}' data-role='excluded'><span class='text'>{memberTitle}</span></label></div></div>";
                    }
                    else
                    {
                        updateModal += $"<div class='form-group'><label label-for='{memberName1}'>{memberTitle}{(required ? "<span class='required'>*</span>" : "")}</label><input class='form-control' type='text' name='{memberName1}' {(required ? "required maxlength='@" + namespaceName + ".Core." + entity.Name + "s." + entity.Name + ".Max" + member.Name + "Length'" : "")} ></div>";
                    }
                }
            }

            content = content.Replace("{$UpdateModal$}", updateModal);



            string fileName = Path.Combine(folder, "Index.cshtml");

            File.WriteAllText(fileName, content, Encoding.UTF8);
        }