diff --git a/Apewer.Source/Apewer.Source.csproj b/Apewer.Source/Apewer.Source.csproj index 010b8c3..ae9a2a0 100644 --- a/Apewer.Source/Apewer.Source.csproj +++ b/Apewer.Source/Apewer.Source.csproj @@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> - <Import Project="..\Apewer.props" /> + <Import Project="..\Apewer\Apewer.props" /> <PropertyGroup> <DefineConstants>SOURCE;$(DefineConstants);</DefineConstants> diff --git a/Apewer.Source/Source/DbClient.cs b/Apewer.Source/Source/DbClient.cs index a0541db..ef95aa4 100644 --- a/Apewer.Source/Source/DbClient.cs +++ b/Apewer.Source/Source/DbClient.cs @@ -299,7 +299,7 @@ namespace Apewer.Source /// <summary>使用指定语句查询,获取查询结果。</summary> /// <param name="sql">要执行的 SQL 语句。</param> /// <param name="parameters">为 SQL 语句提供的参数。</param> - public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new() => OrmHelper.As<object, T>(Query(typeof(T), sql, parameters)); + public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new() => SourceUtility.As<object, T>(Query(typeof(T), sql, parameters)); /// <summary>使用指定语句查询,获取查询结果。</summary> /// <param name="model">目标记录的类型。</param> @@ -318,7 +318,7 @@ namespace Apewer.Source { try { - var array = OrmHelper.Fill(query, model); + var array = SourceUtility.Fill(query, model); result = new Result<object[]>(array); } catch (Exception ex) diff --git a/Apewer.Source/Source/MySql.cs b/Apewer.Source/Source/MySql.cs index 679f4fd..1105d75 100644 --- a/Apewer.Source/Source/MySql.cs +++ b/Apewer.Source/Source/MySql.cs @@ -10,7 +10,7 @@ using System.Net; using System.Text; using System.Transactions; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; namespace Apewer.Source { @@ -480,7 +480,7 @@ namespace Apewer.Source public string Insert(object record, string table = null) { if (record == null) return "参数无效。"; - OrmHelper.FixProperties(record); + SourceUtility.FixProperties(record); var structure = TableStructure.Parse(record.GetType()); if (structure == null) return "无法解析记录模型。"; @@ -541,7 +541,7 @@ namespace Apewer.Source } /// <summary></summary> - public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// <summary></summary> public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new() @@ -556,14 +556,14 @@ namespace Apewer.Source } /// <summary>获取所有记录。Flag 为 0 时将忽略 Flag 条件。</summary> - public Result<object[]> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from `{tn}`; "; return $"select * from `{tn}` where `_flag`={flag}; "; }); /// <summary>获取所有记录。Flag 为 0 时将忽略 Flag 条件。</summary> - public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query<T>(this, (tn) => + public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) => { if (flag == 0) return $"select * from `{tn}`; "; return $"select * from `{tn}` where `_flag`={flag}; "; @@ -577,7 +577,7 @@ namespace Apewer.Source { if (skip < 0) return new Result<object[]>("参数 skip 超出了范围。"); if (count < 1) return new Result<object[]>("参数 count 超出了范围。"); - return OrmHelper.Query(this, model, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); + return SourceUtility.Query(this, model, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); } /// <summary>获取记录。</summary> @@ -587,25 +587,25 @@ namespace Apewer.Source { if (skip < 0) return new Result<T[]>("参数 skip 超出了范围。"); if (count < 1) return new Result<T[]>("参数 count 超出了范围。"); - return OrmHelper.Query<T>(this, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); + return SourceUtility.Query<T>(this, (tn) => $"select * from `{tn}` limit {skip}, {count}; "); } /// <summary>获取记录。</summary> - public Result<object> Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select * from `{tn}` where `_key`='{sk}' limit 1;"; return $"select * from `{tn}` where `_key`='{sk}' and `_flag`={flag} limit 1;"; }); /// <summary>获取记录。</summary> - public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => OrmHelper.Get<T>(this, key, (tn, sk) => + public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) => { if (flag == 0) return $"select * from `{tn}` where `_key`='{sk}' limit 1;"; return $"select * from `{tn}` where `_key`='{sk}' and `_flag`={flag} limit 1;"; }); /// <summary>>获取指定类型的主键,按 Flag 属性筛选。</summary> - public Result<string[]> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select `_key` from `{tn}`;"; return $"select `_key` from `{tn}` where `_flag`={flag};"; diff --git a/Apewer.Source/Source/SqlClient.cs b/Apewer.Source/Source/SqlClient.cs index 34b0034..264560c 100644 --- a/Apewer.Source/Source/SqlClient.cs +++ b/Apewer.Source/Source/SqlClient.cs @@ -7,7 +7,7 @@ using System.Data; using System.Data.Common; using System.Text; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; using System.Data.SqlClient; #if NETFRAMEWORK @@ -560,7 +560,7 @@ namespace Apewer.Source } /// <summary>获取按指定语句查询到的所有记录。</summary> - public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// <summary>获取按指定语句查询到的所有记录。</summary> public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new() @@ -575,35 +575,35 @@ namespace Apewer.Source } /// <summary>获取记录。</summary> - public Result<object[]> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>获取记录。</summary> - public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query<T>(this, (tn) => + public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public Result<object> Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => OrmHelper.Get<T>(this, key, (tn, sk) => + public Result<T> Get<T>(string key, long flag = 0) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// <summary>查询有效的 Key 值。</summary> - public Result<string[]> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}]; "; return $"select _key from [{tn}] where _flag={flag}; "; diff --git a/Apewer.Source/Source/SqlClientThin.cs b/Apewer.Source/Source/SqlClientThin.cs index 9d118dd..7776c14 100644 --- a/Apewer.Source/Source/SqlClientThin.cs +++ b/Apewer.Source/Source/SqlClientThin.cs @@ -9,7 +9,7 @@ using System.Data; using System.Data.Common; using System.Text; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; using System.Data.SqlClient; #if NETFRAMEWORK @@ -249,35 +249,35 @@ namespace Apewer.Source } /// <summary>获取记录。</summary> - public override Result<object[]> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public override Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>获取记录。</summary> - public override Result<T[]> Query<T>(long flag = 0) => OrmHelper.Query<T>(this, (tn) => + public override Result<T[]> Query<T>(long flag = 0) => SourceUtility.Query<T>(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public override Result<object> Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public override Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public override Result<T> Get<T>(string key, long flag = 0) => OrmHelper.Get<T>(this, key, (tn, sk) => + public override Result<T> Get<T>(string key, long flag = 0) => SourceUtility.Get<T>(this, key, (tn, sk) => { if (flag == 0) return $"select top 1 * from [{tn}] _key='{sk}'; "; return $"select top 1 * from [{tn}] where _key='{sk}' and _key='{sk}'; "; }); /// <summary>查询有效的 Key 值。</summary> - public override Result<string[]> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public override Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}]; "; return $"select _key from [{tn}] where _flag={flag}; "; diff --git a/Apewer.Source/Source/Sqlite.cs b/Apewer.Source/Source/Sqlite.cs index 897361e..86982a9 100644 --- a/Apewer.Source/Source/Sqlite.cs +++ b/Apewer.Source/Source/Sqlite.cs @@ -8,7 +8,7 @@ using System.Data.SQLite; using System.Text; //using Mono.Data.Sqlite; -using static Apewer.Source.OrmHelper; +using static Apewer.Source.SourceUtility; namespace Apewer.Source { @@ -422,7 +422,7 @@ namespace Apewer.Source public string Insert(object record, string table = null) { if (record == null) return "参数无效。"; - OrmHelper.FixProperties(record); + SourceUtility.FixProperties(record); // 解析模型,获取表名。 var structure = TableStructure.Parse(record.GetType()); @@ -506,7 +506,7 @@ namespace Apewer.Source } /// <summary>获取按指定语句查询到的所有记录。</summary> - public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => OrmHelper.Query(this, model, sql, parameters); + public Result<object[]> Query(Type model, string sql, IEnumerable<IDataParameter> parameters = null) => SourceUtility.Query(this, model, sql, parameters); /// <summary>获取按指定语句查询到的所有记录。</summary> public Result<T[]> Query<T>(string sql, IEnumerable<IDataParameter> parameters = null) where T : class, new() @@ -521,35 +521,35 @@ namespace Apewer.Source } /// <summary>查询多条记录。</summary> - public Result<object[]> Query(Type model, long flag = 0) => OrmHelper.Query(this, model, (tn) => + public Result<object[]> Query(Type model, long flag = 0) => SourceUtility.Query(this, model, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>查询多条记录。</summary> - public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => OrmHelper.Query<T>(this, (tn) => + public Result<T[]> Query<T>(long flag = 0) where T : class, IRecord, new() => SourceUtility.Query<T>(this, (tn) => { if (flag == 0) return $"select * from [{tn}]; "; return $"select * from [{tn}] where _flag={flag}; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public Result<object> Get(Type model, string key, long flag = 0) => OrmHelper.Get(this, model, key, (tn, sk) => + public Result<object> Get(Type model, string key, long flag = 0) => SourceUtility.Get(this, model, key, (tn, sk) => { if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; "; return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; "; }); /// <summary>获取具有指定 Key 的记录。</summary> - public Result<T> Get<T>(string key, long flag) where T : class, IRecord, new() => OrmHelper.Get<T>(this, key, (tn, sk) => + public Result<T> Get<T>(string key, long flag) where T : class, IRecord, new() => SourceUtility.Get<T>(this, key, (tn, sk) => { if (flag == 0) return $"select * from [{tn}] where _key='{sk}' limit 1; "; return $"select * from [{tn}] where _flag={flag} and _key='{sk}' limit 1; "; }); /// <summary>获取指定类型的主键,按 Flag 属性筛选。</summary> - public Result<string[]> Keys(Type model, long flag = 0) => OrmHelper.Keys(this, model, (tn) => + public Result<string[]> Keys(Type model, long flag = 0) => SourceUtility.Keys(this, model, (tn) => { if (flag == 0) return $"select _key from [{tn}] where _flag={flag}; "; return $"select _key from [{tn}]; "; diff --git a/Apewer.Web/Apewer.Web.csproj b/Apewer.Web/Apewer.Web.csproj index 5ce43fe..3dff1d2 100644 --- a/Apewer.Web/Apewer.Web.csproj +++ b/Apewer.Web/Apewer.Web.csproj @@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> - <Import Project="..\Apewer.props" /> + <Import Project="..\Apewer\Apewer.props" /> <PropertyGroup> <DefineConstants>WEB;$(DefineConstants);</DefineConstants> diff --git a/Apewer.Windows/Apewer.Windows.csproj b/Apewer.Windows/Apewer.Windows.csproj index e433f3f..190b45a 100644 --- a/Apewer.Windows/Apewer.Windows.csproj +++ b/Apewer.Windows/Apewer.Windows.csproj @@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> - <Import Project="..\Apewer.props" /> + <Import Project="..\Apewer\Apewer.props" /> <PropertyGroup> <NoWarn>CS0108;CS0612</NoWarn> diff --git a/Apewer/Apewer.csproj b/Apewer/Apewer.csproj index 7d9403c..4547155 100644 --- a/Apewer/Apewer.csproj +++ b/Apewer/Apewer.csproj @@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> - <Import Project="..\Apewer.props" /> + <Import Project="..\Apewer\Apewer.props" /> <PropertyGroup> <TargetFrameworks>netstandard2.1;netstandard2.0;netcoreapp3.1;net461;net40;net20</TargetFrameworks> diff --git a/Apewer/Apewer.props b/Apewer/Apewer.props new file mode 100644 index 0000000..898879a --- /dev/null +++ b/Apewer/Apewer.props @@ -0,0 +1,63 @@ +<Project> + + <!-- <Import Sdk="Microsoft.NET.Sdk" Project="Sdk.props" /> --> + + <!-- 程序集信息 --> + <PropertyGroup> + <Company>Apewer Lab</Company> + <Copyright>Copyright Apewer Lab. All rights reserved.</Copyright> + <Description></Description> + <RootNamespace>Apewer</RootNamespace> + <Product>Apewer Libraries</Product> + <Version>6.5.7</Version> + </PropertyGroup> + + <!-- 生成 --> + <PropertyGroup> + <AllowUnsafeBlocks>true</AllowUnsafeBlocks> + <LangVersion>latest</LangVersion> + </PropertyGroup> + + <!-- 打包 --> + <PropertyGroup Condition="'$(Configuration)' == 'Release'"> + <DebugType>none</DebugType> + <DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile> + <GeneratePackageOnBuild>true</GeneratePackageOnBuild> + <IsPackable>true</IsPackable> + <PackageOutputPath>..\</PackageOutputPath> + </PropertyGroup> + + <!-- 程序集名称 --> + <PropertyGroup> + <AssemblyTitle Condition="'$(TargetFramework)' == 'net6.0'">$(AssemblyName) - .NET 6</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'netcoreapp3.1'">$(AssemblyName) - .NET Core 3.1</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'netstandard2.1'">$(AssemblyName) - .NET Standard 2.1</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'netstandard2.0'">$(AssemblyName) - .NET Standard 2.0</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'net461'">$(AssemblyName) - .NET Framework 4.6.1</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'net40'">$(AssemblyName) - .NET Framework 4.0</AssemblyTitle> + <AssemblyTitle Condition="'$(TargetFramework)' == 'net20'">$(AssemblyName) - .NET Framework 2.0</AssemblyTitle> + </PropertyGroup> + + <!-- 条件编译 --> + <PropertyGroup> + <DefineConstants Condition="'$(TargetFramework)' == 'net6.0'">NETCORE;NET6;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'netcoreapp3.1'">NETCORE;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'netstandard2.1'">NETSTD;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'netstandard2.0'">NETSTD;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'net461'">NETFX;NET461;NET45;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'net40'">NETFX;NET40;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + <DefineConstants Condition="'$(TargetFramework)' == 'net20'">NETFX;NET20;$(DefineConstants);$(AdditionalConstants)</DefineConstants> + </PropertyGroup> + + <!-- Visual Studio 2022 --> + <!-- + <ItemGroup> + <PackageReference Condition="'$(TargetFramework)'=='net461'" Include="Microsoft.NETFramework.ReferenceAssemblies.net461" Version="1.0.2" PrivateAssets="all" /> + <PackageReference Condition="'$(TargetFramework)'=='net40'" Include="Microsoft.NETFramework.ReferenceAssemblies.net40" Version="1.0.2" PrivateAssets="all" /> + <PackageReference Condition="'$(TargetFramework)'=='net20'" Include="Microsoft.NETFramework.ReferenceAssemblies.net20" Version="1.0.2" PrivateAssets="all" /> + </ItemGroup> + --> + + <!-- <Import Sdk="Microsoft.NET.Sdk" Project="Sdk.targets" /> --> + +</Project> \ No newline at end of file diff --git a/Apewer/ArrayBuilder.cs b/Apewer/ArrayBuilder.cs index 710e9d4..6251fca 100644 --- a/Apewer/ArrayBuilder.cs +++ b/Apewer/ArrayBuilder.cs @@ -60,6 +60,24 @@ namespace Apewer /// <summary>当前的元素数量。</summary> public int Count { get => _count; } + /// <summary>指定元素在数组中的偏移位置。</summary> + public int IndexOf(T value) + { + var array = _array; + var length = _count; + if (length < 1) return -1; + for (var i = 0; i < length; i++) + { + var item = array[i]; + if (item == null) continue; + if (item.Equals(value)) return i; + } + return -1; + } + + /// <summary>已包含指定的元素。</summary> + public bool Contains(T value) => IndexOf(value) > -1; + /// <summary>添加元素。</summary> public void Add(T item) { diff --git a/Apewer/NetworkUtility.cs b/Apewer/NetworkUtility.cs index 503c0cf..196926d 100644 --- a/Apewer/NetworkUtility.cs +++ b/Apewer/NetworkUtility.cs @@ -383,7 +383,7 @@ namespace Apewer #region Port - private static List<int> ListActivePort(IPEndPoint[] endpoints) + private static int[] ListActivePort(IPEndPoint[] endpoints) { var list = new List<int>(endpoints.Length); foreach (var endpoint in endpoints) @@ -394,20 +394,14 @@ namespace Apewer } list.Sort(); list.Capacity = list.Count; - return list; + return list.ToArray(); } /// <summary>列出活动的 TCP 端口。</summary> - public static List<int> ListActiveTcpPort() - { - return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - } + public static int[] ListActiveTcpPort() => ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); /// <summary>列出活动的 UDP 端口。</summary> - public static List<int> ListActiveUdpPort() - { - return ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); - } + public static int[] ListActiveUdpPort() => ListActivePort(IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners()); #endregion diff --git a/Apewer/RuntimeUtility.cs b/Apewer/RuntimeUtility.cs index afa3a61..892bddf 100644 --- a/Apewer/RuntimeUtility.cs +++ b/Apewer/RuntimeUtility.cs @@ -534,7 +534,7 @@ namespace Apewer /// <param name="background">设置线程为后台线程。</param> /// <param name="try">忽略 Action 抛出的异常。</param> /// <remarks>对返回的 Thread 调用 Abort 可结束线程的执行。</remarks> - public static Thread StartThread(Action action, bool background = true, bool @try = false) + public static Thread StartThread(Action action, bool background = false, bool @try = false) { if (action == null) return null; var thread = new Thread(new ThreadStart(() => Invoke(action, @try))); diff --git a/Apewer/Source/Query.cs b/Apewer/Source/Query.cs index b8d489d..6a35933 100644 --- a/Apewer/Source/Query.cs +++ b/Apewer/Source/Query.cs @@ -225,18 +225,18 @@ namespace Apewer.Source } /// <summary>获取指定列的所有值,无效值不加入结果。</summary> - public T[] ReadColumn<T>(int column = 0, Func<object, T> formatter = null) => OrmHelper.Column(this, (r) => (formatter ?? GetValueFormatter<T>()).Invoke(Value(r, column))); + public T[] ReadColumn<T>(int column = 0, Func<object, T> formatter = null) => SourceUtility.Column(this, (r) => (formatter ?? GetValueFormatter<T>()).Invoke(Value(r, column))); /// <summary>获取指定列的所有值,无效值不加入结果。</summary> /// <exception cref="ArgumentNullException"></exception> - public T[] ReadColumn<T>(string column, Func<object, T> formatter = null) => OrmHelper.Column(this, (r) => (formatter ?? GetValueFormatter<T>()).Invoke(Value(r, column))); + public T[] ReadColumn<T>(string column, Func<object, T> formatter = null) => SourceUtility.Column(this, (r) => (formatter ?? GetValueFormatter<T>()).Invoke(Value(r, column))); /// <summary>获取指定列的所有值,无效值不加入结果。</summary> - public string[] ReadColumn(int column = 0) => OrmHelper.Column(this, (r) => this.Text(r, column)); + public string[] ReadColumn(int column = 0) => SourceUtility.Column(this, (r) => this.Text(r, column)); /// <summary>获取指定列的所有值,无效值不加入结果。</summary> /// <exception cref="ArgumentNullException"></exception> - public string[] ReadColumn(string column) => OrmHelper.Column(this, (r) => this.Text(r, column)); + public string[] ReadColumn(string column) => SourceUtility.Column(this, (r) => this.Text(r, column)); #endregion diff --git a/Apewer/Source/Record.cs b/Apewer/Source/Record.cs index b35f8b0..f286a5c 100644 --- a/Apewer/Source/Record.cs +++ b/Apewer/Source/Record.cs @@ -14,82 +14,34 @@ namespace Apewer.Source public abstract class Record : IRecord { - const int KeyLength = 32; - - private string _key = null; - private long _flag = 0; - /// <summary>记录主键,一般使用 GUID 的字符串形式。</summary> /// <remarks> /// <para>注:</para> /// <para>1. 默认长度为 32,需要修改长度时应该重写此属性;</para> /// <para>2. 带有 Independent 特性的模型不包含此属性。</para> /// </remarks> - [Column("_key", ColumnType.NVarChar, KeyLength)] - public virtual string Key { get { return _key; } set { _key = Compact(value, KeyLength); } } + [Column("_key", ColumnType.NVarChar, 32)] + public virtual string Key { get; set; } /// <summary>记录的标记,Int64 类型,区分记录的状态。</summary> /// <remarks>带有 Independent 特性的模型不包含此属性。</remarks> [Column("_flag", ColumnType.Integer)] - public long Flag { get { return _flag; } set { _flag = value; } } + public virtual long Flag { get; set; } /// <summary>重置 Key 属性的值。</summary> public virtual void ResetKey() => Key = TextUtility.Key(); /// <summary></summary> - public Record() => ResetKey(); - - #region static - - /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary> - public static List<Type> EnumerateTableTypes<T>() where T : IRecord => EnumerateTableTypes(typeof(T)); - - /// <summary>枚举带有 Table 特性的派生类型。</summary> - public static List<Type> EnumerateTableTypes(Type baseType) + public Record() { - if (baseType == null) return new List<Type>(); - var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - var list = new List<Type>(); - foreach (var a in assemblies) - { - var types = RuntimeUtility.GetTypes(a); - foreach (var t in types) - { - if (!EnumerateTableTypes(t, baseType)) continue; - if (list.Contains(t)) continue; - list.Add(t); - } - } - return list; + ResetKey(); + Flag = DefaultFlag; } - private static bool EnumerateTableTypes(Type type, Type @base) - { - if (type == null || @base == null) return false; - if (!type.IsAbstract) - { - if (RuntimeUtility.Contains<TableAttribute>(type, false)) - { - if (type.Equals(@base)) - { - return true; - } - else - { - if (RuntimeUtility.IsInherits(type, @base)) return true; ; - } - } - } - return false; - } + #region static - // 限制文本长度,并去除两边的空白字符。 - static string Compact(string text, int length = -1) - { - if (string.IsNullOrEmpty(text)) return ""; - if (length > 0 && text.Length > length) text = text.Substring(0, length); - return text.Trim(); - } + /// <summary>创建 Record 对象时的默认 Flag 值。</summary> + public static long DefaultFlag { get; set; } #endregion diff --git a/Apewer/Source/OrmHelper.cs b/Apewer/Source/SourceUtility.cs similarity index 92% rename from Apewer/Source/OrmHelper.cs rename to Apewer/Source/SourceUtility.cs index 159cfb1..dcbb987 100644 --- a/Apewer/Source/OrmHelper.cs +++ b/Apewer/Source/SourceUtility.cs @@ -9,7 +9,7 @@ namespace Apewer.Source { /// <summary>ORM 帮助程序。</summary> - public static class OrmHelper + public static class SourceUtility { #region As @@ -319,18 +319,6 @@ namespace Apewer.Source var tableName = tableStructure.Name; if (string.IsNullOrEmpty(tableName)) return new Result<string[]>("表名无效。"); - // var keyName = null as string; - // foreach (var column in tableStructure.Columns) - // { - // if (column.Key == "Key") - // { - // keyName = column.Value.Field; - // break; - // } - // } - // if (string.IsNullOrEmpty(keyName)) return Result<List<string>>.Error("主键字段无效。"); - - // var sql = sqlGetter(tableName, keyName); var sql = sqlGetter(tableName); var query = null as IQuery; try @@ -413,6 +401,39 @@ namespace Apewer.Source return setted; } + /// <summary>枚举带有 Table 特性的 <typeparamref name="T"/> 派生类型。</summary> + public static Type[] EnumerateRecords<T>() where T : IRecord => EnumerateRecords(typeof(T)); + + /// <summary>枚举带有 Table 特性的派生类型。</summary> + /// <exception cref="ArgumentNullException"></exception> + public static Type[] EnumerateRecords(Type baseType) + { + if (baseType == null) throw new ArgumentNullException(nameof(baseType)); + var assemblies = AppDomain.CurrentDomain.GetAssemblies(); + var builder = new ArrayBuilder<Type>(); + foreach (var assembly in assemblies) + { + var types = RuntimeUtility.GetTypes(assembly); + foreach (var type in types) + { + if (!EnumerateRecords(type, baseType)) continue; + if (builder.Contains(type)) continue; + builder.Add(type); + } + } + return builder.Export(); + } + + static bool EnumerateRecords(Type type, Type @base) + { + if (type == null || @base == null) return false; + if (type.IsAbstract) return false; + if (!RuntimeUtility.Contains<TableAttribute>(type, false)) return false; + if (type.Equals(@base)) return true; + if (RuntimeUtility.IsInherits(type, @base)) return true; + return false; + } + #endregion }