Skip to content

Commit 6d72c7d

Browse files
committed
OrderStatus and CardTypes as Enumerations
1 parent d5cd24a commit 6d72c7d

3 files changed

Lines changed: 111 additions & 12 deletions

File tree

src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/CardType.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
1+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
55

66
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate
77
{
8-
8+
99
public class CardType
10-
: Entity
10+
: Enumeration
1111
{
1212
public static CardType Amex = new CardType(1, "Amex");
1313
public static CardType Visa = new CardType(2, "Visa");
1414
public static CardType MasterCard = new CardType(3, "MasterCard");
1515

16-
public string Name { get; private set; }
17-
1816
protected CardType() { }
1917

2018
public CardType(int id, string name)
19+
: base(id, name)
2120
{
22-
Id = id;
23-
Name = name;
21+
2422
}
2523

2624
public static IEnumerable<CardType> List()

src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/OrderStatus.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
22
{
33
using Seedwork;
4+
using SeedWork;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78

89
public class OrderStatus
9-
:Entity
10+
: Enumeration
1011
{
11-
public string Name { get; private set; }
12-
1312
public static OrderStatus InProcess = new OrderStatus(1, nameof(InProcess).ToLowerInvariant());
1413
public static OrderStatus Shipped = new OrderStatus(2, nameof(Shipped).ToLowerInvariant());
1514
public static OrderStatus Canceled = new OrderStatus(3, nameof(Canceled).ToLowerInvariant());
@@ -19,9 +18,8 @@ protected OrderStatus()
1918
}
2019

2120
public OrderStatus(int id, string name)
21+
: base(id, name)
2222
{
23-
Id = id;
24-
Name = name;
2523
}
2624

2725
public static IEnumerable<OrderStatus> List()
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
6+
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
7+
{
8+
public abstract class Enumeration : IComparable
9+
{
10+
public string Name { get; private set; }
11+
12+
public int Id { get; private set; }
13+
14+
protected Enumeration()
15+
{
16+
}
17+
18+
protected Enumeration(int id, string name)
19+
{
20+
Id = id;
21+
Name = name;
22+
}
23+
24+
public override string ToString()
25+
{
26+
return Name;
27+
}
28+
29+
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
30+
{
31+
var type = typeof(T);
32+
var fields = type.GetTypeInfo().GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
33+
34+
foreach (var info in fields)
35+
{
36+
var instance = new T();
37+
var locatedValue = info.GetValue(instance) as T;
38+
39+
if (locatedValue != null)
40+
{
41+
yield return locatedValue;
42+
}
43+
}
44+
}
45+
46+
public override bool Equals(object obj)
47+
{
48+
var otherValue = obj as Enumeration;
49+
50+
if (otherValue == null)
51+
{
52+
return false;
53+
}
54+
55+
var typeMatches = GetType().Equals(obj.GetType());
56+
var valueMatches = Id.Equals(otherValue.Id);
57+
58+
return typeMatches && valueMatches;
59+
}
60+
61+
public override int GetHashCode()
62+
{
63+
return Id.GetHashCode();
64+
}
65+
66+
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
67+
{
68+
var absoluteDifference = Math.Abs(firstValue.Id - secondValue.Id);
69+
return absoluteDifference;
70+
}
71+
72+
public static T FromValue<T>(int value) where T : Enumeration, new()
73+
{
74+
var matchingItem = parse<T, int>(value, "value", item => item.Id == value);
75+
return matchingItem;
76+
}
77+
78+
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
79+
{
80+
var matchingItem = parse<T, string>(displayName, "display name", item => item.Name == displayName);
81+
return matchingItem;
82+
}
83+
84+
private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
85+
{
86+
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
87+
88+
if (matchingItem == null)
89+
{
90+
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
91+
92+
throw new InvalidOperationException(message);
93+
}
94+
95+
return matchingItem;
96+
}
97+
98+
public int CompareTo(object other)
99+
{
100+
return Id.CompareTo(((Enumeration)other).Id);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)