diff --git a/docs/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.md b/docs/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.md index 82f910c5a8c05..4bb4f2e3e9588 100644 --- a/docs/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.md +++ b/docs/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.md @@ -1,7 +1,7 @@ --- title: Using Enumeration classes instead of enum types description: .NET Microservices Architecture for Containerized .NET Applications | Lear how you can use enumeration classes, instead of enums, as a way to solve some limitations of the latter. -ms.date: 10/08/2018 +ms.date: 11/25/2020 --- # Use enumeration classes instead of enum types @@ -9,7 +9,7 @@ ms.date: 10/08/2018 Instead, you can create Enumeration classes that enable all the rich features of an object-oriented language. -However, this isn't a critical topic and in many cases, for simplicity, you can still use regular [enum types](../../../csharp/language-reference/builtin-types/enum.md) if that's your preference. Anyway, the use of enumeration classes is more related to business-related concepts. +However, this isn't a critical topic and in many cases, for simplicity, you can still use regular [enum types](../../../csharp/language-reference/builtin-types/enum.md) if that's your preference. The use of enumeration classes is more related to business-related concepts. ## Implement an Enumeration base class @@ -22,29 +22,23 @@ public abstract class Enumeration : IComparable public int Id { get; private set; } - protected Enumeration(int id, string name) - { - Id = id; - Name = name; - } + protected Enumeration(int id, string name) => (Id, Name) = (id, name); public override string ToString() => Name; - public static IEnumerable GetAll() where T : Enumeration - { - var fields = typeof(T).GetFields(BindingFlags.Public | - BindingFlags.Static | - BindingFlags.DeclaredOnly); - - return fields.Select(f => f.GetValue(null)).Cast(); - } + public static IEnumerable GetAll() where T : Enumeration => + typeof(T).GetFields(BindingFlags.Public | + BindingFlags.Static | + BindingFlags.DeclaredOnly) + .Select(f => f.GetValue(null)) + .Cast(); public override bool Equals(object obj) { - var otherValue = obj as Enumeration; - - if (otherValue == null) + if (obj is not Enumeration otherValue) + { return false; + } var typeMatches = GetType().Equals(obj.GetType()); var valueMatches = Id.Equals(otherValue.Id); @@ -61,11 +55,12 @@ public abstract class Enumeration : IComparable You can use this class as a type in any entity or value object, as for the following `CardType` : `Enumeration` class: ```csharp -public class CardType : Enumeration +public class CardType + : Enumeration { - public static readonly CardType Amex = new CardType(1, "Amex"); - public static readonly CardType Visa = new CardType(2, "Visa"); - public static readonly CardType MasterCard = new CardType(3, "MasterCard"); + public static CardType Amex = new CardType(1, nameof(Amex)); + public static CardType Visa = new CardType(2, nameof(Visa)); + public static CardType MasterCard = new CardType(3, nameof(MasterCard)); public CardType(int id, string name) : base(id, name)