Skip to main content
 首页 » 编程设计

c#之对象实例 MVC4 的动态数据注释

2024年02月13日15zhujiabin

我有以下名为 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 接口(interface),然后创建多个类来实现该接口(interface)并在每个类中对范围进行硬编码。这种方式很糟糕,因为我不能仅仅更改数据库中的最大值或最小值来影响网站。

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

请您参考如下方法:

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

我会说这里最简单的方法是让 Eid 成为 readonly领域:

private readonly int Eid; 
 
public QuoteDimension(int eid) 
{ 
   Eid = eid; 
} 
 
public QuoteDimension(int eid) : this(0) 
{ 
} 

并在您的 QuoteDimension 类中实现 IValidatableObject:

public class TemplateNameModel : IValidatableObject 
{ 
    //definition of the class 
 
    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 
    { 
        // check for conditions here 
        yield return new ValidationResult("Validation message."); 
    } 
} 

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