新增生成器选项BuilderOption,便于批量设置生成参数
This commit is contained in:
parent
b7d76f9688
commit
f00cbedd6d
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace XCode.Code
|
||||
{
|
||||
/// <summary>生成器选项</summary>
|
||||
public class BuilderOption
|
||||
{
|
||||
#region 属性
|
||||
/// <summary>命名空间</summary>
|
||||
public String Namespace { get; set; }
|
||||
|
||||
/// <summary>引用命名空间</summary>
|
||||
public HashSet<String> Usings { get; } = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>纯净类</summary>
|
||||
public Boolean Pure { get; set; }
|
||||
|
||||
/// <summary>生成接口</summary>
|
||||
public Boolean Interface { get; set; }
|
||||
|
||||
/// <summary>类名后缀。如Model/Dto等</summary>
|
||||
public String ClassPrefix { get; set; }
|
||||
|
||||
/// <summary>基类</summary>
|
||||
public String BaseClass { get; set; }
|
||||
|
||||
/// <summary>是否分部类</summary>
|
||||
public Boolean Partial { get; set; } = true;
|
||||
|
||||
/// <summary>输出目录</summary>
|
||||
public String Output { get; set; }
|
||||
|
||||
/// <summary>连接名</summary>
|
||||
public String ConnName { get; set; }
|
||||
#endregion
|
||||
|
||||
#region 构造
|
||||
/// <summary>实例化</summary>
|
||||
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 方法
|
||||
/// <summary>克隆</summary>
|
||||
/// <returns></returns>
|
||||
public BuilderOption Clone() => MemberwiseClone() as BuilderOption;
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -22,46 +22,17 @@ namespace XCode.Code
|
|||
/// <summary>类名。默认Table.Name</summary>
|
||||
public String ClassName { get; set; }
|
||||
|
||||
/// <summary>命名空间</summary>
|
||||
public String Namespace { get; set; }
|
||||
|
||||
/// <summary>引用命名空间</summary>
|
||||
public HashSet<String> Usings { get; } = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>纯净类</summary>
|
||||
public Boolean Pure { get; set; }
|
||||
|
||||
/// <summary>生成接口</summary>
|
||||
public Boolean Interface { get; set; }
|
||||
|
||||
/// <summary>基类</summary>
|
||||
public String BaseClass { get; set; }
|
||||
|
||||
/// <summary>是否分部类</summary>
|
||||
public Boolean Partial { get; set; } = true;
|
||||
#endregion
|
||||
|
||||
#region 构造
|
||||
/// <summary>实例化</summary>
|
||||
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");
|
||||
}
|
||||
/// <summary>生成器选项</summary>
|
||||
public BuilderOption Option { get; set; } = new BuilderOption();
|
||||
#endregion
|
||||
|
||||
#region 静态快速
|
||||
/// <summary>加载模型文件</summary>
|
||||
/// <param name="xmlFile"></param>
|
||||
/// <param name="atts"></param>
|
||||
/// <param name="xmlFile">Xml模型文件</param>
|
||||
/// <param name="option">生成可选项</param>
|
||||
/// <param name="atts">扩展属性字典</param>
|
||||
/// <returns></returns>
|
||||
public static IList<IDataTable> LoadModels(String xmlFile, out IDictionary<String, String> atts)
|
||||
public static IList<IDataTable> LoadModels(String xmlFile, BuilderOption option, out IDictionary<String, String> 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);
|
||||
}
|
||||
|
||||
/// <summary>生成简易版模型</summary>
|
||||
/// <param name="tables">表集合</param>
|
||||
/// <param name="output">输出目录</param>
|
||||
/// <param name="prefix">后缀。附在实体类名和文件名后面</param>
|
||||
/// <param name="option">可选项</param>
|
||||
/// <returns></returns>
|
||||
public static Int32 BuildModels(IList<IDataTable> tables, String output, String prefix = null)
|
||||
public static Int32 BuildModels(IList<IDataTable> 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
|
|||
|
||||
/// <summary>生成简易版实体接口</summary>
|
||||
/// <param name="tables">表集合</param>
|
||||
/// <param name="output">输出目录</param>
|
||||
/// <param name="prefix">后缀。附在实体类名和文件名后面</param>
|
||||
/// <param name="option">可选项</param>
|
||||
/// <returns></returns>
|
||||
public static Int32 BuildInterfaces(IList<IDataTable> tables, String output, String prefix = null)
|
||||
public static Int32 BuildInterfaces(IList<IDataTable> 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
|
|||
/// <summary>执行生成</summary>
|
||||
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
|
|||
|
||||
/// <summary>获取基类</summary>
|
||||
/// <returns></returns>
|
||||
protected virtual String GetBaseClass() => BaseClass;
|
||||
protected virtual String GetBaseClass() => Option.BaseClass;
|
||||
|
||||
/// <summary>实体类头部</summary>
|
||||
protected virtual void BuildAttribute()
|
||||
|
@ -217,7 +198,7 @@ namespace XCode.Code
|
|||
var des = Table.Description;
|
||||
WriteLine("/// <summary>{0}</summary>", 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("/// <summary>{0}</summary>", 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 保存
|
||||
/// <summary>输出目录</summary>
|
||||
public String Output { get; set; }
|
||||
|
||||
/// <summary>保存文件,返回文件路径</summary>
|
||||
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);
|
||||
|
|
|
@ -15,9 +15,6 @@ namespace XCode.Code
|
|||
public class EntityBuilder : ClassBuilder
|
||||
{
|
||||
#region 属性
|
||||
/// <summary>连接名</summary>
|
||||
public String ConnName { get; set; }
|
||||
|
||||
/// <summary>泛型实体类。泛型参数名TEntity</summary>
|
||||
public Boolean GenericType { get; set; }
|
||||
|
||||
|
@ -28,20 +25,6 @@ namespace XCode.Code
|
|||
public IList<IDataTable> AllTables { get; set; } = new List<IDataTable>();
|
||||
#endregion
|
||||
|
||||
#region 构造
|
||||
/// <summary>实例化</summary>
|
||||
public EntityBuilder()
|
||||
{
|
||||
Usings.Add("XCode");
|
||||
Usings.Add("XCode.Configuration");
|
||||
Usings.Add("XCode.DataAccessLayer");
|
||||
|
||||
Pure = false;
|
||||
|
||||
if (Debug) Log = XTrace.Log;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 静态快速
|
||||
/// <summary>为Xml模型文件生成实体类</summary>
|
||||
/// <param name="xmlFile">模型文件</param>
|
||||
|
@ -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
|
|||
|
||||
/// <summary>为Xml模型文件生成实体类</summary>
|
||||
/// <param name="tables">模型文件</param>
|
||||
/// <param name="output">输出目录</param>
|
||||
/// <param name="nameSpace">命名空间</param>
|
||||
/// <param name="connName">连接名</param>
|
||||
/// <param name="baseClass">基类</param>
|
||||
/// <param name="option">生成可选项</param>
|
||||
/// <param name="chineseFileName">是否中文名称</param>
|
||||
public static Int32 BuildTables(IList<IDataTable> tables, String output = null, String nameSpace = null, String connName = null, String baseClass = null, Boolean chineseFileName = true)
|
||||
public static Int32 BuildTables(IList<IDataTable> 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;
|
||||
}
|
||||
|
||||
/// <summary>为Xml模型文件生成实体类</summary>
|
||||
/// <param name="tables">模型文件</param>
|
||||
/// <returns></returns>
|
||||
public static Int32 BuildTables(IList<IDataTable> 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 += "<TEntity>";
|
||||
|
||||
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
|
|||
/// <summary>增加常用命名空间</summary>
|
||||
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);
|
||||
|
|
|
@ -15,9 +15,12 @@ namespace XUnitTest.XCode.Code
|
|||
{
|
||||
private IList<IDataTable> _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);
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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<TEntity> : IUser
|
||||
{
|
||||
#region 属性
|
||||
|
|
Loading…
Reference in New Issue