|
|
@ -11,7 +11,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork |
|
|
|
|
|
|
|
public int Id { get; private set; } |
|
|
|
|
|
|
|
protected Enumeration(){} |
|
|
|
protected Enumeration() |
|
|
|
{ } |
|
|
|
|
|
|
|
protected Enumeration(int id, string name) |
|
|
|
{ |
|
|
@ -21,19 +22,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork |
|
|
|
|
|
|
|
public override string ToString() => Name; |
|
|
|
|
|
|
|
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new() |
|
|
|
public static IEnumerable<T> GetAll<T>() where T : Enumeration |
|
|
|
{ |
|
|
|
var type = typeof(T); |
|
|
|
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); |
|
|
|
var fields = typeof(T).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; |
|
|
|
} |
|
|
|
return fields.Select(f => f.GetValue(null)).Cast<T>(); |
|
|
|
} |
|
|
|
|
|
|
|
public override bool Equals(object obj) |
|
|
@ -57,19 +50,19 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork |
|
|
|
return absoluteDifference; |
|
|
|
} |
|
|
|
|
|
|
|
public static T FromValue<T>(int value) where T : Enumeration, new() |
|
|
|
public static T FromValue<T>(int value) where T : Enumeration |
|
|
|
{ |
|
|
|
var matchingItem = Parse<T, int>(value, "value", item => item.Id == value); |
|
|
|
return matchingItem; |
|
|
|
} |
|
|
|
|
|
|
|
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new() |
|
|
|
public static T FromDisplayName<T>(string displayName) where T : Enumeration |
|
|
|
{ |
|
|
|
var matchingItem = Parse<T, string>(displayName, "display name", item => item.Name == displayName); |
|
|
|
return matchingItem; |
|
|
|
} |
|
|
|
|
|
|
|
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new() |
|
|
|
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration |
|
|
|
{ |
|
|
|
var matchingItem = GetAll<T>().FirstOrDefault(predicate); |
|
|
|
|
|
|
@ -79,6 +72,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork |
|
|
|
return matchingItem; |
|
|
|
} |
|
|
|
|
|
|
|
public int CompareTo(object other) => Id.CompareTo(((Enumeration) other).Id); |
|
|
|
public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id); |
|
|
|
} |
|
|
|
} |