gpt4 book ai didi

c# - 什么数据结构适合这个?

转载 作者:行者123 更新时间:2023-11-30 16:17:29 24 4
gpt4 key购买 nike

在代码中我想做这样的事情:

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)
};
}

但是每当我创建一个具有舞台的实体的新实例时,一个新的舞台就会被添加到数据库中。我只需要几个恒定的阶段。

舞台的使用:

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);

关于c# - 什么数据结构适合这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com