Ejemplo n.º 1
0
        /// <summary>
        /// 產生 Entities 的 Class 定義
        /// </summary>
        /// <param name="project"></param>
        /// <param name="currentFolder"></param>
        /// <param name="entitiesFolder"></param>
        /// <param name="classType">需要產生的類別類型 (DTO 物件/Entity 物件)</param>
        public static void CreateEnitiesFromSourceTables(
            Project project,
            string currentFolder,
            ProjectItem entitiesFolder,
            string classNameOrTableName,
            string commandName,
            bool createKeyAttribute,
            CLASS_TYPE classType)
        {
            Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();

            string   ClassName       = classNameOrTableName.Replace(" ", "_");
            ClassDef clsDef          = new ClassDef();
            SQLStore store           = new SQLStore();
            string   ClassDefined    = ClassDef.GetClassTemplate;
            string   classEndWord    = classType == CLASS_TYPE.DTO ? "Dto" : "Ent";
            string   usingStatement  = classType == CLASS_TYPE.ENTITY ? $"\nusing {project.Name}.Common;" : "";
            string   commandOrFolder = classType == CLASS_TYPE.ENTITY ? "Entities" : commandName;

            ClassDefined = ClassDefined.Replace("$(USING)$", usingStatement);
            if (string.IsNullOrEmpty(currentFolder))
            {
                ClassDefined = ClassDefined.Replace(".$(FOLDER_NAME)$", currentFolder);
            }
            else
            {
                ClassDefined = ClassDefined.Replace("$(FOLDER_NAME)$", currentFolder);
            }
            ClassDefined = ClassDefined.Replace("$(FOLDER_NAME)$", currentFolder);
            ClassDefined = ClassDefined.Replace("$(NAMESPACE_DEF)$", $"{project.Name}");
            ClassDefined = ClassDefined.Replace("$(QUERY_COMMAND_NAME)$", $"{commandOrFolder}");

            ClassDefined = ClassDefined.Replace(
                "$(CLASS_DEF)$",
                clsDef.GetClassDef(
                    store.GetNoDataDataTableByName(classNameOrTableName),
                    $"{ClassName.ToUpperFirstWord()}{classEndWord}",
                    createKeyAttribute ? store.GetTableKeyByName(string.Format("{0}", ClassName)) : new string[] { },
                    classType));

            //產生等會使用的暫存檔名
            string TempCSPath = Path.Combine(
                Environment.GetEnvironmentVariable("temp"),
                $"{classNameOrTableName.ToUpperFirstWord()}{classEndWord}.cs");

            if (!Directory.Exists(Path.GetDirectoryName(TempCSPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(TempCSPath));
            }
            //建立暫存的 Class 檔案
            CreateModelCSFile(ClassDefined, TempCSPath);
            //加入暫存的 Class 檔案
            entitiesFolder.ProjectItems.AddFromFileCopy(TempCSPath);
            //刪除掉暫存檔案
            try
            {
                File.Delete(TempCSPath);
            }
            catch (Exception ex) { } //刪除暫存檔案若失敗不處理任何訊息.
            //}
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 當整個專案正在進行 Generator 時所引發的事件.
        /// </summary>
        /// <param name="project">Visual Studio IDE 工具中,目前專案 Projects 集合</param>
        public void ProjectFinishedGenerating(Project project)
        {
            ProjectItem folder     = null;
            ProjectItem viewFolder = null;

            var result = from item in project.ProjectItems.OfType <ProjectItem>().AsEnumerable()
                         where item.Name == "Models"
                         select item;

            if (result.FirstOrDefault() == null)
            {
                folder = project.ProjectItems.AddFolder("Models");
            }
            else
            {
                //若這個目錄已經存在,則直接取得這個目錄.
                folder = result.FirstOrDefault();
            }

            var resultView = folder.ProjectItems.OfType <ProjectItem>().Where(c => c.Name == "ViewModels");

            if (resultView.FirstOrDefault() == null)
            {
                viewFolder = folder.ProjectItems.AddFolder("ViewModels");
            }
            else
            {
                viewFolder = resultView.FirstOrDefault();
            }

            foreach (string node in frmORMappingWindow.SelectedTables)
            {
                //MessageBox.Show(string.Format("node={0}", node));
                string ClassName = node.Replace(" ", "_");
                //MessageBox.Show(string.Format("ClassName={0}", ClassName));
                ClassDef clsDef       = new ClassDef();
                SQLStore store        = new SQLStore();
                string   ClassDefined = ClassDef.GetClassTemplate;
                ClassDefined = ClassDefined.Replace("$(NAMESPACE_DEF)$", string.Format("{0}.Models.ViewModels", project.Name));
                ClassDefined = ClassDefined.Replace("$(CLASS_DEF)$", clsDef.GetClassDef(store.GetNoDataDataTableByName(string.Format("[{0}]", node)), ClassName));
                //產生等會使用的暫存檔名
                string TempCSPath = Path.Combine(
                    Environment.GetEnvironmentVariable("temp"),
                    string.Format("{0}.cs", ClassName));

                //MessageBox.Show(TempCSPath);

                if (!Directory.Exists(Path.GetDirectoryName(TempCSPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(TempCSPath));
                }
                //建立暫存的 Class 檔案
                CreateModelCSFile(ClassDefined, TempCSPath);
                //加入暫存的 Class 檔案
                viewFolder.ProjectItems.AddFromFileCopy(TempCSPath);
                //刪除掉暫存檔案
                try {
                    File.Delete(TempCSPath);
                }
                catch (Exception ex) { } //刪除暫存檔案若失敗不處理任何訊息.
            }

            #region 使用預設Table建立View的資料夾
            ProjectItem defViewFolder = null;

            var result2 = from item in project.ProjectItems.OfType <ProjectItem>().AsEnumerable()
                          where item.Name == "Views"
                          select item;

            if (result2.FirstOrDefault() != null)
            {
                var defualtView = result2.FirstOrDefault();
                defViewFolder = defualtView.ProjectItems.AddFolder(defaultTable);
            }
            #endregion
        }