Created
October 22, 2014 01:48
-
-
Save jejernig/f4dbd72729c4f75ea203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Common | |
{ | |
public class ChangeReason : DynamicEnum<ChangeReason> | |
{ | |
public ChangeReason() { } | |
public ChangeReason(string reasonCode, string shortDesc, string desc) | |
: base(reasonCode, shortDesc, desc) { } | |
static ChangeReason() | |
{ | |
Initialize(new List<ChangeReason> | |
{ | |
new ChangeReason("CODE", "SHORT", "LONG DESCRIPTION"), | |
}); | |
} | |
public static ChangeReason SomeProperty | |
{ | |
get { return ChangeReason.IntoDomainMapping["CODE"]; } | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Runtime.Serialization; | |
namespace Common | |
{ | |
public abstract class DynamicEnum<T> : IEquatable<T>, IComparable<T> | |
where T : DynamicEnum<T>, new() | |
{ | |
#region Instance | |
public string ReasonCode { get; private set; } | |
public string ShortDescription { get; private set; } | |
public string Description { get; private set; } | |
protected DynamicEnum() { } | |
protected DynamicEnum(string reasonCode, string shortDesc, string desc) | |
{ | |
ReasonCode = reasonCode; | |
ShortDescription = shortDesc; | |
Description = desc; | |
} | |
#region IEquatable<AreaStatus> Members | |
/// <summary> | |
/// Indicates whether the current object is equal to another object of the same type. | |
/// </summary> | |
/// <param name="other">An object to compare with this object.</param> | |
/// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns> | |
public bool Equals(T other) | |
{ | |
return ReasonCode == other.ReasonCode; | |
} | |
#endregion | |
/// <summary> | |
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance. | |
/// </summary> | |
/// <param name="obj">The <see cref="T:System.Object" /> to compare with the current <see cref="T:System.Object" />.</param> | |
/// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns> | |
public override bool Equals(object obj) | |
{ | |
return Equals(obj as T); | |
} | |
/// <summary> | |
/// Returns a hash code for this instance. | |
/// </summary> | |
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns> | |
public override int GetHashCode() | |
{ | |
return ReasonCode.GetHashCode(); | |
} | |
#region IComparable<AreaStatus> Members | |
/// <summary> | |
/// Compares the current object with another object of the same type. | |
/// </summary> | |
/// <param name="other">An object to compare with this object.</param> | |
/// <returns>A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other" /> parameter.Zero This object is equal to <paramref name="other" />. Greater than zero This object is greater than <paramref name="other" />.</returns> | |
public int CompareTo(T other) | |
{ | |
return ReasonId.CompareTo(other.ReasonId); | |
} | |
#endregion | |
#endregion | |
#region Class / Static | |
/// <summary> | |
/// Initializes static members of the <see cref="DynamicEnum{T}"/> class. | |
/// </summary> | |
static DynamicEnum() | |
{ | |
// Despite appearances, static methods are not really inherited by | |
// child classes. This means when the mapping fields below are accessed | |
// by an implementing class the CLR does not see it as a method on that | |
// class. In the event that the implementing class's static constructor | |
// hasn't been called yet, it will not be called at that point since | |
// technically no static method/property or instance of the implementing | |
// class has been used. Working around this by creating an instance here | |
// which causes the derived class's static constructor to be called | |
// beforehand. This could alternately be solved by moving the default 'enum' | |
// value initialization out of the implementing classes to the Global.asax | |
// where the database 'enum' values are optionally loaded. | |
new T(); | |
} | |
/// <summary> | |
/// Initializes the specified statuses. | |
/// </summary> | |
/// <param name="statuses">The statuses.</param> | |
public static void Initialize(IEnumerable<T> statuses) | |
{ | |
IntoDomainMapping = statuses.ToDictionary(x => x.ReasonCode, x => x); | |
IntoDBMapping = IntoDomainMapping.ToDictionary(x => x.Value, x => x.Key); | |
} | |
/// <summary> | |
/// Gets or sets the into domain mapping. | |
/// </summary> | |
/// <value>The into domain mapping.</value> | |
public static Dictionary<string, T> IntoDomainMapping { get; protected set; } | |
/// <summary> | |
/// Gets or sets the into database mapping. | |
/// </summary> | |
/// <value>The into database mapping.</value> | |
public static Dictionary<T, string> IntoDBMapping { get; protected set; } | |
#endregion | |
#region Operator Overloads | |
/// <summary> | |
/// Implements the ==. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator ==(DynamicEnum<T> s1, T s2) | |
{ | |
return s1.Equals(s2); | |
} | |
/// <summary> | |
/// Implements the !=. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator !=(DynamicEnum<T> s1, T s2) | |
{ | |
return !s1.Equals(s2); | |
} | |
/// <summary> | |
/// Implements the >. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator >(DynamicEnum<T> s1, T s2) | |
{ | |
return s1.CompareTo(s2) > 0; | |
} | |
/// <summary> | |
/// Implements the <. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator <(DynamicEnum<T> s1, T s2) | |
{ | |
return s1.CompareTo(s2) < 0; | |
} | |
/// <summary> | |
/// Implements the >=. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator >=(DynamicEnum<T> s1, T s2) | |
{ | |
return s1.CompareTo(s2) >= 0; | |
} | |
/// <summary> | |
/// Implements the <=. | |
/// </summary> | |
/// <param name="s1">The s1.</param> | |
/// <param name="s2">The s2.</param> | |
/// <returns>The result of the operator.</returns> | |
public static bool operator <=(DynamicEnum<T> s1, T s2) | |
{ | |
return s1.CompareTo(s2) <= 0; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment