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

Commit

Permalink
updated CardType and Enumeration classes (#1528)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Apr 8, 2021
1 parent 5974647 commit a71b004
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
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

0 comments on commit a71b004

Please sign in to comment.