前言:我相信你們在編寫代碼時常常會遇到各類狀態值,並且爲了不硬編碼和代碼中出現魔法數,一般咱們都會定義一個枚舉,來表示各類狀態值,直到我看到Java中這樣使用枚舉,我再想C# 中可不能夠這樣寫,今天就分享一下個人感悟。前端
(1)switch中使用枚舉git
public enum EmployeeType { Manager, Servant, AssistantToTheRegionalManager }
public class Employee { public EmployeeType Type { get; set; } public decimal Bonus { get; set; } }
static void ProcessBonus(Employee employee) { switch (employee.Type) { case EmployeeType.Manager: employee.Bonus = 1000m; break; case EmployeeType.Servant: employee.Bonus = 0.01m; break; case EmployeeType.AssistantToTheRegionalManager: employee.Bonus = 1.0m; break; default: throw new ArgumentOutOfRangeException(); } }
在沒有進某唐時我也是這樣的寫的,代碼很爛,違法了開閉原則,擴展性極差。在代碼規範中是不容許出現這樣的寫法的。對於上面的寫法可使用設計模式來重構。後面會繼續更新設計模式的文章。github
(2)類型轉換json
EnumTricks.IsVolumeHigh((Volume)27);
EnumTricks.High((int)Medium);
關於枚舉的MSDN文檔說了什麼:設計模式
「The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.api
(1)沒有類型安全安全
枚舉是簡單的值類型,能夠提供對無效值的保護,而且不會出現任何行爲。他們是有用的,由於他們是魔法數字的改進,但就是這樣。若是要約束類型可能的值,枚舉不必定能幫助您,由於仍然能夠提供無效類型。例如,此枚舉有三個值,默認狀況下將具備int類型。值範圍爲1到3。app
public enum Volume { Low = 1, Medium, High }
public static class EnumTricks { public static bool IsVolumeHigh(Volume volume) { var result = false; switch (volume) { case Volume.Low: Console.WriteLine("Volume is low."); break; case Volume.Medium: Console.WriteLine("Volume is medium."); break; case Volume.High: Console.WriteLine("Volume is high."); result = true; break; } return result; } }
static void Main(string[] args) { EnumTricks.IsVolumeHigh((Volume)27); Console.ReadKey(); }
public static class EnumTricks { public static bool IsVolumeHigh(Volume volume) { var result = false; switch (volume) { case Volume.Low: Console.WriteLine("Volume is low."); break; case Volume.Medium: Console.WriteLine("Volume is medium."); break; case Volume.High: Console.WriteLine("Volume is high."); result = true; break; } return result; } public static int EnumToInt(Volume volume) { return (int)volume; } public static Volume IntToEnum(int intValue) { return (Volume)intValue; } public static Volume StringToEnum(string stringValue) { return (Volume)Enum.Parse(typeof(Volume), stringValue); } public static int StringToInt(string stringValue) { var volume = StringToEnum(stringValue); return EnumToInt(volume); } public static string EnumToString(Volume volume) { return volume.ToString(); } }
這應該失敗,至少在運行時。它沒有。這真的很奇怪......在編譯期間或運行期間都不會檢測到錯誤的調用。你會以爲本身處於一個虛假的安全狀態。若是,咱們把傳進去的枚舉轉換爲string時,來看看這兩種狀況有什麼不一樣:async
我不知道你們平時在使用枚舉的時候,是否有意識檢查傳入的是不是有效的值。可使用Enum.IsDefined()來檢查int值是不是一個有效的值ide
解決方案:若是int值在枚舉值的定義範圍內,則使用Enum.IsDefined()查找。若是在範圍內,則返回True,不然返回False。
(2)轉化
您是否嘗試過將enum轉換爲int,int轉換爲enum,string轉換爲enum,將字符串轉換爲enum的int值?以下代碼:
public static class EnumTricks { public static bool IsVolumeHigh(Volume volume) { var result = false; switch (volume) { case Volume.Low: Console.WriteLine("Volume is low."); break; case Volume.Medium: Console.WriteLine("Volume is medium."); break; case Volume.High: Console.WriteLine("Volume is high."); result = true; break; } return result; } public static int EnumToInt(Volume volume) { return (int)volume; } public static Volume IntToEnum(int intValue) { return (Volume)intValue; } public static Volume StringToEnum(string stringValue) { return (Volume)Enum.Parse(typeof(Volume), stringValue); } public static int StringToInt(string stringValue) { var volume = StringToEnum(stringValue); return EnumToInt(volume); } }
是否是咱們平常的代碼中也有這樣的類型轉換代碼,不是說很差,只是類型轉換也是有性能損失的,若是能換中方式能夠一樣實現並且還避免以上問題豈不是更好,這樣咱們的代碼也更好維護和擴展,下面咱們經過使用枚舉類的方式來解決這個問題。
public class Enumeration: IComparable { private readonly int _value; private readonly string _displayName; protected Enumeration() { } protected Enumeration(int value, string displayName) { _value = value; _displayName = displayName; } public int Value { get { return _value; } } public string DisplayName { get { return _displayName; } } public override string ToString() { return DisplayName; } public static IEnumerable<T> GetAll<T>() where T : Enumeration, new() { var type = typeof(T); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (var info in fields) { var instance = new T(); var locatedValue = info.GetValue(instance) as T; if (locatedValue != null) { yield return locatedValue; } } } public override bool Equals(object obj) { var otherValue = obj as Enumeration; if (otherValue == null) { return false; } var typeMatches = GetType().Equals(obj.GetType()); var valueMatches = _value.Equals(otherValue.Value); return typeMatches && valueMatches; } public override int GetHashCode() { return _value.GetHashCode(); } public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue) { var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value); return absoluteDifference; } public static T FromValue<T>(int value) where T : Enumeration, new() { var matchingItem = parse<T, int>(value, "value", item => item.Value == value); return matchingItem; } public static T FromDisplayName<T>(string displayName) where T : Enumeration, new() { var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName); return matchingItem; } private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new() { var matchingItem = GetAll<T>().FirstOrDefault(predicate); if (matchingItem == null) { var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T)); throw new ApplicationException(message); } return matchingItem; } public int CompareTo(object other) { return Value.CompareTo(((Enumeration)other).Value); } }
public class Volume: Enumeration { private Volume() { throw new Exception(""); } private Volume(int value, string displayName): base(value, displayName) { } public static readonly Volume Low = new Volume(1, nameof(Low).ToLowerInvariant()); public static readonly Volume Medium = new Volume(2, nameof(Medium).ToLowerInvariant()); public static readonly Volume High = new Volume(3, nameof(High).ToLowerInvariant()); public static IEnumerable<Volume> List() => new[] { Low, Medium, High }; public static Volume From(int value) { var state = List().SingleOrDefault(s => s.Value == value); if (state == null) { throw new Exception($"Possible values for Volume: {String.Join(",", List().Select(s => s.Value))}"); } return state; } public static Volume FromName(string name) { var state = List() .SingleOrDefault(s => String.Equals(s.DisplayName, name, StringComparison.CurrentCultureIgnoreCase)); if (state == null) { throw new Exception($"Possible values for Volume: {String.Join(",", List().Select(s => s.DisplayName))}"); } return state; } }
static void Main(string[] args) { //EnumTricks.IsVolumeHigh((Volume)27); //var tmp = Enum.IsDefined(typeof(Volume), 3); //var str = EnumTricks.EnumToString((Volume)27); //var str2 = EnumTricks.EnumToString((Volume)3); //Console.WriteLine($"Volume 27:{str}"); //Console.WriteLine($"Volume 3:{str2}"); Console.WriteLine("------------------------------------------------------------"); Console.WriteLine(Volume.High.Value); Console.WriteLine(Volume.High.DisplayName); var volume = Volume.From(2); var volume2 = Volume.FromName("high"); var none = Volume.From(27); Console.ReadKey(); }
代碼以下:
Error文件下:
public interface ICommonError { int GetErrCode(); string GetErrMsg(); ICommonError SetErrMsg(string errMsg); }
public class Enumeration : IComparable { private readonly int _value; private readonly string _displayName; protected Enumeration() { } protected Enumeration(int value, string displayName) { _value = value; _displayName = displayName; } public int Value { get { return _value; } } public string DisplayName { get { return _displayName; } } public override string ToString() { return DisplayName; } public static IEnumerable<T> GetAll<T>() where T : Enumeration, new() { var type = typeof(T); var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); foreach (var info in fields) { var instance = new T(); var locatedValue = info.GetValue(instance) as T; if (locatedValue != null) { yield return locatedValue; } } } public override bool Equals(object obj) { var otherValue = obj as Enumeration; if (otherValue == null) { return false; } var typeMatches = GetType().Equals(obj.GetType()); var valueMatches = _value.Equals(otherValue.Value); return typeMatches && valueMatches; } public override int GetHashCode() { return _value.GetHashCode(); } public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue) { var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value); return absoluteDifference; } public static T FromValue<T>(int value) where T : Enumeration, new() { var matchingItem = parse<T, int>(value, "value", item => item.Value == value); return matchingItem; } public static T FromDisplayName<T>(string displayName) where T : Enumeration, new() { var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == displayName); return matchingItem; } private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new() { var matchingItem = GetAll<T>().FirstOrDefault(predicate); if (matchingItem == null) { var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T)); throw new ApplicationException(message); } return matchingItem; } public int CompareTo(object other) { return Value.CompareTo(((Enumeration)other).Value); } }
public class EmBusinessError : Enumeration, ICommonError { private int errCode; private String errMsg; public static readonly EmBusinessError parameterValidationError = new EmBusinessError(10001, "參數不合法"); private EmBusinessError() { throw new Exception("私有構造函數不能調用"); } private EmBusinessError(int value, string displayName) : base(value, displayName) { this.errCode = value; this.errMsg = displayName; } public int GetErrCode() { return this.errCode; } public string GetErrMsg() { return this.errMsg; } public void SetErrCode(int errCode) { this.errCode = errCode; } public ICommonError SetErrMsg(string errMsg) { this.errMsg = errMsg; return this; } }
//包裝器業務異常類實現 public class BusinessException : Exception, ICommonError { private ICommonError commonError; //直接接收EmBusinessError的傳參用於構造業務異常 public BusinessException(ICommonError commonError):base() { this.commonError = commonError; } public BusinessException(ICommonError commonError, string errMsg):base() { this.commonError = commonError; this.commonError.SetErrMsg(errMsg); } public int GetErrCode() { return this.commonError.GetErrCode(); } public string GetErrMsg() { return this.commonError.GetErrMsg(); } public ICommonError SetErrMsg(string errMsg) { this.commonError.SetErrMsg(errMsg); return this; } public ICommonError GetCommonError() { return commonError; } }
異常中間件:
public class ExceptionHandlerMiddleWare { private readonly RequestDelegate next; /// <summary> /// /// </summary> /// <param name="next"></param> public ExceptionHandlerMiddleWare(RequestDelegate next) { this.next = next; } public async Task Invoke(HttpContext context) { try { await next(context); } catch (Exception ex) { await HandleExceptionAsync(context, ex); } } private static async Task HandleExceptionAsync(HttpContext context, Exception exception) { if (exception == null) return; await WriteExceptionAsync(context, exception).ConfigureAwait(false); } private static async Task WriteExceptionAsync(HttpContext context, Exception exception) { var response = context.Response; response.ContentType = "application/json;charset=utf-8"; var result = new CommonReturnType(); if (exception is BusinessException) { var businessException = (BusinessException)exception; var errModel = new { errCode= businessException.GetErrCode(), errMsg= businessException.GetErrMsg() }; result = CommonReturnType.Create(errModel, "fail"); } await response.WriteAsync(JsonConvert.SerializeObject(new { data = result.GetData(), status = result.GetStatus() }) ).ConfigureAwait(false); } }
Response文件夾:
public class CommonReturnType { //代表對應請求的返回處理結果 "success" 或 "fail" private string status; //若status=success,則data內返回前端須要的json數據 //若status=fail,則data內使用通用的錯誤碼格式 private object data; //定義一個通用的建立方法 public static CommonReturnType Create(object result) { return CommonReturnType.Create(result, "success"); } public static CommonReturnType Create(object result, string status) { CommonReturnType type = new CommonReturnType(); type.SetStatus(status); type.SetData(result); return type; } public string GetStatus() { return status; } public void SetStatus(string status) { this.status = status; } public object GetData() { return data; } public void SetData(object data) { this.data = data; } }
最後推薦一個類庫,這是我在Nuget上發現的枚舉類庫,地址:https://github.com/ardalis/SmartEnum
好了,先分享到這裏,但願對你有幫助和啓發。
參考資料:
(2)https://ardalis.com/enum-alternatives-in-c
感謝:張家華 提供的分享。
微軟已經爲咱們提供了一些封裝 傳送門: https://docs.microsoft.com/en-us/dotnet/api/system.enum?view=netcore-3.0&tdsourcetag=s_pctim_aiomsg
做者:郭崢
出處:http://www.cnblogs.com/runningsmallguo/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文連接。