Skip to content

Commit d0abdc0

Browse files
Issue 18496 (#21677)
* update date, remove casual language * changes per suggestions * revert newline only change * revert newline only change * revert changes to maintain existing style found in eShopOnContainers * Apply suggestions from code review Co-authored-by: David Pine <[email protected]>
1 parent 2ddb032 commit d0abdc0

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

docs/architecture/microservices/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.md

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
---
22
title: Using Enumeration classes instead of enum types
33
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.
4-
ms.date: 10/08/2018
4+
ms.date: 11/25/2020
55
---
66
# Use enumeration classes instead of enum types
77

88
[Enumerations](../../../csharp/language-reference/builtin-types/enum.md) (or *enum types* for short) are a thin language wrapper around an integral type. You might want to limit their use to when you are storing one value from a closed set of values. Classification based on sizes (small, medium, large) is a good example. Using enums for control flow or more robust abstractions can be a [code smell](https://deviq.com/code-smells/). This type of usage leads to fragile code with many control flow statements checking values of the enum.
99

1010
Instead, you can create Enumeration classes that enable all the rich features of an object-oriented language.
1111

12-
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.
12+
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.
1313

1414
## Implement an Enumeration base class
1515

@@ -22,29 +22,23 @@ public abstract class Enumeration : IComparable
2222

2323
public int Id { get; private set; }
2424

25-
protected Enumeration(int id, string name)
26-
{
27-
Id = id;
28-
Name = name;
29-
}
25+
protected Enumeration(int id, string name) => (Id, Name) = (id, name);
3026

3127
public override string ToString() => Name;
3228

33-
public static IEnumerable<T> GetAll<T>() where T : Enumeration
34-
{
35-
var fields = typeof(T).GetFields(BindingFlags.Public |
36-
BindingFlags.Static |
37-
BindingFlags.DeclaredOnly);
38-
39-
return fields.Select(f => f.GetValue(null)).Cast<T>();
40-
}
29+
public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
30+
typeof(T).GetFields(BindingFlags.Public |
31+
BindingFlags.Static |
32+
BindingFlags.DeclaredOnly)
33+
.Select(f => f.GetValue(null))
34+
.Cast<T>();
4135

4236
public override bool Equals(object obj)
4337
{
44-
var otherValue = obj as Enumeration;
45-
46-
if (otherValue == null)
38+
if (obj is not Enumeration otherValue)
39+
{
4740
return false;
41+
}
4842

4943
var typeMatches = GetType().Equals(obj.GetType());
5044
var valueMatches = Id.Equals(otherValue.Id);
@@ -61,11 +55,12 @@ public abstract class Enumeration : IComparable
6155
You can use this class as a type in any entity or value object, as for the following `CardType` : `Enumeration` class:
6256

6357
```csharp
64-
public class CardType : Enumeration
58+
public class CardType
59+
: Enumeration
6560
{
66-
public static readonly CardType Amex = new CardType(1, "Amex");
67-
public static readonly CardType Visa = new CardType(2, "Visa");
68-
public static readonly CardType MasterCard = new CardType(3, "MasterCard");
61+
public static CardType Amex = new CardType(1, nameof(Amex));
62+
public static CardType Visa = new CardType(2, nameof(Visa));
63+
public static CardType MasterCard = new CardType(3, nameof(MasterCard));
6964

7065
public CardType(int id, string name)
7166
: base(id, name)

0 commit comments

Comments
 (0)