updated CardType and Enumeration classes (#1528)

* updated CardType and Enumeration classes

* Update per IEvangelist's suggestion

* Update src/Services/Ordering/Ordering.Domain/SeedWork/Enumeration.cs

Co-authored-by: David Pine <david.pine@microsoft.com>

Co-authored-by: Sumit Ghosh <sumit.ghosh@neudesic.com>
Co-authored-by: David Pine <david.pine@microsoft.com>
This commit is contained in:
Matt Callahan 2021-04-07 23:16:11 -07:00 committed by GitHub
parent 5974647b52
commit a71b004897
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 19 deletions

View File

@ -9,9 +9,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
public class CardType public class CardType
: Enumeration : Enumeration
{ {
public static CardType Amex = new CardType(1, "Amex"); public static CardType Amex = new CardType(1, nameof(Amex));
public static CardType Visa = new CardType(2, "Visa"); public static CardType Visa = new CardType(2, nameof(Visa));
public static CardType MasterCard = new CardType(3, "MasterCard"); public static CardType MasterCard = new CardType(3, nameof(MasterCard));
public CardType(int id, string name) public CardType(int id, string name)
: base(id, name) : base(id, name)

View File

@ -11,27 +11,23 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
public int Id { get; private set; } public int Id { get; private set; }
protected Enumeration(int id, string name) protected Enumeration(int id, string name) => (Id, Name) = (id, name);
{
Id = id;
Name = name;
}
public override string ToString() => Name; public override string ToString() => Name;
public static IEnumerable<T> GetAll<T>() where T : Enumeration public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
{ typeof(T).GetFields(BindingFlags.Public |
var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly); BindingFlags.Static |
BindingFlags.DeclaredOnly)
return fields.Select(f => f.GetValue(null)).Cast<T>(); .Select(f => f.GetValue(null))
} .Cast<T>();
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var otherValue = obj as Enumeration; if (obj is not Enumeration otherValue)
{
if (otherValue == null)
return false; return false;
}
var typeMatches = GetType().Equals(obj.GetType()); var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id); var valueMatches = Id.Equals(otherValue.Id);