public void RemoveTest()
 {
     _nullableDictionary.Add("1", RandomNumber());
     Assert.True(_nullableDictionary.ContainsKey("1"));
     _nullableDictionary.Remove("1");
     Assert.False(_nullableDictionary.ContainsKey("1"));
 }
Esempio n. 2
0
        /// <summary>为Xml模型文件生成实体类</summary>
        /// <param name="xmlFile">模型文件</param>
        /// <param name="output">输出目录</param>
        /// <param name="nameSpace">命名空间</param>
        /// <param name="connName">连接名</param>
        /// <param name="chineseFileName">中文文件名</param>
        /// <param name="ignoreNameCase">忽略表名、字段名大小写(true 当前表名与类名称相同时,则自动省略该属性,反之 false)</param>
        public static Int32 Build(String xmlFile = null, String output = null, String nameSpace = null, String connName = null, Boolean?chineseFileName = null, Boolean?ignoreNameCase = null)
        {
            if (xmlFile.IsNullOrEmpty())
            {
                var di = ".".GetBasePath().AsDirectory();
                //XTrace.WriteLine("未指定模型文件,准备从目录中查找第一个xml文件 {0}", di.FullName);
                // 选当前目录第一个
                xmlFile = di.GetFiles("*.xml", SearchOption.TopDirectoryOnly).FirstOrDefault()?.FullName;
            }

            if (xmlFile.IsNullOrEmpty())
            {
                throw new Exception("找不到任何模型文件!");
            }

            xmlFile = xmlFile.GetBasePath();
            if (!File.Exists(xmlFile))
            {
                throw new FileNotFoundException("指定模型文件不存在!", xmlFile);
            }

            // 导入模型
            var xml  = File.ReadAllText(xmlFile);
            var atts = new NullableDictionary <String, String>(StringComparer.OrdinalIgnoreCase)
            {
                ["xmlns"]             = "http://www.newlifex.com/Model2020.xsd",
                ["xmlns:xs"]          = "http://www.w3.org/2001/XMLSchema-instance",
                ["xs:schemaLocation"] = "http://www.newlifex.com http://www.newlifex.com/Model2020.xsd"
            };

            // 导入模型
            var tables = ModelHelper.FromXml(xml, DAL.CreateTable, atts);

            if (tables.Count == 0)
            {
                return(0);
            }

            // 输出
            if (!output.IsNullOrEmpty())
            {
                atts["Output"] = output;
            }
            else
            {
                output = atts["Output"];
            }
            if (output.IsNullOrEmpty())
            {
                output = Path.GetDirectoryName(xmlFile);
            }

            // 命名空间
            if (!nameSpace.IsNullOrEmpty())
            {
                atts["NameSpace"] = nameSpace;
            }
            else
            {
                nameSpace = atts["NameSpace"];
            }
            if (nameSpace.IsNullOrEmpty())
            {
                nameSpace = Path.GetFileNameWithoutExtension(xmlFile);
            }

            // 连接名
            if (!connName.IsNullOrEmpty())
            {
                atts["ConnName"] = connName;
            }
            else
            {
                connName = atts["ConnName"];
            }
            if (connName.IsNullOrEmpty() && !nameSpace.IsNullOrEmpty())
            {
                connName = nameSpace.Split(".").LastOrDefault(e => !e.EqualIgnoreCase("Entity"));
            }

            // 基类
            var baseClass = "";

            if (!baseClass.IsNullOrEmpty())
            {
                atts["BaseClass"] = baseClass;
            }
            else
            {
                baseClass = atts["BaseClass"];
            }

            // 中文文件名
            if (chineseFileName != null)
            {
                atts["ChineseFileName"] = chineseFileName.Value ? "True" : "False";
            }
            else
            {
                chineseFileName = atts["ChineseFileName"].ToBoolean(true);
            }

            // 忽略表名/字段名称大小写
            if (ignoreNameCase != null)
            {
                atts["IgnoreNameCase"] = ignoreNameCase.Value ? "True" : "False";
            }
            else
            {
                var str = atts["IgnoreNameCase"];
                if (str.IsNullOrEmpty())
                {
                    str = atts["NameIgnoreCase"];
                }
                ignoreNameCase = str.ToBoolean();
            }

            //XTrace.WriteLine("代码生成源:{0}", xmlFile);

            var rs = BuildTables(tables, output, nameSpace, connName, baseClass, chineseFileName.Value, ignoreNameCase.Value);

            // 确保输出空特性
            if (atts["Output"].IsNullOrEmpty())
            {
                atts["Output"] = "";
            }
            if (atts["NameSpace"].IsNullOrEmpty())
            {
                atts["NameSpace"] = "";
            }
            if (atts["ConnName"].IsNullOrEmpty())
            {
                atts["ConnName"] = "";
            }
            if (atts["BaseClass"].IsNullOrEmpty())
            {
                atts["BaseClass"] = "Entity";
            }
            if (atts["IgnoreNameCase"].IsNullOrEmpty())
            {
                atts["IgnoreNameCase"] = true + "";
            }
            atts.Remove("NameIgnoreCase");

            // 更新xsd
            atts["xmlns"]             = atts["xmlns"].Replace("ModelSchema", "Model2020");
            atts["xs:schemaLocation"] = atts["xs:schemaLocation"].Replace("ModelSchema", "Model2020");

            // 保存模型文件
            var xml2 = ModelHelper.ToXml(tables, atts);

            if (xml != xml2)
            {
                File.WriteAllText(xmlFile, xml2);
            }

            return(rs);
        }