Merge pull request #653 from luk355/dev
Modified Enumeration class to support private constructor
This commit is contained in:
commit
53479908a0
@ -1,12 +1,11 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure
|
||||||
{
|
{
|
||||||
using AspNetCore.Builder;
|
|
||||||
using global::Ordering.API.Extensions;
|
using global::Ordering.API.Extensions;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Ordering.Infrastructure;
|
using Ordering.Infrastructure;
|
||||||
@ -97,14 +96,9 @@
|
|||||||
return new CardType(id++, value.Trim('"').Trim());
|
return new CardType(id++, value.Trim('"').Trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<CardType> GetPredefinedCardTypes()
|
private IEnumerable<CardType> GetPredefinedCardTypes()
|
||||||
{
|
{
|
||||||
return new List<CardType>()
|
return Enumeration.GetAll<CardType>();
|
||||||
{
|
|
||||||
CardType.Amex,
|
|
||||||
CardType.Visa,
|
|
||||||
CardType.MasterCard
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<OrderStatus> GetOrderStatusFromFile(string contentRootPath, ILogger<OrderingContextSeed> log)
|
private IEnumerable<OrderStatus> GetOrderStatusFromFile(string contentRootPath, ILogger<OrderingContextSeed> log)
|
||||||
|
@ -1,54 +1,39 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.BuyerAggregate
|
||||||
{
|
{
|
||||||
|
/// <remarks>
|
||||||
|
/// Card type class should be marked as abstract with protected constructor to encapsulate known enum types
|
||||||
|
/// this is currently not possible as OrderingContextSeed uses this constructor to load cardTypes from csv file
|
||||||
|
/// </remarks>
|
||||||
public class CardType
|
public class CardType
|
||||||
: Enumeration
|
: Enumeration
|
||||||
{
|
{
|
||||||
public static CardType Amex = new CardType(1, "Amex");
|
public static CardType Amex = new AmexCardType();
|
||||||
public static CardType Visa = new CardType(2, "Visa");
|
public static CardType Visa = new VisaCardType();
|
||||||
public static CardType MasterCard = new CardType(3, "MasterCard");
|
public static CardType MasterCard = new MasterCardType();
|
||||||
|
|
||||||
protected CardType() { }
|
|
||||||
|
|
||||||
public CardType(int id, string name)
|
public CardType(int id, string name)
|
||||||
: base(id, name)
|
: base(id, name)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<CardType> List()
|
private class AmexCardType : CardType
|
||||||
{
|
{
|
||||||
return new[] { Amex, Visa, MasterCard };
|
public AmexCardType() : base(1, "Amex")
|
||||||
|
{ }
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CardType FromName(string name)
|
private class VisaCardType : CardType
|
||||||
{
|
{
|
||||||
var state = List()
|
public VisaCardType() : base(2, "Visa")
|
||||||
.SingleOrDefault(s => String.Equals(s.Name, name, StringComparison.CurrentCultureIgnoreCase));
|
{ }
|
||||||
|
|
||||||
if (state == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"Possible values for CardType: {String.Join(",", List().Select(s => s.Name))}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CardType From(int id)
|
private class MasterCardType : CardType
|
||||||
{
|
{
|
||||||
var state = List().SingleOrDefault(s => s.Id == id);
|
public MasterCardType() : base(3, "MasterCard")
|
||||||
|
{ }
|
||||||
if (state == null)
|
|
||||||
{
|
|
||||||
throw new ArgumentException($"Possible values for CardType: {String.Join(",", List().Select(s => s.Name))}");
|
|
||||||
}
|
|
||||||
|
|
||||||
return state;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
|
|||||||
|
|
||||||
public int Id { get; private set; }
|
public int Id { get; private set; }
|
||||||
|
|
||||||
protected Enumeration(){}
|
protected Enumeration()
|
||||||
|
{ }
|
||||||
|
|
||||||
protected Enumeration(int id, string name)
|
protected Enumeration(int id, string name)
|
||||||
{
|
{
|
||||||
@ -21,19 +22,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
|
|||||||
|
|
||||||
public override string ToString() => Name;
|
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 = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
|
||||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
|
|
||||||
|
|
||||||
foreach (var info in fields)
|
return fields.Select(f => f.GetValue(null)).Cast<T>();
|
||||||
{
|
|
||||||
var instance = new T();
|
|
||||||
var locatedValue = info.GetValue(instance) as T;
|
|
||||||
|
|
||||||
if (locatedValue != null)
|
|
||||||
yield return locatedValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Equals(object obj)
|
public override bool Equals(object obj)
|
||||||
@ -57,19 +50,19 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
|
|||||||
return absoluteDifference;
|
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);
|
var matchingItem = Parse<T, int>(value, "value", item => item.Id == value);
|
||||||
return matchingItem;
|
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);
|
var matchingItem = Parse<T, string>(displayName, "display name", item => item.Name == displayName);
|
||||||
return matchingItem;
|
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);
|
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
|
||||||
|
|
||||||
@ -79,6 +72,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
|
|||||||
return matchingItem;
|
return matchingItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CompareTo(object other) => Id.CompareTo(((Enumeration) other).Id);
|
public int CompareTo(object other) => Id.CompareTo(((Enumeration)other).Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user