在ASP.Net Core 中使用枚举类而不是枚举
前言:我相信大家在编写代码时经常会遇到各种状态值,而且为了避免硬编码和代码中出现魔法数,通常我们都会定义一个枚举,来表示各种状态值,直到我看到Java中这样使用枚举,我再想C# 中可不可以这样写,今天就分享一下我的感悟。
一、通常我们是这样使用枚举的
(1)switch中使用枚举
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();
}
}
在没有进某唐时我也是这样的写的,代码很烂,违法了开闭原则,扩展性极差。在代码规范中是不允许出现这样的写法的。对于上面的写法可以使用设计模式来重构。后面会继续更新设计模式的文章。
(2)类型转换
EnumTricks.IsVolumeHigh((Volume));
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.
(1)没有类型安全
枚举是简单的值类型,可以提供对无效值的保护,并且不会出现任何行为。他们是有用的,因为他们是魔法数字的改进,但就是这样。如果要约束类型可能的值,枚举不一定能帮助您,因为仍然可以提供无效类型。例如,此枚举有三个值,默认情况下将具有int类型。值范围为1到3。
public enum Volume
{
Low = ,
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)); 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时,来看看这两种情况有什么不同:

我不知道大家平时在使用枚举的时候,是否有意识检查传入的是否是有效的值。可以使用Enum.IsDefined()来检查int值是否是一个有效的值
解决方案:如果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(, nameof(Low).ToLowerInvariant());
public static readonly Volume Medium = new Volume(, nameof(Medium).ToLowerInvariant());
public static readonly Volume High = new Volume(, 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();
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(, "参数不合法"); 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/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
在ASP.Net Core 中使用枚举类而不是枚举的更多相关文章
- ASP.NET Core中的Startup类
ASP.NET Core程序要求有一个启动类.按照惯例,启动类的名字是 "Startup" .Startup类负责配置请求管道,处理应用程序的所有请求.你可以指定在Main方法中使 ...
- 扩展Asp.Net Core中的IdentityUser类
虽然Asp.Net Core.Identity提供了IdentityUser类,但是在有些情况下我们需要一些额外的用户信息,比如性别,年龄等,这时候就需要来扩展IdentityUser类以达到我们的需 ...
- ASP.NET Core 中文文档 第三章 原理(1)应用程序启动
原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...
- ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理
ServiceProvider最终提供的服务实例都是根据对应的ServiceDescriptor创建的,对于一个具体的ServiceDescriptor对象来说,如果它的ImplementationI ...
- ASP.Net Core 中使用Zookeeper搭建分布式环境中的配置中心系列一:使用Zookeeper.Net组件演示基本的操作
前言:马上要过年了,祝大家新年快乐!在过年回家前分享一篇关于Zookeeper的文章,我们都知道现在微服务盛行,大数据.分布式系统中经常会使用到Zookeeper,它是微服务.分布式系统中必不可少的分 ...
- 在Asp.net Core中使用中间件来管理websocket
介绍 ASP.NET Core SignalR是一个有用的库,可以简化Web应用程序中实时通信的管理.但是,我宁愿使用WebSockets,因为我想要更灵活,并且与任何WebSocket客户端兼容. ...
- Asp.Net Core 中IdentityServer4 实战之角色授权详解
一.前言 前几篇文章分享了IdentityServer4密码模式的基本授权及自定义授权等方式,最近由于改造一个网关服务,用到了IdentityServer4的授权,改造过程中发现比较适合基于Role角 ...
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- 在ASP.NET Core中使用百度在线编辑器UEditor
在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...
随机推荐
- 由浅入深,讲解 spring 实战详细使用——spring 源码篇
首先,这篇文章不会详细的深入底层源码,只是基于注解和配置来说说我们的 spring 的使用,别小看基础,保证有你没用过的注解和配置,走起. 我们先来建立一个 maven 项目,引入 spring 文件 ...
- IT兄弟连 Java语法教程 数据类型转换
类型转换是将一个值从一种类型更改为另一种类型的过程.例如,可以将String(字符串,字符串是Java中非常特殊的数据类型,字符串属于引用类型,但是可以像基本类型一样使用字面值赋值)类型数据“456” ...
- JS setInterval 循环定时器的使用 以及动态倒计时展示
例: var setTime = setInterval(function () { ff(); //每秒调用的方法函数 }, 1000); //1000毫秒为1秒 //可使用 clearInterv ...
- 为何我建议1-3年的Java程序员仔细看看这篇文章
此文的目的是为了督促自己去不断学习,让自己有更明确的方向去提升自己.以技能树为基础,以面试要点为大纲,我觉得比抓住什么看什么要更有目的,更能坚持下去.世界瞬息万变,我们要时刻准备着.时刻提高着自己,才 ...
- jsp页面科学计数法显示问题的解决办法
在JSP页面中返回的double类型的数值,如果数值过大,就会被显示成科学计数法. 这个问题通常出现在表单填写的input中,因此是必须要解决的问题. JSTL提供了标准的数值格式化标签,使用上非常简 ...
- 3、Hibernate的多表关联
一.数据库中的表关系: 一对一关系 一个人对应一张身份证,一张身份证对应一个人,一对一关系是最好理解的一种关系,在数据库建表的时候可以将人表的主键放置与身份证表里面,也可以将身份证表的主键放置于人表里 ...
- MSSQL查询当前登录进程以及执行状态
--当前连接进程declare @tempTable table (SPID INT,Status VARCHAR(255), Login VARCHAR(255),HostName VARCHAR( ...
- 机器学习常见的几种评价指标:精确率(Precision)、召回率(Recall)、F值(F-measure)、ROC曲线、AUC、准确率(Accuracy)
原文链接:https://blog.csdn.net/weixin_42518879/article/details/83959319 主要内容:机器学习中常见的几种评价指标,它们各自的含义和计算(注 ...
- java基础(21):异常
1. 异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 1.1 ...
- Java技巧——将前端的对象数组通过Json字符串传到后端并转换为对象集合
Java技巧——将前端的对象数组通过Json字符串传到后端并转换为对象集合 摘要:本文主要记录了如何将将前端的对象数组通过Json字符串传到后端,并在后端将Json字符串转换为对象集合. 前端代码 前 ...