在代码中,我想做这样的事情:
item.Stage = Stage.Values.ONE;
其中Stage.Values.ONE代表一些预定义的阶段:
public class Stage
{
[Key]
public virtual int StageId { get; set; }
public string Name { get; set; }
public TimeSpan Span { get; set; }
}
我正在处理EF CodeFirst ...,并且要定义很多阶段。我不确定是否应该将数据存储在数据库中,还是在dbContext中,还是在什么中,但是我正在寻找最简单的实现。
我已经试过了:
我尝试了以下操作(定义两个常量):
public class Stage
{
[Key]
public virtual int StageId { get; set; }
public string Name { get; set; }
public TimeSpan Span { get; set; }
public static class Values
{
public static readonly Stage ONE = new Stage()
{
StageId = 0,
Name = "ONE",
Span = new TimeSpan(0, 0, 0)
};
public static readonly Stage TWO = new Stage()
{
StageId = 1,
Name = "TWO",
Span = new TimeSpan(0, 0, 10)
};
}
但是,每当我创建具有Stage的实体的新实例时,就会向数据库中添加一个新Stage。我只需要几个固定的阶段。
舞台的使用:
public class Side
{
public Side()
{
Stage = Stage.Values.ONE; // Adds new Stage to DB, when it should be a reference to the one I defined above
}
public virtual Stage Stage { get; set; }
}
最佳答案
它看起来有点像枚举,在成功使用之前,我已经多次使用“扩展枚举”模式。因为您在代码中引用这些值,所以也没有必要将它们存储在数据库中,但是如果需要的话,也可以。
此处详细描述了该技术:http://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/
基本上,您创建一个基类,该基类提供与枚举类似的许多服务,然后创建您从其继承的“枚举类”,并提供一堆静态实例,这些实例调用构造函数并提供许多您需要具有的属性。
为了避免链接腐烂,这里是要使用的基类(只需将整个类放入项目中的某个位置),然后向下滚动以查找自己的代码。
public abstract class Enumeration : IComparable
{
private readonly int _value;
private readonly string _displayName;
protected Enumeration()
{
}
protected Enumeration(int value, string displayName)
{
_value = value;
_displayName = displayName;
}
public int Value
{
get { return _value; }
}
public string DisplayName
{
get { return _displayName; }
}
public override string ToString()
{
return DisplayName;
}
public static IEnumerable<T> GetAll<T>() where T : Enumeration, new()
{
var type = typeof(T);
var fields = type.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 = _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, new()
{
var matchingItem = parse<T, int>(value, "value", item => item.Value == value);
return matchingItem;
}
public static T FromDisplayName<T>(string displayName) where T : Enumeration, new()
{
var matchingItem = parse<T, string>(displayName, "display name", item => item.DisplayName == 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 ApplicationException(message);
}
return matchingItem;
}
public int CompareTo(object other)
{
return Value.CompareTo(((Enumeration)other).Value);
}
}
现在,您的代码将如下所示:
public class Stage : Enumeration
{
public TimeSpan TimeSpan { get; private set; }
public static readonly Stage One
= new Stage (1, "Stage one", new TimeSpan(5));
public static readonly Stage Two
= new Stage (2, "Stage two", new TimeSpan(10));
public static readonly Stage Three
= new Stage (3, "Stage three", new TimeSpan(15));
private EmployeeType() { }
private EmployeeType(int value, string displayName, TimeSpan span) : base(value, displayName)
{
TimeSpan = span;
}
}
设置完成后,您只需将.Value存储在数据库中即可。恐怕我还没有在EF中完成此操作,但是在nHibernate中,告诉属性仅存储属性的“ .Value”是相当简单的,您可以在加载该值时将其连接起来打电话给:
Stage.FromValue<Stage>(intValue);