Nice
2 years ago
15 changed files with 261 additions and 120 deletions
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Easy.Abstract; |
|||
public interface IPagingRequest |
|||
{ |
|||
/// <summary>
|
|||
/// 页面索引
|
|||
/// </summary>
|
|||
public int PageIndex { get; set; } |
|||
/// <summary>
|
|||
/// 页面大小
|
|||
/// </summary>
|
|||
public int PageSize { get; set; } |
|||
} |
@ -0,0 +1,23 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Easy.Abstract; |
|||
public interface IPagingSortingRequest : IPagingRequest |
|||
{ |
|||
/// <summary>
|
|||
/// Sorting information.
|
|||
/// 应包括排序字段和可选方向(ASC或DESC)
|
|||
/// 可以包含多个由逗号(,)分隔的字段。
|
|||
/// </summary>
|
|||
/// <example>
|
|||
/// 例子:
|
|||
/// "Name"
|
|||
/// "Name DESC"
|
|||
/// "Name ASC, Age DESC"
|
|||
/// "ClassName Name ASC, Age DESC"
|
|||
/// </example>
|
|||
public string Sorting { get; set; } |
|||
} |
@ -1,44 +0,0 @@ |
|||
using System.Diagnostics.CodeAnalysis; |
|||
|
|||
namespace Easy; |
|||
|
|||
public static class Check |
|||
{ |
|||
public static T NotNull<T>(T value, [NotNull] string parameterName) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
throw new ArgumentNullException(parameterName); |
|||
} |
|||
return value; |
|||
} |
|||
|
|||
public static T NotNull<T>(T value, [NotNull] string parameterName, string message) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
throw new ArgumentNullException(parameterName, message); |
|||
} |
|||
return value; |
|||
} |
|||
|
|||
public static string NotNullOrWhiteSpace(string value, [NotNull] string parameterName, int maxLength = int.MaxValue, int minLength = 0) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(value)) |
|||
{ |
|||
throw new ArgumentException($"{parameterName} can not be null, empty or white space!", parameterName); |
|||
} |
|||
|
|||
if (value.Length > maxLength) |
|||
{ |
|||
throw new ArgumentException($"{parameterName} length must be equal to or lower than {maxLength}!", parameterName); |
|||
} |
|||
|
|||
if (minLength > 0 && value.Length < minLength) |
|||
{ |
|||
throw new ArgumentException($"{parameterName} length must be equal to or bigger than {minLength}!", parameterName); |
|||
} |
|||
|
|||
return value; |
|||
} |
|||
} |
@ -1,18 +0,0 @@ |
|||
namespace Easy.Exceptions; |
|||
|
|||
/// <summary>
|
|||
/// 业务异常
|
|||
/// </summary>
|
|||
public class BusinessException : Exception |
|||
{ |
|||
/// <summary>
|
|||
/// 业务异常
|
|||
/// </summary>
|
|||
/// <param name="message">消息</param>
|
|||
public BusinessException(string message) |
|||
: base(message) |
|||
{ |
|||
|
|||
} |
|||
|
|||
} |
@ -1,20 +0,0 @@ |
|||
namespace Easy.Exceptions; |
|||
public class EasyException : Exception |
|||
{ |
|||
public EasyException() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public EasyException(string message) |
|||
: base(message) |
|||
{ |
|||
|
|||
} |
|||
|
|||
public EasyException(string message, Exception innerException) |
|||
: base(message, innerException) |
|||
{ |
|||
|
|||
} |
|||
} |
@ -1,21 +0,0 @@ |
|||
using Easy.Exceptions; |
|||
|
|||
namespace Easy; |
|||
|
|||
/// <summary>
|
|||
/// when 语句
|
|||
/// </summary>
|
|||
public class When |
|||
{ |
|||
/// <summary>
|
|||
/// 是
|
|||
/// </summary>
|
|||
/// <param name="isOk">是否</param>
|
|||
/// <param name="message">消息</param>
|
|||
/// <exception cref="BusinessException">服务异常会被中间件拦截</exception>
|
|||
public static void Is(bool isOk, string message) |
|||
{ |
|||
if (isOk) throw new BusinessException(message); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,35 @@ |
|||
namespace System |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Field)] |
|||
public class EnumDefineAttribute : Attribute |
|||
{ |
|||
/// <summary>
|
|||
/// 枚举值。
|
|||
/// </summary>
|
|||
public int Value { get; internal set; } |
|||
|
|||
/// <summary>
|
|||
/// 枚举描述。
|
|||
/// </summary>
|
|||
public string Description { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 枚举获取列表分组标识。
|
|||
/// </summary>
|
|||
public string[] Group { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 枚举获取列表排序号,不指定则采用枚举值排序。
|
|||
/// </summary>
|
|||
public int? Sort { get; set; } |
|||
|
|||
public EnumDefineAttribute() |
|||
{ |
|||
} |
|||
|
|||
public EnumDefineAttribute(string description) |
|||
{ |
|||
Description = description; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,174 @@ |
|||
using System.Collections; |
|||
using System.Reflection; |
|||
|
|||
namespace System |
|||
{ |
|||
public static class EnumExtension |
|||
{ |
|||
/// <summary>
|
|||
/// A T extension method to determines whether the object is equal to any of the provided values.
|
|||
/// </summary>
|
|||
/// <param name="this">The object to be compared.</param>
|
|||
/// <param name="values">The value list to compare with the object.</param>
|
|||
/// <returns>true if the values list contains the object, else false.</returns>
|
|||
public static bool In(this Enum @this, params Enum[] values) |
|||
{ |
|||
return Array.IndexOf(values, @this) >= 0; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据枚举值获得枚举。
|
|||
/// </summary>
|
|||
public static TEnum GetEnum<TEnum>(this int enValue) where TEnum : struct |
|||
{ |
|||
Type typeFromHandle = typeof(TEnum); |
|||
if (!typeFromHandle.IsEnum) |
|||
{ |
|||
throw new Exception("GetEnum<TEnum> 中的 TEnum 必须为枚举类型!"); |
|||
} |
|||
|
|||
return (TEnum)Enum.Parse(typeFromHandle, Convert.ToString(enValue)); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获得枚举值。
|
|||
/// </summary>
|
|||
public static int GetValue(this Enum en) |
|||
{ |
|||
return Convert.ToInt32(en); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获得枚举项定义的名称。
|
|||
/// </summary>
|
|||
/// <param name="en"></param>
|
|||
/// <returns></returns>
|
|||
public static string GetName(this Enum en) |
|||
{ |
|||
return Enum.GetName(en.GetType(), en); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获得枚举EnumDefineAttribute定义的Description内容。
|
|||
/// </summary>
|
|||
public static string GetDescription(this Enum en) |
|||
{ |
|||
EnumDefineAttribute enumDefineAttribute = GetEnumDefineAttribute(en); |
|||
return enumDefineAttribute.Description; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 根据枚举值获得EnumDefineAttribute定义的Description内容。
|
|||
/// </summary>
|
|||
public static string GetDescription<TEnum>(this int enValue) where TEnum : struct |
|||
{ |
|||
return (GetEnum<TEnum>(enValue) as Enum).GetDescription(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获得枚举的EnumDefineAttribute定义。
|
|||
/// </summary>
|
|||
public static EnumDefineAttribute GetEnumDefineAttribute(this Enum en) |
|||
{ |
|||
string name = Enum.GetName(en.GetType(), en); |
|||
FieldInfo field = en.GetType().GetField(name); |
|||
return Attribute.GetCustomAttribute(field, typeof(EnumDefineAttribute)) as EnumDefineAttribute; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 返回类型为 IEnumerable 的输入。
|
|||
/// </summary>
|
|||
static IEnumerable<T> AsEnumerable<T>() where T : struct |
|||
{ |
|||
Type typeFromHandle = typeof(T); |
|||
if (!typeFromHandle.IsEnum) |
|||
{ |
|||
throw new Exception("AsEnumerable<T> 中的 T 必须为枚举类型!"); |
|||
} |
|||
|
|||
return new Enum<T>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 返回枚举项的EnumDefineAttribute集合。
|
|||
/// </summary>
|
|||
public static List<EnumDefineAttribute> GetEnumDefines<T>(string groupName) where T : struct |
|||
{ |
|||
Type typeFromHandle = typeof(T); |
|||
if (!typeFromHandle.IsEnum) |
|||
{ |
|||
throw new Exception("GetEnumDefineAttributes<T> 中的 T 必须为枚举类型!"); |
|||
} |
|||
|
|||
List<EnumDefineAttribute> list = new List<EnumDefineAttribute>(); |
|||
EnumDefineAttribute eda = null; |
|||
foreach (var en in AsEnumerable<T>()) |
|||
{ |
|||
eda = GetEnumDefineAttribute(en as Enum); |
|||
eda.Value = Convert.ToInt32(en); |
|||
if (!eda.Sort.HasValue) eda.Sort = eda.Value; |
|||
|
|||
if (!string.IsNullOrEmpty(groupName) && !eda.Group.Contains(groupName)) |
|||
{ |
|||
continue; |
|||
} |
|||
else |
|||
{ |
|||
list.Add(eda); |
|||
} |
|||
} |
|||
|
|||
return list.OrderBy(o => o.Sort).ToList(); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// T 类型枚举列表
|
|||
/// </summary>
|
|||
/// <typeparam name="T">枚举类型</typeparam>
|
|||
public class Enum<T> : IEnumerable<T> |
|||
{ |
|||
#region IEnumerable<T> 成员
|
|||
|
|||
/// <summary>
|
|||
/// 返回一个循环访问集合的枚举数。
|
|||
/// </summary>
|
|||
/// <returns>可用于循环访问集合的 IEnumerator。</returns>
|
|||
public IEnumerator<T> GetEnumerator() |
|||
{ |
|||
foreach (T item in Enum.GetValues(typeof(T))) |
|||
{ |
|||
yield return item; |
|||
} |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region IEnumerable 成员
|
|||
|
|||
/// <summary>
|
|||
/// 返回一个循环访问集合的枚举数。
|
|||
/// </summary>
|
|||
/// <returns>可用于循环访问集合的 IEnumerator 。</returns>
|
|||
IEnumerator IEnumerable.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
|
|||
#endregion
|
|||
|
|||
#region IEnumerable<T> 成员
|
|||
|
|||
/// <summary>
|
|||
/// 返回一个循环访问集合的枚举数。
|
|||
/// </summary>
|
|||
/// <returns>
|
|||
/// <returns>可用于循环访问集合的 IEnumerator<T> 。</returns>
|
|||
IEnumerator<T> IEnumerable<T>.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
|
|||
#endregion
|
|||
} |
|||
} |
Loading…
Reference in new issue