我有以下名为 QuoteDimensions 的类(我为了发布问题而对其进行了精简)。我想在基于 eid 的阀门创建对象时设置高度和宽度的范围。我制作了两个自定义范围属性类来从数据库中获取所需的最小值和最大值。此解决方案不起作用,因为我不能(或不知道如何)将运行时变量的值转换为常量。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public partial class QuoteDimension
{
private const int Eid=0; ///constants don't work this way
public QuoteDimension(int eid)
{
Eid= eid; ///constants don't work this way
//dostuff
}
[Required]
public int ID { get; set; }
[Required]
public int Quote_ID_FK { get; set; }
[Required]
[CustomRangeAttributeHeight(Eid)]
public Double Height { get; set; }
[Required]
[CustomRangeAttributeWidth(Eid)]
public Double Width { get; set; }
}
public class CustomRangeAttributeWidth : RangeAttribute
{
public static int Eid;
public CustomRangeAttributeWidth(int eid)
: base(getmin(), getmax())
{
Eid = eid;
}
private static double getmax()
{
QuoteDatabaseEntities db = new QuoteDatabaseEntities();
double temp = (from ed in db.EnclosureDimensions
where ed.Enclosure_ID_FK == Eid
select ed.WidthMax).Single();
consta
return temp;
}
private static double getmin()
{
QuoteDatabaseEntities db = new QuoteDatabaseEntities();
double temp = (from ed in db.EnclosureDimensions
where ed.Enclosure_ID_FK == Eid
select ed.WidthMin).Single();
return temp;
}
}
public class CustomRangeAttributeHeight : RangeAttribute
{
private static int Eid;
public CustomRangeAttributeHeight(int eid)
: base(getmin(), getmax())
{
Eid = eid;
}
private static double getmax()
{
QuoteDatabaseEntities db = new QuoteDatabaseEntities();
double temp = (from ed in db.EnclosureDimensions
where ed.Enclosure_ID_FK == Eid
select ed.HeightMax).Single();
return temp;
}
private static double getmin()
{
QuoteDatabaseEntities db = new QuoteDatabaseEntities();
double temp = (from ed in db.EnclosureDimensions
where ed.Enclosure_ID_FK == Eid
select ed.HeightMin).Single();
return temp;
}
}
我考虑创建一个自定义元数据提供程序,但我认为这不会解决我的问题。
由于我似乎无法以这种方式工作,我的另一个想法是创建一个 QuoteDimensions 接口,然后创建多个实现该接口的类并硬编码每个类中的范围。这种方式很臭,因为我不能只更改数据库中的 aa max 或 min 来影响网站。
任何想法或建议都会很有帮助。谢谢你。