Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

updated CardType and Enumeration classes #1528

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
public class CardType
: Enumeration
{
public static CardType Amex = new CardType(1, "Amex");
public static CardType Visa = new CardType(2, "Visa");
public static CardType MasterCard = new CardType(3, "MasterCard");
public static CardType Amex = new CardType(1, nameof(Amex));
public static CardType Visa = new CardType(2, nameof(Visa));
public static CardType MasterCard = new CardType(3, nameof(MasterCard));

public CardType(int id, string name)
: base(id, name)
Expand Down
28 changes: 12 additions & 16 deletions src/Services/Ordering/Ordering.Domain/SeedWork/Enumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,23 @@ public abstract class Enumeration : IComparable

public int Id { get; private set; }

protected Enumeration(int id, string name)
{
Id = id;
Name = name;
}
protected Enumeration(int id, string name) => (Id, Name) = (id, name);

public override string ToString() => Name;

public static IEnumerable<T> GetAll<T>() where T : Enumeration
{
var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);

return fields.Select(f => f.GetValue(null)).Cast<T>();
}

public static IEnumerable<T> GetAll<T>() where T : Enumeration =>
typeof(T).GetFields(BindingFlags.Public |
BindingFlags.Static |
BindingFlags.DeclaredOnly)
.Select(f => f.GetValue(null))
.Cast<T>();
public override bool Equals(object obj)
{
var otherValue = obj as Enumeration;

if (otherValue == null)
if (obj is not Enumeration otherValue)
{
return false;
}

var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id);
Expand Down