Skip to content

Instantly share code, notes, and snippets.

@mtherien
Last active December 14, 2018 15:29
Show Gist options
  • Select an option

  • Save mtherien/6043769 to your computer and use it in GitHub Desktop.

Select an option

Save mtherien/6043769 to your computer and use it in GitHub Desktop.
Creates a script that will maintain look-up table records for a database project. The resulting script can be used as a Post Deploy script in a database project, giving you the ability to create a working database from the project.Credits: http://www.codeproject.com/Articles/5598/Generating-INSERT-statements-in-SQL-Server http://www.codeproject.…
/// <summary>
/// Abstract class to use instead of an Enum class.
/// See https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/ for more information.
/// Also https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/microservice-ddd-cqrs-patterns/enumeration-classes-over-enum-types.
/// </summary>
/// <example>
/// public class MyEnumerationClass : Enumeration
/// {
/// private MyEnumerationClass(int value, string displayName, string customParameter)
/// : base(value, displayName)
/// {
/// CustomParameter = customParameter;
/// }
///
/// public string CustomParameter { get; }
///
/// public static MyEnumerationClass MyEnumValue1 => new MyEnumerationClass(1, "A test value", "Custom value 1");
///
/// public static MyEnumerationClass MyEnumValue2 => new MyEnumerationClass(2, "A test value", "Custom value 2");
/// }
///
/// public class Program
/// {
/// public static void Main()
/// {
/// var myValue = MyEnumerationClass.MyEnumValue1;
///
/// Console.WriteLine(myValue.CustomParameter);
///
/// var allValues = Enumeration.GetAll&lt;MyEnumerationClass&gt;();
///
/// foreach (var myEnumerationClass in allValues)
/// {
/// Console.WriteLine($"{myEnumerationClass.Value}: {myEnumerationClass.DisplayName} is ({myEnumerationClass.CustomParameter}");
/// }
/// }
/// }
/// </example>
public abstract class Enumeration : IComparable
{
protected Enumeration()
{
}
protected Enumeration(int value, string displayName)
{
Value = value;
DisplayName = displayName;
}
public int Value { get; }
public string DisplayName { get; }
public override string ToString()
{
return DisplayName;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration
{
var type = typeof(T);
var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
foreach (var info in properties)
{
var locatedValue = info.GetValue(null) 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 = Value.Equals(otherValue.Value);
return typeMatches && valueMatches;
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static int AbsoluteDifference(Enumeration firstValue, Enumeration secondValue)
{
var absoluteDifference = Math.Abs(firstValue.Value - secondValue.Value);
return absoluteDifference;
}
public static T FromValue<T>(int value) where T : Enumeration
{
var allItems = GetAll<T>();
var matchingItem = allItems.FirstOrDefault(i => i.Value == value);
if (matchingItem == null)
{
var message = $"'{value}' is not a valid in {typeof(T).FullName}";
throw new ApplicationException(message);
}
return matchingItem;
}
public static T FromDisplayName<T>(string displayName) where T : Enumeration
{
var allItems = GetAll<T>();
var matchingItem = allItems.FirstOrDefault(i => i.DisplayName == displayName);
if (matchingItem == null)
{
var message = $"'{displayName}' is not a valid display name in {typeof(T).FullName}";
throw new ApplicationException(message);
}
return matchingItem;
}
private static T parse<T, K>(K value, string description, Func<T, bool> predicate) where T : Enumeration, new()
{
T matchingItem = null;
foreach (var item in GetAll<T>())
{
if (predicate(item))
{
matchingItem = item;
break;
}
}
if (matchingItem == null)
{
var message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(T));
throw new ApplicationException(message);
}
return matchingItem;
}
public int CompareTo(object other)
{
return Value.CompareTo(((Enumeration)other).Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment