From f00cbedd6d12e58d3d1b51bdf5c86457b4c27f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E7=9F=B3=E5=A4=B4?= Date: Thu, 27 Aug 2020 19:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=9F=E6=88=90=E5=99=A8?= =?UTF-8?q?=E9=80=89=E9=A1=B9BuilderOption=EF=BC=8C=E4=BE=BF=E4=BA=8E?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E8=AE=BE=E7=BD=AE=E7=94=9F=E6=88=90=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode/Code/BuilderOption.cs | 61 ++++++ XCode/Code/ClassBuilder.cs | 103 ++++------ XCode/Code/EntityBuilder.cs | 177 +++++------------- XUnitTest.XCode/Code/ClassBuilderTests.cs | 64 +++++-- XUnitTest.XCode/Code/EntityBuilderTests.cs | 23 ++- .../Code/entity_user_generictype.cs | 2 +- 6 files changed, 214 insertions(+), 216 deletions(-) create mode 100644 XCode/Code/BuilderOption.cs diff --git a/XCode/Code/BuilderOption.cs b/XCode/Code/BuilderOption.cs new file mode 100644 index 000000000..3b85a980b --- /dev/null +++ b/XCode/Code/BuilderOption.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace XCode.Code +{ + /// 生成器选项 + public class BuilderOption + { + #region 属性 + /// 命名空间 + public String Namespace { get; set; } + + /// 引用命名空间 + public HashSet Usings { get; } = new HashSet(StringComparer.OrdinalIgnoreCase); + + /// 纯净类 + public Boolean Pure { get; set; } + + /// 生成接口 + public Boolean Interface { get; set; } + + /// 类名后缀。如Model/Dto等 + public String ClassPrefix { get; set; } + + /// 基类 + public String BaseClass { get; set; } + + /// 是否分部类 + public Boolean Partial { get; set; } = true; + + /// 输出目录 + public String Output { get; set; } + + /// 连接名 + public String ConnName { get; set; } + #endregion + + #region 构造 + /// 实例化 + public BuilderOption() + { + Namespace = GetType().Namespace; + + Usings.Add("System"); + Usings.Add("System.Collections.Generic"); + Usings.Add("System.ComponentModel"); + Usings.Add("System.Runtime.Serialization"); + Usings.Add("System.Web.Script.Serialization"); + Usings.Add("System.Xml.Serialization"); + } + #endregion + + #region 方法 + /// 克隆 + /// + public BuilderOption Clone() => MemberwiseClone() as BuilderOption; + #endregion + } +} \ No newline at end of file diff --git a/XCode/Code/ClassBuilder.cs b/XCode/Code/ClassBuilder.cs index c0d192098..6f40dc24d 100644 --- a/XCode/Code/ClassBuilder.cs +++ b/XCode/Code/ClassBuilder.cs @@ -22,46 +22,17 @@ namespace XCode.Code /// 类名。默认Table.Name public String ClassName { get; set; } - /// 命名空间 - public String Namespace { get; set; } - - /// 引用命名空间 - public HashSet Usings { get; } = new HashSet(StringComparer.OrdinalIgnoreCase); - - /// 纯净类 - public Boolean Pure { get; set; } - - /// 生成接口 - public Boolean Interface { get; set; } - - /// 基类 - public String BaseClass { get; set; } - - /// 是否分部类 - public Boolean Partial { get; set; } = true; - #endregion - - #region 构造 - /// 实例化 - public ClassBuilder() - { - Namespace = GetType().Namespace; - - Usings.Add("System"); - Usings.Add("System.Collections.Generic"); - Usings.Add("System.ComponentModel"); - Usings.Add("System.Runtime.Serialization"); - Usings.Add("System.Web.Script.Serialization"); - Usings.Add("System.Xml.Serialization"); - } + /// 生成器选项 + public BuilderOption Option { get; set; } = new BuilderOption(); #endregion #region 静态快速 /// 加载模型文件 - /// - /// + /// Xml模型文件 + /// 生成可选项 + /// 扩展属性字典 /// - public static IList LoadModels(String xmlFile, out IDictionary atts) + public static IList LoadModels(String xmlFile, BuilderOption option, out IDictionary atts) { if (xmlFile.IsNullOrEmpty()) { @@ -85,27 +56,36 @@ namespace XCode.Code ["xs:schemaLocation"] = "http://www.newlifex.com http://www.newlifex.com/Model2020.xsd" }; + if (option != null) + { + option.Output = atts["Output"] ?? Path.GetDirectoryName(xmlFile); + option.Namespace = atts["NameSpace"] ?? Path.GetFileNameWithoutExtension(xmlFile); + option.ConnName = atts["ConnName"]; + option.BaseClass = atts["BaseClass"]; + } + // 导入模型 return ModelHelper.FromXml(xmlContent, DAL.CreateTable, atts); } /// 生成简易版模型 /// 表集合 - /// 输出目录 - /// 后缀。附在实体类名和文件名后面 + /// 可选项 /// - public static Int32 BuildModels(IList tables, String output, String prefix = null) + public static Int32 BuildModels(IList tables, BuilderOption option = null) { + if (option == null) option = new BuilderOption(); + + option.Pure = true; + var count = 0; foreach (var item in tables) { var builder = new ClassBuilder { Table = item, - Output = output, - Pure = true, + Option = option, }; - if (!prefix.IsNullOrEmpty()) builder.ClassName = item.Name + prefix; builder.Execute(); builder.Save(null, true, false); @@ -117,21 +97,22 @@ namespace XCode.Code /// 生成简易版实体接口 /// 表集合 - /// 输出目录 - /// 后缀。附在实体类名和文件名后面 + /// 可选项 /// - public static Int32 BuildInterfaces(IList tables, String output, String prefix = null) + public static Int32 BuildInterfaces(IList tables, BuilderOption option = null) { + if (option == null) option = new BuilderOption(); + + option.Interface = true; + var count = 0; foreach (var item in tables) { var builder = new ClassBuilder { Table = item, - Output = output, - Interface = true, + Option = option, }; - if (!prefix.IsNullOrEmpty()) builder.ClassName = "I" + item.Name + prefix; builder.Execute(); builder.Save(null, true, false); @@ -146,7 +127,7 @@ namespace XCode.Code /// 执行生成 public virtual void Execute() { - if (ClassName.IsNullOrEmpty()) ClassName = Interface ? ("I" + Table.Name) : Table.Name; + if (ClassName.IsNullOrEmpty()) ClassName = (Option.Interface ? ("I" + Table.Name) : Table.Name) + Option.ClassPrefix; //WriteLog("生成 {0} {1}", Table.Name, Table.DisplayName); Clear(); @@ -163,14 +144,14 @@ namespace XCode.Code protected virtual void OnExecuting() { // 引用命名空间 - var us = Usings.OrderBy(e => e.StartsWith("System") ? 0 : 1).ThenBy(e => e).ToArray(); + var us = Option.Usings.OrderBy(e => e.StartsWith("System") ? 0 : 1).ThenBy(e => e).ToArray(); foreach (var item in us) { WriteLine("using {0};", item); } WriteLine(); - var ns = Namespace; + var ns = Option.Namespace; if (!ns.IsNullOrEmpty()) { WriteLine("namespace {0}", ns); @@ -192,10 +173,10 @@ namespace XCode.Code if (!bc.IsNullOrEmpty()) bc = " : " + bc; // 分部类 - var pc = Partial ? " partial" : ""; + var pc = Option.Partial ? " partial" : ""; // 类接口 - if (Interface) + if (Option.Interface) WriteLine("public{2} interface {0}{1}", cn, bc, pc); else WriteLine("public{2} class {0}{1}", cn, bc, pc); @@ -208,7 +189,7 @@ namespace XCode.Code /// 获取基类 /// - protected virtual String GetBaseClass() => BaseClass; + protected virtual String GetBaseClass() => Option.BaseClass; /// 实体类头部 protected virtual void BuildAttribute() @@ -217,7 +198,7 @@ namespace XCode.Code var des = Table.Description; WriteLine("/// {0}", des); - if (!Pure && !Interface) + if (!Option.Pure && !Option.Interface) { WriteLine("[Serializable]"); WriteLine("[DataObject]"); @@ -232,8 +213,7 @@ namespace XCode.Code // 类接口 WriteLine("}"); - var ns = Namespace; - if (!ns.IsNullOrEmpty()) + if (!Option.Namespace.IsNullOrEmpty()) { Writer.Write("}"); } @@ -260,7 +240,7 @@ namespace XCode.Code var des = dc.Description; WriteLine("/// {0}", des); - if (!Pure && !Interface) + if (!Option.Pure && !Option.Interface) { if (!des.IsNullOrEmpty()) WriteLine("[Description(\"{0}\")]", des); @@ -271,7 +251,7 @@ namespace XCode.Code var type = dc.Properties["Type"]; if (type.IsNullOrEmpty()) type = dc.DataType?.Name; - if (Interface) + if (Option.Interface) WriteLine("{0} {1} {{ get; set; }}", type, dc.Name); else WriteLine("public {0} {1} {{ get; set; }}", type, dc.Name); @@ -338,20 +318,17 @@ namespace XCode.Code #endregion #region 保存 - /// 输出目录 - public String Output { get; set; } - /// 保存文件,返回文件路径 public virtual String Save(String ext = null, Boolean overwrite = true, Boolean chineseFileName = true) { - var p = Output; + var p = Option.Output; if (ext.IsNullOrEmpty()) ext = ".cs"; else if (!ext.Contains(".")) ext += ".cs"; - if (Interface) + if (Option.Interface) p = p.CombinePath(ClassName + ext); else if (chineseFileName && !Table.DisplayName.IsNullOrEmpty()) p = p.CombinePath(Table.DisplayName + ext); diff --git a/XCode/Code/EntityBuilder.cs b/XCode/Code/EntityBuilder.cs index 2d49cc6fa..e9b7d24d5 100644 --- a/XCode/Code/EntityBuilder.cs +++ b/XCode/Code/EntityBuilder.cs @@ -15,9 +15,6 @@ namespace XCode.Code public class EntityBuilder : ClassBuilder { #region 属性 - /// 连接名 - public String ConnName { get; set; } - /// 泛型实体类。泛型参数名TEntity public Boolean GenericType { get; set; } @@ -28,20 +25,6 @@ namespace XCode.Code public IList AllTables { get; set; } = new List(); #endregion - #region 构造 - /// 实例化 - public EntityBuilder() - { - Usings.Add("XCode"); - Usings.Add("XCode.Configuration"); - Usings.Add("XCode.DataAccessLayer"); - - Pure = false; - - if (Debug) Log = XTrace.Log; - } - #endregion - #region 静态快速 /// 为Xml模型文件生成实体类 /// 模型文件 @@ -60,72 +43,32 @@ namespace XCode.Code xmlFile = xmlFile.GetBasePath(); if (!File.Exists(xmlFile)) throw new FileNotFoundException("指定模型文件不存在!", xmlFile); + var option = new BuilderOption + { + Output = output, + Namespace = nameSpace, + ConnName = connName, + }; + // 导入模型 - var tables = LoadModels(xmlFile, out var atts); + var tables = LoadModels(xmlFile, option, out var 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"]; + // 反哺。确保输出空特性 + atts["Output"] = option.Output + ""; + atts["NameSpace"] = option.Namespace + ""; + atts["ConnName"] = option.ConnName + ""; + atts["BaseClass"] = option.BaseClass + ""; + atts.Remove("NameIgnoreCase"); + atts.Remove("IgnoreNameCase"); // 中文文件名 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); - - // 确保输出空特性 - 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"); - atts.Remove("IgnoreNameCase"); + var rs = BuildTables(tables, option, chineseFileName.Value); // 更新xsd atts["xmlns"] = atts["xmlns"].Replace("ModelSchema", "Model2020"); @@ -141,19 +84,16 @@ namespace XCode.Code /// 为Xml模型文件生成实体类 /// 模型文件 - /// 输出目录 - /// 命名空间 - /// 连接名 - /// 基类 + /// 生成可选项 /// 是否中文名称 - public static Int32 BuildTables(IList tables, String output = null, String nameSpace = null, String connName = null, String baseClass = null, Boolean chineseFileName = true) + public static Int32 BuildTables(IList tables, BuilderOption option, Boolean chineseFileName = true) { if (tables == null || tables.Count == 0) return 0; - output = output.GetBasePath(); + //output = output.GetBasePath(); - // 连接名 - if (connName.IsNullOrEmpty() && !nameSpace.IsNullOrEmpty() && nameSpace.Contains(".")) connName = nameSpace.Substring(nameSpace.LastIndexOf(".") + 1); + //// 连接名 + //if (connName.IsNullOrEmpty() && !nameSpace.IsNullOrEmpty() && nameSpace.Contains(".")) connName = nameSpace.Substring(nameSpace.LastIndexOf(".") + 1); //XTrace.WriteLine("代码生成:{0} 输出:{1} 命名空间:{2} 连接名:{3} 基类:{4}", tables.Count, output, nameSpace, connName, baseClass); @@ -161,13 +101,15 @@ namespace XCode.Code foreach (var item in tables) { var builder = new EntityBuilder { AllTables = tables, }; + if (option != null) builder.Option = option; builder.Load(item); - if (!output.IsNullOrEmpty()) builder.Output = output; - if (!nameSpace.IsNullOrEmpty()) builder.Namespace = nameSpace; - if (!connName.IsNullOrEmpty()) builder.ConnName = connName; - if (!baseClass.IsNullOrEmpty()) builder.BaseClass = baseClass; + //var option = builder.Option; + //if (!output.IsNullOrEmpty()) option.Output = output; + //if (!nameSpace.IsNullOrEmpty()) option.Namespace = nameSpace; + //if (!connName.IsNullOrEmpty()) builder.ConnName = connName; + //if (!baseClass.IsNullOrEmpty()) option.BaseClass = baseClass; builder.Execute(); builder.Save(null, true, chineseFileName); @@ -181,31 +123,6 @@ namespace XCode.Code return count; } - - /// 为Xml模型文件生成实体类 - /// 模型文件 - /// - public static Int32 BuildTables(IList tables) - { - var count = 0; - foreach (var item in tables) - { - var builder = new EntityBuilder { AllTables = tables, }; - - builder.Load(item); - - builder.Execute(); - builder.Save(null, true, true); - - builder.Business = true; - builder.Execute(); - builder.Save(null, false, true); - - count++; - } - - return count; - } #endregion #region 方法 @@ -215,22 +132,24 @@ namespace XCode.Code { Table = table; + var option = Option; + // 命名空间 var str = table.Properties["Namespace"]; - if (!str.IsNullOrEmpty()) Namespace = str; + if (!str.IsNullOrEmpty()) option.Namespace = str; // 连接名 var connName = table.ConnName; - if (connName.IsNullOrEmpty() && !Namespace.IsNullOrEmpty()) + if (connName.IsNullOrEmpty() && !option.Namespace.IsNullOrEmpty()) { - var p = Namespace.LastIndexOf('.'); - if (p > 0) connName = Namespace.Substring(p + 1); + var p = option.Namespace.LastIndexOf('.'); + if (p > 0) connName = option.Namespace.Substring(p + 1); } - if (!connName.IsNullOrEmpty()) ConnName = connName; + if (!connName.IsNullOrEmpty()) option.ConnName = connName; // 基类 str = table.Properties["BaseClass"]; - if (!str.IsNullOrEmpty()) BaseClass = str; + if (!str.IsNullOrEmpty()) option.BaseClass = str; // 泛型实体类 str = table.Properties["RenderGenEntity"]; @@ -242,7 +161,7 @@ namespace XCode.Code // 输出目录 str = table.Properties["Output"]; - if (!str.IsNullOrEmpty()) Output = str.GetBasePath(); + if (!str.IsNullOrEmpty()) option.Output = str.GetBasePath(); } #endregion @@ -251,9 +170,9 @@ namespace XCode.Code public override void Execute() { // 增加常用命名空间 - if (Business) AddNameSpace(); + AddNameSpace(); - if (ClassName.IsNullOrEmpty()) ClassName = Interface ? ("I" + Table.Name) : Table.Name; + if (ClassName.IsNullOrEmpty()) ClassName = (Option.Interface ? ("I" + Table.Name) : Table.Name) + Option.ClassPrefix; if (GenericType) ClassName += ""; base.Execute(); @@ -293,7 +212,7 @@ namespace XCode.Code // 数据类的基类只有接口,业务类基类则比较复杂 if (!Business) return "I" + Table.Name; - var name = BaseClass; + var name = Option.BaseClass; if (name.IsNullOrEmpty()) name = "Entity"; if (GenericType) @@ -333,8 +252,7 @@ namespace XCode.Code BuildInterface(); } - var ns = Namespace; - if (!ns.IsNullOrEmpty()) + if (!Option.Namespace.IsNullOrEmpty()) { Writer.Write("}"); } @@ -343,8 +261,13 @@ namespace XCode.Code /// 增加常用命名空间 protected virtual void AddNameSpace() { - var us = Usings; - if (!Pure && !us.Contains("System.Web")) + var us = Option.Usings; + + us.Add("XCode"); + us.Add("XCode.Configuration"); + us.Add("XCode.DataAccessLayer"); + + if (Business && !Option.Pure && !us.Contains("System.Web")) { us.Add("System.IO"); us.Add("System.Linq"); @@ -388,7 +311,7 @@ namespace XCode.Code } var cn = dt.Properties["ConnName"]; - if (cn.IsNullOrEmpty()) cn = ConnName; + if (cn.IsNullOrEmpty()) cn = Option.ConnName; WriteLine("[BindTable(\"{0}\", Description = \"{1}\", ConnName = \"{2}\", DbType = DatabaseType.{3})]", dt.TableName, dt.Description, cn, dt.DbType); } @@ -411,7 +334,7 @@ namespace XCode.Code if (dc.Properties.TryGetValue("Attribute", out var att)) WriteLine("[{0}]", att.Replace("{name}", dc.Name)); - if (!Pure) + if (!Option.Pure) { var dis = dc.DisplayName; if (!dis.IsNullOrEmpty()) WriteLine("[DisplayName(\"{0}\")]", dis); @@ -429,7 +352,7 @@ namespace XCode.Code else WriteLine("[BindColumn(\"{0}\", \"{1}\", \"{2}\"{3})]", dc.ColumnName, dc.Description, dc.RawType, dc.Master ? ", Master = true" : ""); - if (Interface) + if (Option.Interface) WriteLine("{0} {1} {{ get; set; }}", type, dc.Name); else WriteLine("public {0} {1} {{ get => _{1}; set {{ if (OnPropertyChanging(\"{1}\", value)) {{ _{1} = value; OnPropertyChanged(\"{1}\"); }} }} }}", type, dc.Name); diff --git a/XUnitTest.XCode/Code/ClassBuilderTests.cs b/XUnitTest.XCode/Code/ClassBuilderTests.cs index c713dc96f..0bbd9682d 100644 --- a/XUnitTest.XCode/Code/ClassBuilderTests.cs +++ b/XUnitTest.XCode/Code/ClassBuilderTests.cs @@ -15,9 +15,12 @@ namespace XUnitTest.XCode.Code { private IList _tables; private IDataTable _table; + private BuilderOption _option; + public ClassBuilderTests() { - _tables = ClassBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", out _); + _option = new BuilderOption(); + _tables = ClassBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", _option, out _); _table = _tables.FirstOrDefault(e => e.Name == "User"); } @@ -27,9 +30,9 @@ namespace XUnitTest.XCode.Code var builder = new ClassBuilder { Table = _table, - Namespace = "Company.MyName" }; - builder.Usings.Add("NewLife.Remoting"); + builder.Option.Namespace = "Company.MyName"; + builder.Option.Usings.Add("NewLife.Remoting"); builder.Execute(); @@ -46,9 +49,9 @@ namespace XUnitTest.XCode.Code var builder = new ClassBuilder { Table = _table, - BaseClass = "MyEntityBase", - Partial = false }; + builder.Option.BaseClass = "MyEntityBase"; + builder.Option.Partial = false; builder.Execute(); @@ -65,8 +68,8 @@ namespace XUnitTest.XCode.Code var builder = new ClassBuilder { Table = _table, - Pure = true }; + builder.Option.Pure = true; builder.Execute(); @@ -83,8 +86,8 @@ namespace XUnitTest.XCode.Code var builder = new ClassBuilder { Table = _table, - Interface = true }; + builder.Option.Interface = true; builder.Execute(); @@ -101,26 +104,29 @@ namespace XUnitTest.XCode.Code var builder = new ClassBuilder { Table = _table, - Pure = true, - Output = ".\\Output\\" + Rand.NextString(8) }; + var option = builder.Option; + option.Pure = true; + option.Output = ".\\Output\\" + Rand.NextString(8); + + if (Directory.Exists(option.Output.GetFullPath())) Directory.Delete(option.Output.GetFullPath(), true); builder.Execute(); - var file = (builder.Output + "\\" + builder.Table.DisplayName + ".cs").GetFullPath(); + var file = (option.Output + "\\" + builder.Table.DisplayName + ".cs").GetFullPath(); if (File.Exists(file)) File.Delete(file); builder.Save(); Assert.True(File.Exists(file)); - file = (builder.Output + "\\" + builder.Table.Name + ".xs").GetFullPath(); + file = (option.Output + "\\" + builder.Table.Name + ".xs").GetFullPath(); if (File.Exists(file)) File.Delete(file); builder.Save(".xs", false, false); Assert.True(File.Exists(file)); - // 清理 - Directory.Delete(builder.Output.GetFullPath(), true); + //// 清理 + //Directory.Delete(option.Output.GetFullPath(), true); } [Fact] @@ -130,8 +136,14 @@ namespace XUnitTest.XCode.Code var dir = ".\\Output\\Models\\"; if (Directory.Exists(dir.GetFullPath())) Directory.Delete(dir.GetFullPath(), true); - ClassBuilder.BuildModels(_tables, dir, "Model"); - ClassBuilder.BuildInterfaces(_tables, dir, "Model"); + var option = new BuilderOption + { + Output = dir, + ClassPrefix = "Model" + }; + + ClassBuilder.BuildModels(_tables, option); + ClassBuilder.BuildInterfaces(_tables, option); foreach (var item in _tables) { @@ -149,8 +161,16 @@ namespace XUnitTest.XCode.Code var dir = ".\\Output\\Dtos\\"; if (Directory.Exists(dir.GetFullPath())) Directory.Delete(dir.GetFullPath(), true); - ClassBuilder.BuildModels(_tables, dir, "Dto"); - ClassBuilder.BuildInterfaces(_tables, dir); + var option = new BuilderOption + { + Output = dir, + ClassPrefix = "Dto" + }; + + ClassBuilder.BuildModels(_tables, option); + + option.ClassPrefix = null; + ClassBuilder.BuildInterfaces(_tables, option); foreach (var item in _tables) { @@ -168,15 +188,19 @@ namespace XUnitTest.XCode.Code var dir = ".\\Output\\BuildTT\\"; if (Directory.Exists(dir.GetFullPath())) Directory.Delete(dir.GetFullPath(), true); + var option = new BuilderOption + { + Output = dir, + ClassPrefix = "TT" + }; + // 测试Built.tt foreach (var item in _tables) { var builder = new ClassBuilder { Table = item, - Output = dir, - Pure = true, - ClassName = item.Name + "TT", + Option = option, }; builder.Execute(); builder.Save(null, true, false); diff --git a/XUnitTest.XCode/Code/EntityBuilderTests.cs b/XUnitTest.XCode/Code/EntityBuilderTests.cs index 63cda9dcf..1df2ccdb5 100644 --- a/XUnitTest.XCode/Code/EntityBuilderTests.cs +++ b/XUnitTest.XCode/Code/EntityBuilderTests.cs @@ -13,22 +13,29 @@ namespace XUnitTest.XCode.Code public class EntityBuilderTests { private IDataTable _table; + private BuilderOption _option; + public EntityBuilderTests() { - var tables = EntityBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", out _); + var tables = EntityBuilder.LoadModels(@"..\..\XCode\Membership\Member.xml", _option, out _); _table = tables.FirstOrDefault(e => e.Name == "User"); } [Fact] public void Normal() { - var builder = new EntityBuilder + var option = new BuilderOption { - Table = _table, ConnName = "MyConn", Namespace = "Company.MyName" }; - builder.Usings.Add("NewLife.Remoting"); + option.Usings.Add("NewLife.Remoting"); + + var builder = new EntityBuilder + { + Table = _table, + Option = option, + }; // 数据类 builder.Execute(); @@ -53,11 +60,17 @@ namespace XUnitTest.XCode.Code [Fact] public void GenericType() { + var option = new BuilderOption + { + ConnName = "MyConn", + Namespace = "Company.MyName" + }; + var builder = new EntityBuilder { Table = _table, GenericType = true, - Namespace = "Company.MyName" + Option = option, }; builder.Execute(); diff --git a/XUnitTest.XCode/Code/entity_user_generictype.cs b/XUnitTest.XCode/Code/entity_user_generictype.cs index 183ad51d2..d83686c14 100644 --- a/XUnitTest.XCode/Code/entity_user_generictype.cs +++ b/XUnitTest.XCode/Code/entity_user_generictype.cs @@ -17,7 +17,7 @@ namespace Company.MyName [BindIndex("IU_User_Name", true, "Name")] [BindIndex("IX_User_RoleID", false, "RoleID")] [BindIndex("IX_User_UpdateTime", false, "UpdateTime")] - [BindTable("User", Description = "用户", ConnName = "", DbType = DatabaseType.None)] + [BindTable("User", Description = "用户", ConnName = "MyConn", DbType = DatabaseType.None)] public partial class User : IUser { #region 属性