Update Enumeration.cs

This commit is contained in:
Enrico Tirotta 2018-02-25 09:41:08 +01:00 committed by GitHub
parent bc955e657b
commit dbd5999e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@ -11,9 +11,7 @@ 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,10 +19,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
Name = name; Name = name;
} }
public override string ToString() public override string ToString() => Name;
{
return Name;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new() public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
{ {
@ -37,20 +32,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
var locatedValue = info.GetValue(instance) as T; var locatedValue = info.GetValue(instance) as T;
if (locatedValue != null) if (locatedValue != null)
{
yield return locatedValue; yield return locatedValue;
} }
} }
}
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var otherValue = obj as Enumeration; var otherValue = obj as Enumeration;
if (otherValue == null) if (otherValue == null)
{
return false; return false;
}
var typeMatches = GetType().Equals(obj.GetType()); var typeMatches = GetType().Equals(obj.GetType());
var valueMatches = Id.Equals(otherValue.Id); var valueMatches = Id.Equals(otherValue.Id);
@ -58,10 +49,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
return typeMatches && valueMatches; return typeMatches && valueMatches;
} }
public override int GetHashCode() public override int GetHashCode() => Id.GetHashCode();
{
return Id.GetHashCode();
}
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue) public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
{ {
@ -86,18 +74,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.SeedWork
var matchingItem = GetAll<T>().FirstOrDefault(predicate); var matchingItem = GetAll<T>().FirstOrDefault(predicate);
if (matchingItem == null) if (matchingItem == null)
{ throw new InvalidOperationException($"'{value}' is not a valid {description} in {typeof(T)}");
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
throw new InvalidOperationException(message);
}
return matchingItem; return matchingItem;
} }
public int CompareTo(object other) public int CompareTo(object other) => Id.CompareTo(((Enumeration) other).Id);
{
return Id.CompareTo(((Enumeration)other).Id);
}
} }
} }