Example #1
0
        /// <summary>
        /// 获取数据表对应的类名
        /// </summary>
        /// <param name="table">数据表实体</param>
        /// <param name="codeEntParam">实体参数</param>
        /// <returns>类名</returns>
        private string GetClassName(Table table, CodeEntityParameter codeEntParam)
        {
            string className = table.Name;

            if (codeEntParam.IsAddOrRemovePrefix)
            {
                if (!table.Name.StartsWith(codeEntParam.Prefix))
                {
                    className = string.Format("{0}{1}", codeEntParam.Prefix, table.Name);
                }
            }
            else
            {
                if (table.Name.StartsWith(codeEntParam.Prefix))
                {
                    className = table.Name.Remove(0, codeEntParam.Prefix.Count());
                }
            }

            if (codeEntParam.IsAddSuffix)
            {
                className = string.Format("{0}{1}", className, codeEntParam.Suffix);
            }

            return(className);
        }
Example #2
0
        public static CodeEntityParameter Get()
        {
            CodeEntityParameter entity = null;
            XmlSerializer       xs     = new XmlSerializer(typeof(CodeEntityParameter));
            string xmlPath             = Application.ExecutablePath + ".Config.xml";

            try
            {
                using (Stream stream = new FileStream(xmlPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    entity = xs.Deserialize(stream) as CodeEntityParameter;
                    stream.Close();
                }
            }
            catch
            {
                entity                    = new CodeEntityParameter();
                entity.Server             = "(local)";
                entity.IntegratedSecurity = true;
                entity.UserId             = "sa";
                entity.UserPwd            = "123456";
                entity.SavePath           = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                entity.IsAddSuffix        = true;
                entity.Suffix             = "Entity";
                entity.NameSpace          = "Example.Entity";
            }

            return(entity);
        }
Example #3
0
        /// <summary>
        /// 创建类实体文件
        /// </summary>
        /// <param name="table">数据表实体</param>
        /// <param name="codeEntParam">实体参数</param>
        public void CreateCodeFile(Table table, CodeEntityParameter codeEntParam)
        {
            string        className     = string.Empty;
            CodeDomHelper codeDomHelper = new CodeDomHelper();

            className = GetClassName(table, codeEntParam);
            var codeCompileUnit     = codeDomHelper.GetCodeCompileUnit(codeEntParam.NameSpace, className, string.IsNullOrWhiteSpace(table.Comment) ? table.Name : table.Comment);
            var codeTypeDeclaration = codeCompileUnit.Namespaces[0].Types[0];

            foreach (var column in table.Columns)
            {
                codeTypeDeclaration.Members.Add(codeDomHelper.CreateAutoProperty(column.Type, column.Name, column.Comment));
            }

            CodeDomProvider      provider = new CSharpCodeProvider();
            CodeGeneratorOptions options  = new CodeGeneratorOptions();

            options.BracingStyle = "C";
            var stringBuilder = new StringBuilder();

            using (provider)
            {
                var stringWriter = new StringWriter(stringBuilder);
                provider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, new CodeGeneratorOptions());
            }
            string tmpPath = string.IsNullOrWhiteSpace(codeEntParam.Suffix) ? "Entity" : codeEntParam.Suffix;
            string path    = codeEntParam.SavePath;

            if (!codeEntParam.SavePath.EndsWith(tmpPath))
            {
                path = Path.Combine(codeEntParam.SavePath, tmpPath);
            }

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                string filePath = Path.Combine(path, className + ".cs");
                File.WriteAllText(filePath, CleanupCode(stringBuilder.ToString()), Encoding.UTF8);
            }
            catch (IOException ioEx)
            {
                throw ioEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #4
0
        /// <summary>
        /// 创建类实体文件
        /// </summary>
        /// <param name="table">数据表实体</param>
        /// <param name="codeEntParam">实体参数</param>
        public void CreateCodeFile(Table table, CodeEntityParameter codeEntParam)
        {
            string className = string.Empty;
            CodeDomHelper codeDomHelper = new CodeDomHelper();

            className = GetClassName(table, codeEntParam);
            var codeCompileUnit = codeDomHelper.GetCodeCompileUnit(codeEntParam.NameSpace, className, string.IsNullOrWhiteSpace(table.Comment) ? table.Name : table.Comment);
            var codeTypeDeclaration = codeCompileUnit.Namespaces[0].Types[0];
            foreach (var column in table.Columns)
            {
                codeTypeDeclaration.Members.Add(codeDomHelper.CreateAutoProperty(column.Type, column.Name, column.Comment));
            }

            CodeDomProvider provider = new CSharpCodeProvider();
            CodeGeneratorOptions options = new CodeGeneratorOptions();
            options.BracingStyle = "C";
            var stringBuilder = new StringBuilder();
            using (provider)
            {
                var stringWriter = new StringWriter(stringBuilder);
                provider.GenerateCodeFromCompileUnit(codeCompileUnit, stringWriter, new CodeGeneratorOptions());
            }
            string tmpPath = string.IsNullOrWhiteSpace(codeEntParam.Suffix) ? "Entity" : codeEntParam.Suffix;
            string path = codeEntParam.SavePath;
            if (! codeEntParam.SavePath.EndsWith(tmpPath))
            {
                path = Path.Combine(codeEntParam.SavePath, tmpPath);
            }

            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                string filePath = Path.Combine(path, className + ".cs");
                File.WriteAllText(filePath, CleanupCode(stringBuilder.ToString()), Encoding.UTF8);
            }
            catch (IOException ioEx)
            {
                throw ioEx;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
0
        /// <summary>
        /// 获取数据表对应的类名
        /// </summary>
        /// <param name="table">数据表实体</param>
        /// <param name="codeEntParam">实体参数</param>
        /// <returns>类名</returns>
        private string GetClassName(Table table, CodeEntityParameter codeEntParam)
        {
            string className = table.Name;
            if (codeEntParam.IsAddOrRemovePrefix)
            {
                if (! table.Name.StartsWith(codeEntParam.Prefix))
                {
                    className = string.Format("{0}{1}", codeEntParam.Prefix, table.Name);
                }
            }
            else
            {
                if (table.Name.StartsWith(codeEntParam.Prefix))
                {
                    className = table.Name.Remove(0, codeEntParam.Prefix.Count());
                }
            }

            if (codeEntParam.IsAddSuffix)
            {
                className = string.Format("{0}{1}", className, codeEntParam.Suffix);
            }

            return className;
        }
Example #6
0
 private CodeEntityParameter GetEntityParameter()
 {
     CodeEntityParameter entParam = new CodeEntityParameter();
     entParam.IsAddOrRemovePrefix = rbtnAddPrefix.Checked;
     entParam.IsAddOrRemovePrefix = !rbtnRemovePrefix.Checked;
     entParam.IsAddSuffix = chkSuffix.Checked;
     entParam.NameSpace = txtNameSpace.Text;
     entParam.Prefix = txtPrefix.Text;
     entParam.Suffix = txtSuffix.Text;
     entParam.SavePath = txtSavePath.Text;
     entParam.Server = txtServer.Text;
     entParam.UserId = txtUser.Text;
     entParam.UserPwd = txtPwd.Text;
     entParam.IntegratedSecurity = rbtnWindows.Checked;
     entParam.DataBaseName = cboDatabase.Text;
     return entParam;
 }
Example #7
0
        public static CodeEntityParameter Get()
        {
            CodeEntityParameter entity = null;
            XmlSerializer xs = new XmlSerializer(typeof(CodeEntityParameter));
            string xmlPath = Application.ExecutablePath + ".Config.xml";
            try
            {
                using (Stream stream = new FileStream(xmlPath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    entity = xs.Deserialize(stream) as CodeEntityParameter;
                    stream.Close();
                }
            }
            catch
            {
                entity = new CodeEntityParameter();
                entity.Server = "(local)";
                entity.IntegratedSecurity = true;
                entity.UserId = "sa";
                entity.UserPwd = "123456";
                entity.SavePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                entity.IsAddSuffix = true;
                entity.Suffix = "Entity";
                entity.NameSpace = "Example.Entity";
            }

            return entity;
        }