1

我有以下名为 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 来影响网站。

任何想法或建议都会很有帮助。谢谢你。

4

1 回答 1

4

常量不能在运行时更改。事实上,它们的值已被解析,并且在编译过程中,每次出现的常量都被其值替换。这就是为什么您尝试做的事情是不可能的。

我想说,对你来说最简单的方法是创建Eid一个只读字段:

private readonly int Eid;

public QuoteDimension(int eid)
{
   Eid = eid;
}

public QuoteDimension(int eid) : this(0)
{
}

IValidatableObject在您的QuoteDimension班级中实施:

public class TemplateNameModel : IValidatableObject
{
    //definition of the class

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // check for conditions here
        yield return new ValidationResult("Validation message.");
    }
}

这可能需要将您的自定义属性重构为其他形式的验证器,但允许您在运行时更改它们的参数。

于 2013-07-11T14:14:14.063 回答