OrderStatus and CardTypes as Enumerations
This commit is contained in:
parent
d5cd24a3b9
commit
6d72c7dda0
@ -1,4 +1,4 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -7,20 +7,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
|
|||||||
{
|
{
|
||||||
|
|
||||||
public class CardType
|
public class CardType
|
||||||
: Entity
|
: Enumeration
|
||||||
{
|
{
|
||||||
public static CardType Amex = new CardType(1, "Amex");
|
public static CardType Amex = new CardType(1, "Amex");
|
||||||
public static CardType Visa = new CardType(2, "Visa");
|
public static CardType Visa = new CardType(2, "Visa");
|
||||||
public static CardType MasterCard = new CardType(3, "MasterCard");
|
public static CardType MasterCard = new CardType(3, "MasterCard");
|
||||||
|
|
||||||
public string Name { get; private set; }
|
|
||||||
|
|
||||||
protected CardType() { }
|
protected CardType() { }
|
||||||
|
|
||||||
public CardType(int id, string name)
|
public CardType(int id, string name)
|
||||||
|
: base(id, name)
|
||||||
{
|
{
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<CardType> List()
|
public static IEnumerable<CardType> List()
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate
|
||||||
{
|
{
|
||||||
using Seedwork;
|
using Seedwork;
|
||||||
|
using SeedWork;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public class OrderStatus
|
public class OrderStatus
|
||||||
:Entity
|
: Enumeration
|
||||||
{
|
{
|
||||||
public string Name { get; private set; }
|
|
||||||
|
|
||||||
public static OrderStatus InProcess = new OrderStatus(1, nameof(InProcess).ToLowerInvariant());
|
public static OrderStatus InProcess = new OrderStatus(1, nameof(InProcess).ToLowerInvariant());
|
||||||
public static OrderStatus Shipped = new OrderStatus(2, nameof(Shipped).ToLowerInvariant());
|
public static OrderStatus Shipped = new OrderStatus(2, nameof(Shipped).ToLowerInvariant());
|
||||||
public static OrderStatus Canceled = new OrderStatus(3, nameof(Canceled).ToLowerInvariant());
|
public static OrderStatus Canceled = new OrderStatus(3, nameof(Canceled).ToLowerInvariant());
|
||||||
@ -19,9 +18,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
public OrderStatus(int id, string name)
|
public OrderStatus(int id, string name)
|
||||||
|
: base(id, name)
|
||||||
{
|
{
|
||||||
Id = id;
|
|
||||||
Name = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IEnumerable<OrderStatus> List()
|
public static IEnumerable<OrderStatus> List()
|
||||||
|
103
src/Services/Ordering/Ordering.Domain/SeedWork/Enumeration.cs
Normal file
103
src/Services/Ordering/Ordering.Domain/SeedWork/Enumeration.cs
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
|
||||||
|
{
|
||||||
|
public abstract class Enumeration : IComparable
|
||||||
|
{
|
||||||
|
public string Name { get; private set; }
|
||||||
|
|
||||||
|
public int Id { get; private set; }
|
||||||
|
|
||||||
|
protected Enumeration()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Enumeration(int id, string name)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
|
||||||
|
{
|
||||||
|
var type = typeof(T);
|
||||||
|
var fields = type.GetTypeInfo().GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
|
||||||
|
|
||||||
|
foreach (var info in fields)
|
||||||
|
{
|
||||||
|
var instance = new T();
|
||||||
|
var locatedValue = info.GetValue(instance) as T;
|
||||||
|
|
||||||
|
if (locatedValue != null)
|
||||||
|
{
|
||||||
|
yield return locatedValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Equals(object obj)
|
||||||
|
{
|
||||||
|
var otherValue = obj as Enumeration;
|
||||||
|
|
||||||
|
if (otherValue == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var typeMatches = GetType().Equals(obj.GetType());
|
||||||
|
var valueMatches = Id.Equals(otherValue.Id);
|
||||||
|
|
||||||
|
return typeMatches && valueMatches;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override int GetHashCode()
|
||||||
|
{
|
||||||
|
return Id.GetHashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
|
||||||
|
{
|
||||||
|
var absoluteDifference = Math.Abs(firstValue.Id - secondValue.Id);
|
||||||
|
return absoluteDifference;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T FromValue<T>(int value) where T : Enumeration, new()
|
||||||
|
{
|
||||||
|
var matchingItem = parse<T, int>(value, "value", item => item.Id == value);
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
|
||||||
|
{
|
||||||
|
var matchingItem = parse<T, string>(displayName, "display name", item => item.Name == displayName);
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
|
||||||
|
{
|
||||||
|
var matchingItem = GetAll<T>().FirstOrDefault(predicate);
|
||||||
|
|
||||||
|
if (matchingItem == null)
|
||||||
|
{
|
||||||
|
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
|
||||||
|
|
||||||
|
throw new InvalidOperationException(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return matchingItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo(object other)
|
||||||
|
{
|
||||||
|
return Id.CompareTo(((Enumeration)other).Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user