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

Commit 5347990

Browse files
authored
Merge pull request #653 from luk355/dev
Modified Enumeration class to support private constructor
2 parents a2bc8aa + 92cb497 commit 5347990

File tree

3 files changed

+28
-56
lines changed

3 files changed

+28
-56
lines changed

src/Services/Ordering/Ordering.API/Infrastructure/OrderingContextSeed.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure
22
{
3-
using AspNetCore.Builder;
43
using global::Ordering.API.Extensions;
54
using Microsoft.AspNetCore.Hosting;
65
using Microsoft.EntityFrameworkCore;
76
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
87
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
9-
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
109
using Microsoft.Extensions.Logging;
1110
using Microsoft.Extensions.Options;
1211
using Ordering.Infrastructure;
@@ -97,14 +96,9 @@ private CardType CreateCardType(string value, ref int id)
9796
return new CardType(id++, value.Trim('"').Trim());
9897
}
9998

100-
private IEnumerable<CardType> GetPredefinedCardTypes()
99+
private IEnumerable<CardType> GetPredefinedCardTypes()
101100
{
102-
return new List<CardType>()
103-
{
104-
CardType.Amex,
105-
CardType.Visa,
106-
CardType.MasterCard
107-
};
101+
return Enumeration.GetAll<CardType>();
108102
}
109103

110104
private IEnumerable<OrderStatus> GetOrderStatusFromFile(string contentRootPath, ILogger<OrderingContextSeed> log)
Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,39 @@
11
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
52

63
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate
74
{
8-
5+
/// <remarks>
6+
/// Card type class should be marked as abstract with protected constructor to encapsulate known enum types
7+
/// this is currently not possible as OrderingContextSeed uses this constructor to load cardTypes from csv file
8+
/// </remarks>
99
public class CardType
1010
: Enumeration
1111
{
12-
public static CardType Amex = new CardType(1, "Amex");
13-
public static CardType Visa = new CardType(2, "Visa");
14-
public static CardType MasterCard = new CardType(3, "MasterCard");
15-
16-
protected CardType() { }
12+
public static CardType Amex = new AmexCardType();
13+
public static CardType Visa = new VisaCardType();
14+
public static CardType MasterCard = new MasterCardType();
1715

1816
public CardType(int id, string name)
1917
: base(id, name)
2018
{
21-
2219
}
2320

24-
public static IEnumerable<CardType> List()
21+
private class AmexCardType : CardType
2522
{
26-
return new[] { Amex, Visa, MasterCard };
23+
public AmexCardType() : base(1, "Amex")
24+
{ }
2725
}
2826

29-
public static CardType FromName(string name)
27+
private class VisaCardType : CardType
3028
{
31-
var state = List()
32-
.SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));
33-
34-
if (state == null)
35-
{
36-
throw new ArgumentException($"Possible values for CardType: {String.Join(",", List().Select(s => s.Name))}");
37-
}
38-
39-
return state;
29+
public VisaCardType() : base(2, "Visa")
30+
{ }
4031
}
4132

42-
public static CardType From(int id)
33+
private class MasterCardType : CardType
4334
{
44-
var state = List().SingleOrDefault(s => s.Id == id);
45-
46-
if (state == null)
47-
{
48-
throw new ArgumentException($"Possible values for CardType: {String.Join(",", List().Select(s => s.Name))}");
49-
}
50-
51-
return state;
35+
public MasterCardType() : base(3, "MasterCard")
36+
{ }
5237
}
5338
}
5439
}

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public abstract class Enumeration : IComparable
1111

1212
public int Id { get; private set; }
1313

14-
protected Enumeration(){}
14+
protected Enumeration()
15+
{ }
1516

1617
protected Enumeration(int id, string name)
1718
{
@@ -21,19 +22,11 @@ protected Enumeration(int id, string name)
2122

2223
public override string ToString() => Name;
2324

24-
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
25+
public static IEnumerable<T> GetAll<T>() where T : Enumeration
2526
{
26-
var type = typeof(T);
27-
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
27+
var fields = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
2828

29-
foreach (var info in fields)
30-
{
31-
var instance = new T();
32-
var locatedValue = info.GetValue(instance) as T;
33-
34-
if (locatedValue != null)
35-
yield return locatedValue;
36-
}
29+
return fields.Select(f => f.GetValue(null)).Cast<T>();
3730
}
3831

3932
public override bool Equals(object obj)
@@ -57,19 +50,19 @@ public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondV
5750
return absoluteDifference;
5851
}
5952

60-
public static T FromValue<T>(int value) where T : Enumeration, new()
53+
public static T FromValue<T>(int value) where T : Enumeration
6154
{
6255
var matchingItem = Parse<T, int>(value, "value", item => item.Id == value);
6356
return matchingItem;
6457
}
6558

66-
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
59+
public static T FromDisplayName<T>(string displayName) where T : Enumeration
6760
{
6861
var matchingItem = Parse<T, string>(displayName, "display name", item => item.Name == displayName);
6962
return matchingItem;
7063
}
7164

72-
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
65+
private static T Parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration
7366
{
7467
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
7568

@@ -79,6 +72,6 @@ public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondV
7972
return matchingItem;
8073
}
8174

82-
public int CompareTo(object other) => Id.CompareTo(((Enumeration) other).Id);
75+
public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id);
8376
}
8477
}

0 commit comments

Comments
 (0)