MaxLength属性取决于消耗类

时间:2015-03-12 18:35:48

标签: c# asp.net-mvc data-annotations maxlength

如何在许多类所使用的类上设置不同的MaxLength属性,其中每个消费类的MaxLength可能不同。

就我而言,我正在使用Entity Framework和MVC。我在BilingualStrings的实体数据模型中有一个复杂的类型,包括英语和法语字符串。我有很多实体都有一个双语字符串,它使用复杂类型将英语和法语映射到正确的数据库表/列。所以每个表都有两列,但实体有一个BilingualString类型的属性。单个表中的每个英语或法语的字段长度始终相同,但每个表的字段长度可以不同。

以下是我想要实现的简化示例:

public partial class BilingualString
{
    //[MaxLength(40)]  Cannot put MaxLength here because it would apply to all instances of BilingualString
    public string English { get; set; }
    public string French { get; set; }
}

public class ClassWithShortDescription
{
    //[MaxLength(20)] Cannot put MaxLength here because it does not makes sense.  It needs to be on each English and French properties.
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    //[MaxLength(200)] Cannot put MaxLength here because it does not makes sense.  It needs to be on each English and French properties.
    public BilingualString Description { get; set; }
}

2 个答案:

答案 0 :(得分:0)

经过一些阅读和大量的谷歌搜索,我得出结论,潜在的问题是数据属性设置的元数据是静态的。因此,即使由不同的类使用,嵌套类属性也不能具有不同的元数据。解决方案是将元数据放在消费类的属性上。创建应用于BilingualString类型的属性的自定义maxlength属性。

BilingualString的自定义MaxLength属性     public class MaxLengthBilingualStringAttribute:MaxLengthAttribute     {         public MaxLengthBilingualStringAttribute(int length):base(length)         {         }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        BilingualString bilingualString = new BilingualString();
        if (value.GetType() == typeof(EdmBilingualStringVarCharSingleLine))
        {
            var bs = value as EdmBilingualStringVarCharSingleLine;
            bilingualString.English = bs.English;
            bilingualString.French = bs.French;
        }
        else
            return new ValidationResult("MaxLengthBilingualString Attribute does cannot be used with this type.");
        if (bilingualString.English != null && bilingualString.English.Length > this.Length )
            return new ValidationResult(string.Format("The maximum field length of {0} has been exceed for {1} English.", this.Length, validationContext.DisplayName));
        if (bilingualString.French != null && bilingualString.French.Length > this.Length)
            return new ValidationResult(string.Format("The maximum field length of {0} has been exceed for {1} French.", this.Length, validationContext.DisplayName));
        return ValidationResult.Success;
    }
}

示例实施:

public partial class BilingualString
{
    public string English { get; set; }
    public string French { get; set; }
}

public class ClassWithShortDescription
{
    [MaxLengthBilingualString(20)]
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    [MaxLengthBilingualString(200)]
    public BilingualString Description { get; set; }
}

答案 1 :(得分:-2)

由于英语和法语在BilingualString中是硬编码的,您还可以硬编码两个属性而不是一个:

public class ClassWithShortDescription
{
    [MaxLengthForEnglish(20)]
    [MaxLengthForFrench(25)]
    public BilingualString Description { get; set; }
}

public class ClassWithLongDescription
{
    [MaxLengthForEnglish(200)]
    [MaxLengthForFrench(250)]
    public BilingualString Description { get; set; }
}