Example #1
0
        public static void WritePOCO(bool overread, bool overwrite = true)
        {
            if (_tables == null || overread)
            {
                GetTables();
            }
            string        baseFolder = ProjectContainer.DomainEntity.ToDirectory();
            StringBuilder build      = new StringBuilder();

            foreach (Table tbl in _tables.Where(t => !t.IsMapping).OrderBy(x => x.NameHumanCase))
            {
                foreach (Column col in tbl.Columns.OrderBy(x => x.Ordinal).Where(x => !x.Hidden))
                {
                    build.AppendLine("        " + WritePocoColumn(col));
                }
                ModelContainer.Regist("$Property$", build.ToString(), "实体自身属性");
                build.Clear();
                if (tbl.ReverseNavigationProperty.Count() > 0)
                {
                    foreach (string s in tbl.ReverseNavigationProperty.OrderBy(x => x))
                    {
                        build.AppendLine("        " + s);
                    }
                }
                ModelContainer.Regist("$Navigation$", build.ToString(), "实体导航属性");
                build.Clear();
                if (tbl.HasForeignKey)
                {
                    foreach (Column col in from c in tbl.Columns.OrderBy(x => x.EntityFk) where c.EntityFk != null select c)
                    {
                        build.AppendLine("        " + col.EntityFk);
                    }
                }
                ModelContainer.Regist("$ForeignKey$", build.ToString(), "实体外键属性");
                build.Clear();
                if (tbl.Columns.Where(c => c.Default != string.Empty).Count() > 0 || tbl.ReverseNavigationCtor.Count() > 0)
                {
                    build.AppendLine("        public " + tbl.Name + "()");
                    build.AppendLine("        {");
                    foreach (Column col in tbl.Columns.OrderBy(x => x.Ordinal).Where(c => c.Default != string.Empty))
                    {
                        build.AppendLine("          " + col.PropertyName + "=" + col.Default + ";");
                    }
                    foreach (string s in tbl.ReverseNavigationCtor)
                    {
                        build.AppendLine("          " + s);
                    }
                    build.AppendLine("        }");
                }
                ModelContainer.Regist("$Constructor$", build.ToString(), "实体构造函数");
                build.Clear();
                CodeCreateManager codeManager = new CodeCreateManager(ConstructType.Entity, new TemplateEntity()
                {
                    Entity = tbl.Name, Data2Obj = tbl.NameHumanCase
                });
                codeManager.IsOverWrite = overwrite;
                codeManager.BuildTaget  = ProjectContainer.DomainEntity;
                codeManager.CreateCode();
            }
        }
Example #2
0
        /// <summary>
        /// 写入模型关键字Xml
        /// </summary>
        /// <param name="key"></param>
        /// <param name="newComment"></param>
        public static void SetKeyComment(string key, string newComment, string value)
        {
            DataSet dt = new DataSet();

            dt.ReadXml("KeyComment.xml".GetFileResource("Xml"));
            DataSet dtNew = new DataSet();

            if (dt != null && dt.Tables.Count > 0)
            {
                dtNew.Merge(dt);
                int index = 0;
                for (index = 0; index < dt.Tables[0].Rows.Count; index++)
                {
                    if (dt.Tables[0].Rows[index][0].ToString() == key)
                    {
                        dtNew.Tables[0].Rows[index][1] = value;
                        dtNew.Tables[0].Rows[index][2] = newComment;
                        dtNew.Tables[0].Rows[index][3] = DateTime.Now;
                        break;
                    }
                }
                if (index > dt.Tables[0].Rows.Count - 1)
                {
                    dtNew.Tables[0].Rows.Add(key, value, newComment, DateTime.Now);
                }
            }
            else
            {
                dtNew.Tables[0].Rows.Add(key, value, newComment, DateTime.Now);
            }
            dtNew.WriteXml("KeyComment.xml".GetFileResource("Xml"));
            ModelContainer.Regist(key, value, newComment);
        }
Example #3
0
        public static void Create(bool overWrite)
        {
            StringBuilder build = new StringBuilder();

            //Infrustruture
            if (ProjectContainer.Infrastructure == null || overWrite)
            {
                ProjectContainer.Infrastructure = SolutionCommon.Dte.AddClassLibrary(SolutionCommon.Infrastructure, true);
            }
            ProjectContainer.Infrastructure.AddAdoNetEntityDataModel(SolutionCommon.ProjectName);
            ProjectContainer.Infrastructure.ProjectItems.Find(SolutionCommon.ProjectName + ".tt", true).Delete();
            string contextPath = SolutionCommon.ProjectName + ".Context.tt";

            ProjectContainer.Infrastructure.ProjectItems.Find(contextPath, true).Remove();
            ProjectContainer.Infrastructure.AddFromFile(Path.Combine(ProjectContainer.Infrastructure.ToDirectory(), contextPath)).Name = SolutionCommon.ProjectName + "Context.tt";
            ProjectContainer.Infrastructure.Save();
            ProjectItem contextItem = ProjectContainer.Infrastructure.ProjectItems.Find(SolutionCommon.ProjectName + "Context.cs", true);
            EditPoint   editPoint   = contextItem.FileCodeModel.CodeElements.Item(1).StartPoint.CreateEditPoint();

            editPoint.Insert(string.Format("using {0};\r\n", SolutionCommon.DomainEntity));
            //Extention
            CodeStaticManager codeManager = new CodeStaticManager(ConstructType.DbContextExtensions);

            codeManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "Extention", Name = "DbContextExtensions.cs", Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();
            //Unit

            using (StreamReader reader = new StreamReader(Path.Combine(ProjectContainer.Infrastructure.ToDirectory(), SolutionCommon.ProjectName + "Context.cs")))
            {
                string ContextName = string.Empty;
                while (reader.Peek() != -1)
                {
                    string temp = reader.ReadLine();
                    if (temp.IndexOf("class") != -1)
                    {
                        temp        = temp.Substring(25);
                        ContextName = temp.Split(':')[0].Trim();
                        ModelContainer.Regist("$ContextName$", ContextName, "上下文名称");
                        break;
                    }
                }
            }

            codeManager            = new CodeStaticManager(ConstructType.ContextUnit);
            codeManager.BuildTaget = new StringCodeArgment()
            {
                Name = SolutionCommon.ProjectName + "ContextUnit.cs", Encode = Encoding.Default, Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();
        }
Example #4
0
        public static void CreateCodeFirst(bool overWrite = true)
        {
            if (ProjectContainer.Infrastructure == null)
            {
                ProjectContainer.Infrastructure = SolutionCommon.Dte.AddClassLibrary(SolutionCommon.Infrastructure, true);
            }
            CodeStaticManager codeManager = new CodeStaticManager(ConstructType.DbContextExtensions);

            codeManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "Extention", Name = "DbContextExtensions.cs", Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();

            CodeFirstLogic.WriteConfigFies(overWrite);

            Tables tables = CodeFirstLogic.GetTables();

            CodeBuilderContainer.DBContextBuilder.Clear();
            foreach (Table tb in tables)
            {
                CodeAppendManager appendManager = new CodeAppendManager(ConstructType.DBContext, new TemplateEntity()
                {
                    Entity = tb.Name, Data2Obj = tb.NameHumanCase
                });
                appendManager.BuildTaget = CodeBuilderContainer.DBContextBuilder;
                appendManager.CreateCode();
            }
            ModelContainer.Regist("$ContextName$", CodeFirstTools.DbContextName, "上下文名称");
            codeManager = new CodeStaticManager(ConstructType.DBContext);

            codeManager.BuildTaget = new StringCodeArgment()
            {
                Name = CodeFirstTools.DbContextName + ".cs", Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();

            codeManager            = new CodeStaticManager(ConstructType.ContextUnit);
            codeManager.BuildTaget = new StringCodeArgment()
            {
                Name = CodeFirstTools.DbContextName + "Unit.cs", Encode = Encoding.Default, Target = ProjectContainer.Infrastructure
            };
            codeManager.CreateCode();
        }
Example #5
0
        /// <summary>
        /// 活动模板的抽象类构造函数
        /// </summary>
        /// <param name="consType">创建类别</param>
        /// <param name="entity">活动模板的实体信息</param>
        public CodeManageBase(ConstructType consType, TemplateEntity entity)
        {
            IsStaticModel = true;
            ModelEntity   = entity;
            Construct     = consType;
            StringBuilder result = new StringBuilder();

            ModelContainer.Regist("$Data2Obj$", entity.Data2Obj, "实体的数据库名称");
            ModelContainer.Regist("$Entity$", entity.Entity, "实体的应用名称");
            using (StreamReader reader = new StreamReader(string.Format("{0}.slm", consType.ToString()).GetFileResource("Code")))
            {
                while (reader.Peek() != -1)
                {
                    string temp = reader.ReadLine();
                    temp = ModelContainer.Replace(temp);
                    result.AppendLine(temp);
                }
            }
            CodeContent = result.ToString();
        }
Example #6
0
        public static void WriteConfigFies(bool overread, bool overWrite = true)
        {
            if (_tables == null || overread)
            {
                GetTables();
            }

            string baseFolder = ProjectContainer.Infrastructure.ToDirectory();

            foreach (Table tbl in _tables.Where(t => !t.IsMapping).OrderBy(x => x.NameHumanCase))
            {
                ModelContainer.Regist("$Schema$", tbl.Schema, "数据库命名空间");
                ModelContainer.Regist("$PrimaryKey$", tbl.PrimaryKeyNameHumanCase(), "映射主键内容");
                StringBuilder build = new StringBuilder();
                foreach (Column col in tbl.Columns.Where(x => !x.Hidden).OrderBy(x => x.Ordinal))
                {
                    build.AppendLine(col.Config);
                }
                ModelContainer.Regist("$Property$", build.ToString(), "映射自身属性内容");
                build.Clear();
                if (tbl.HasForeignKey)
                {
                    foreach (Column col in from c in tbl.Columns.OrderBy(x => x.Ordinal) where c.ConfigFk != null select c)
                    {
                        build.AppendLine(col.ConfigFk);
                    }
                }
                ModelContainer.Regist("$ForeignKey$", build.ToString(), "映射外键属性内容");
                CodeCreateManager codeManager = new CodeCreateManager(ConstructType.Map, new TemplateEntity()
                {
                    Entity = tbl.Name, Data2Obj = tbl.NameHumanCase
                });
                codeManager.IsOverWrite = overWrite;
                codeManager.BuildTaget  = ProjectContainer.Infrastructure;
                codeManager.CreateCode();
            }
        }
Example #7
0
        public static void CreateFrame(bool overWrite)
        {
            if (ProjectContainer.Service == null)
            {
                ProjectContainer.Service = SolutionCommon.Dte.AddWebService(SolutionCommon.Service, true);
            }

            if (ProjectContainer.Service.ProjectItems.Find("web.config") != null)
            {
                ProjectContainer.Service.ProjectItems.Find("web.config").Delete();
            }
            if (SolutionCommon.infrastryctType == InfrastructType.DbFirst)
            {
                using (StreamReader reader = new StreamReader(Path.Combine(ProjectContainer.Infrastructure.ToDirectory(), "app.config")))
                {
                    while (reader.Peek() != -1)
                    {
                        string temp = reader.ReadLine();
                        if (temp.IndexOf(ModelContainer.Resolve("$ContextName$")) != -1)
                        {
                            ModelContainer.Regist("$ConnectionString$", temp, "数据库连接字符串");
                            break;
                        }
                    }
                }
            }
            else
            {
                string str = string.Empty;
                if (CodeFirstTools.DataType == "MicrosoftSqlServer")
                {
                    str = "<add name=\"" + CodeFirstTools.DbContextName + "\" providerName=\"" + CodeFirstTools.ProviderName + "\" connectionString=\"" + CodeFirstTools.ConnectionString + ";MultipleActiveResultSets=True;Pooling=True;\"/>";
                }
                else
                {
                    str = "<add name=\"" + CodeFirstTools.DbContextName + "\" providerName=\"Oracle.DataAccess.Client\" connectionString=\"" + CodeFirstTools.ConnectionString + "\"/>";
                }
                ModelContainer.Regist("$ConnectionString$", str, "数据库连接字符串");
            }

            if (SolutionCommon.infrastryctType == InfrastructType.CodeFirst)
            {
                ModelContainer.Regist("$DBSchemaApp$", string.Format("<add key=\"DBSchema\" value=\"{0}\"/>", CodeFirstTools.SchemaName), "数据库命名空间");
            }
            else
            {
                ModelContainer.Regist("$DBSchemaApp$", "", "数据库命名空间");
            }
            CodeStaticManager staticManager = new CodeStaticManager(ConstructType.WebConfig);

            staticManager.BuildTaget = new StringCodeArgment()
            {
                Encode = Encoding.UTF8, Name = "web.config", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //ServiceBehinde
            string behindCode = string.Format("<%@ ServiceHost Language=\"C#\" Debug=\"true\" Service=\"{1}.{0}Service\" CodeBehind=\"{0}Service.svc.cs\" %>", SolutionCommon.ProjectName, SolutionCommon.Service);

            ProjectContainer.Service.AddFromFileString(behindCode, SolutionCommon.ProjectName + "Service.svc", Encoding.Default);


            //AttachDataSignBehavior
            staticManager            = new CodeStaticManager(ConstructType.AttachDataSignBehavior);
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "InstanceProviders", Name = "AttachDataSignBehavior.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //UnityInstanceProvider
            staticManager            = new CodeStaticManager(ConstructType.UnityInstanceProvider);
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "InstanceProviders", Name = "UnityInstanceProvider.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //UnityInstanceProviderServiceBehavior
            staticManager            = new CodeStaticManager(ConstructType.UnityInstanceProviderServiceBehavior);
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "InstanceProviders", Name = "UnityInstanceProviderServiceBehavior.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //AssemblyInfo
            string        assemblyInfoPath = Path.Combine(ProjectContainer.Service.ToDirectory(), "Properties", "AssemblyInfo.cs");
            StringBuilder build            = FileOprateHelp.ReadFile(assemblyInfoPath);

            build.AppendLine("[assembly: log4net.Config.XmlConfigurator(Watch = true)]");//日志监视
            FileOprateHelp.SaveFile(build.ToString(), assemblyInfoPath);

            SolutionCommon.Dte.SetStartup(ProjectContainer.Service);
        }
Example #8
0
        public static void CreateCode(bool overWrite)
        {
            if (ProjectContainer.Service == null)
            {
                throw new Exception("Entity2Code Service Project Can not be Find");
            }

            if (overWrite)
            {
                CodeBuilderContainer.ServiceBuilder.Clear();
                CodeBuilderContainer.IServiceBuilder.Clear();
                CodeBuilderContainer.ContainBuilder.Clear();
                List <TemplateEntity> entitys = DomainEntityLogic.GetEntitys(false);
                for (int i = 0; i < entitys.Count; i++)
                {
                    CodeAppendManager codeManager = new CodeAppendManager(ConstructType.Service, entitys[i]);
                    codeManager.BuildTaget = CodeBuilderContainer.ServiceBuilder;
                    codeManager.CreateCode();

                    codeManager            = new CodeAppendManager(ConstructType.IService, entitys[i]);
                    codeManager.BuildTaget = CodeBuilderContainer.IServiceBuilder;
                    codeManager.CreateCode();

                    codeManager            = new CodeAppendManager(ConstructType.Container, entitys[i]);
                    codeManager.BuildTaget = CodeBuilderContainer.ContainBuilder;
                    codeManager.CreateCode();
                }
            }

            CodeStaticManager staticManager = new CodeStaticManager(ConstructType.Service);

            //Service
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Name = SolutionCommon.ProjectName + "Service.svc.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //IService
            staticManager            = new CodeStaticManager(ConstructType.IService);
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Name = "I" + SolutionCommon.ProjectName + "Service.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();

            //Container
            if (SolutionCommon.infrastryctType == InfrastructType.CodeFirst)
            {
                ModelContainer.Regist("$DBAppContent$", " string db= System.Configuration.ConfigurationManager.AppSettings[\"DBSchema\"];", "获取数据库连接字符串的函数内容");
                ModelContainer.Regist("$DBConstr$", "new InjectionConstructor(db)", "注册上下文的函数内容");
            }
            else
            {
                ModelContainer.Regist("$DBAppContent$", "", "获取数据库连接字符串的函数内容");
                ModelContainer.Regist("$DBConstr$", "", "注册上下文的函数内容");
            }
            staticManager            = new CodeStaticManager(ConstructType.Container);
            staticManager.BuildTaget = new StringCodeArgment()
            {
                Folder = "InstanceProviders", Name = "Container.cs", Target = ProjectContainer.Service
            };
            staticManager.CreateCode();
        }
Example #9
0
        private void btnInsert_Click(object sender, EventArgs e)
        {
            //获取所有参数
            MethodCommon.MethodName = txtName.Text;
            MethodCommon.Organize   = txtCompany.Text;
            MethodCommon.Author     = txtAuthor.Text;
            MethodCommon.Return     = cmbReturn.Text;
            MethodCommon.Comment    = txtComment.Text;
            List <ReferArg> refs = new List <ReferArg>();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (row.Index < dataGridView1.Rows.Count - 1)
                {
                    ReferArg temp = new ReferArg();
                    temp.Comment = row.Cells[Comment.Name].Value == null ? string.Empty : row.Cells[Comment.Name].Value.ToString();
                    temp.Name    = row.Cells[VarName.Name].Value == null ? string.Empty : row.Cells[VarName.Name].Value.ToString();
                    temp.IsOut   = row.Cells[IsOut.Name].Value == null ? false : (row.Cells[IsOut.Name].Value.ToString() == "1" ? true : false);
                    temp.VType   = row.Cells[VarType.Name].Value == null ? string.Empty : row.Cells[VarType.Name].Value.ToString();

                    refs.Add(temp);
                }
            }
            MethodCommon.InnerArgs = refs;

            StringBuilder build1 = new StringBuilder();
            StringBuilder build2 = new StringBuilder();
            StringBuilder build3 = new StringBuilder();

            foreach (ReferArg item in refs)
            {
                build1.AppendLine(string.Format("/// <param name='{0}'>{1}</param>", item.Name, item.Comment));
                build2.Append(string.Format("{0} {1},", item.VType, item.Name));
                build3.Append(item.Name + ",");
            }

            ModelContainer.Regist("$ParamComment$", build1.ToString());
            ModelContainer.Regist("$Param$", build2.ToString().Remove(build2.ToString().Length - 1));
            ModelContainer.Regist("$InnerParam$", build3.ToString().Remove(build3.ToString().Length - 1));

            string returnComment = string.Empty;
            string needReturn    = string.Empty;
            string needReturn2   = string.Empty;

            if (MethodCommon.Return != "void")
            {
                returnComment = string.Format(" /// <returns>{0}</returns>", txtReComment.Text);
                needReturn    = "return";
                needReturn2   = "";
            }


            ModelContainer.Regist("$Comment$", MethodCommon.Comment, "方法描述");
            ModelContainer.Regist("$Return$", MethodCommon.Return, "方法返回类型");
            ModelContainer.Regist("$Author$", MethodCommon.Author, "方法作者");
            ModelContainer.Regist("$Organize$", MethodCommon.Organize, "方法机构");
            ModelContainer.Regist("$MethodName$", MethodCommon.MethodName, "方法名称");
            ModelContainer.Regist("$ReturnComment$", returnComment);
            ModelContainer.Regist("$NeedReturn$", needReturn);
            ModelContainer.Regist("$NeedReturn2$", needReturn2);

            TemplateEntity template = new TemplateEntity();

            template.Entity   = _selectEntity.Entity;
            template.Data2Obj = _selectEntity.Data2Obj.Substring(0, _selectEntity.Data2Obj.Length - 3);

            CodeAppendManager manager = new CodeAppendManager(ConstructType.MethodApp, template);

            build1.Clear();
            manager.BuildTaget = build1;
            manager.CreateCode();

            FileOprateHelp.WriteMethod(ProjectContainer.Application, build1.ToString(), template, template.Data2Obj + "App");

            CodeAppendManager manager1 = new CodeAppendManager(ConstructType.MethodIApp, template);

            build1.Clear();
            manager1.BuildTaget = build1;
            manager1.CreateCode();

            FileOprateHelp.WriteMethod(ProjectContainer.IApplication, build1.ToString(), template, "I" + template.Data2Obj + "App");

            CodeAppendManager manager2 = new CodeAppendManager(ConstructType.MethodServer, template);

            build1.Clear();
            manager2.BuildTaget = build1;
            manager2.CreateCode();

            FileOprateHelp.WriteMethod(ProjectContainer.Service, build1.ToString(), template, SolutionCommon.ProjectName + "Service.svc");

            CodeAppendManager manager3 = new CodeAppendManager(ConstructType.MethodIServer, template);

            build1.Clear();
            manager3.BuildTaget = build1;
            manager3.CreateCode();

            FileOprateHelp.WriteMethod(ProjectContainer.Service, build1.ToString(), template, "I" + SolutionCommon.ProjectName + "Service");

            this.Close();
        }