|
@@ -0,0 +1,130 @@
|
|
1
|
+```
|
|
2
|
+<PackageReference Include="Ardalis.SmartEnum" Version="2.0.0" />
|
|
3
|
+```
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+```
|
|
8
|
+ /// <summary>
|
|
9
|
+ /// 枚举作为状态机
|
|
10
|
+ /// </summary>
|
|
11
|
+ public abstract class ReservationStatus : SmartEnum<ReservationStatus>
|
|
12
|
+ {
|
|
13
|
+ private ReservationStatus(string name, int value) : base(name, value) { }
|
|
14
|
+
|
|
15
|
+ public abstract bool CanTransitionTo(ReservationStatus next);
|
|
16
|
+
|
|
17
|
+ public static readonly ReservationStatus New = new NewStatus();
|
|
18
|
+ public static readonly ReservationStatus Accepted = new AcceptedStatus();
|
|
19
|
+ public static readonly ReservationStatus Paid = new PaidStatus();
|
|
20
|
+ public static readonly ReservationStatus Cancelled = new CancelledStatus();
|
|
21
|
+
|
|
22
|
+ private sealed class NewStatus : ReservationStatus
|
|
23
|
+ {
|
|
24
|
+ public NewStatus() : base("New", 0)
|
|
25
|
+ {
|
|
26
|
+ }
|
|
27
|
+
|
|
28
|
+ public override bool CanTransitionTo(ReservationStatus next) =>
|
|
29
|
+ next == ReservationStatus.Accepted || next == ReservationStatus.Cancelled;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ private sealed class AcceptedStatus : ReservationStatus
|
|
33
|
+ {
|
|
34
|
+ public AcceptedStatus() : base("Accepted", 1)
|
|
35
|
+ {
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public override bool CanTransitionTo(ReservationStatus next) =>
|
|
39
|
+ next == ReservationStatus.Paid || next == ReservationStatus.Cancelled;
|
|
40
|
+ }
|
|
41
|
+
|
|
42
|
+ private sealed class PaidStatus : ReservationStatus
|
|
43
|
+ {
|
|
44
|
+ public PaidStatus() : base("Paid", 2)
|
|
45
|
+ {
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ public override bool CanTransitionTo(ReservationStatus next) =>
|
|
49
|
+ next == ReservationStatus.Cancelled;
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ private sealed class CancelledStatus : ReservationStatus
|
|
53
|
+ {
|
|
54
|
+ public CancelledStatus() : base("Cancelled", 3)
|
|
55
|
+ {
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ public override bool CanTransitionTo(ReservationStatus next) =>
|
|
59
|
+ false;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ /// <summary>
|
|
66
|
+ /// 枚举可以初始化
|
|
67
|
+ /// </summary>
|
|
68
|
+ public sealed class TestEnum : SmartEnum<TestEnum>
|
|
69
|
+ {
|
|
70
|
+ private TestEnum(string name, int value) : base(name, value)
|
|
71
|
+ {
|
|
72
|
+
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ public static readonly TestEnum One = new TestEnum(nameof(One), 1);
|
|
76
|
+ public static readonly TestEnum Two = new TestEnum(nameof(Two), 2);
|
|
77
|
+ public static readonly TestEnum Three = new TestEnum(nameof(Three), 3);
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /// <summary>
|
|
81
|
+ /// 别名
|
|
82
|
+ /// </summary>
|
|
83
|
+ public sealed class TestEnum1 : SmartEnum<TestEnum1>
|
|
84
|
+ {
|
|
85
|
+ //真实的值通过构造函数来设定
|
|
86
|
+ private TestEnum1(string name, ushort value) : base(name, value) { }
|
|
87
|
+
|
|
88
|
+ /// <summary>
|
|
89
|
+ /// One看作是别名
|
|
90
|
+ /// </summary>
|
|
91
|
+ public static readonly TestEnum1 One = new TestEnum1("a", 1);
|
|
92
|
+ public static readonly TestEnum1 Two = new TestEnum1("b", 2);
|
|
93
|
+ public static readonly TestEnum1 Three = new TestEnum1("a", 3);
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ /// <summary>
|
|
97
|
+ /// 枚举也可以被继承
|
|
98
|
+ /// 也可以是抽象
|
|
99
|
+ /// </summary>
|
|
100
|
+ public abstract class EmployeeType : SmartEnum<EmployeeType>
|
|
101
|
+ {
|
|
102
|
+ private EmployeeType(string name, int value):base(name, value) { }
|
|
103
|
+ public abstract decimal BonusSize { get; }
|
|
104
|
+
|
|
105
|
+ public static readonly EmployeeType Manager = new ManagerType();
|
|
106
|
+ public static readonly EmployeeType Assistant = new AssistantType();
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+ private sealed class ManagerType : EmployeeType
|
|
110
|
+ {
|
|
111
|
+ public ManagerType():base("Manager",1)
|
|
112
|
+ {
|
|
113
|
+
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ public override decimal BonusSize => 10m;
|
|
117
|
+ }
|
|
118
|
+
|
|
119
|
+ private sealed class AssistantType : EmployeeType
|
|
120
|
+ {
|
|
121
|
+ public AssistantType() : base("Assistant",2)
|
|
122
|
+ {
|
|
123
|
+
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ public override decimal BonusSize => 20m;
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+```
|
|
130
|
+
|