使用System.ComponentModel.DataAnnotations时禁止验证的任何方法?

时间:2011-01-21 13:36:48

标签: .net validation model data-annotations

这里的其他疑问不幸地回答了我的问题,我希望能在这个问题上找到天才。

目前的问题是我有一个域对象'GamePlayer',它有许多DataAnnotations,可以对从UI传入的数据进行模型验证。这些都是一种享受,并且完全可以测试等。

然而,我们有一个数据库,允许数据在一段时间内随意增长(大约660,000个用户帐户),我认为20,000到50,000个帐户目前违反了我们已经实施的规则在GamePlayer对象上。

所以,我希望能够做的仍然是使用数据注释,但能够在从数据库到模型填充的途中“禁用”它们。

我知道如果我使用ModelMetaData并将验证属性存储在另一个类中并将主类与[MetadataType(typeof(GamePlayerMetadata))]相关联,我可以这样做:

TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(GamePlayer), typeof(GamePlayerMetadata)), typeof(GamePlayer));

因此,执行反向(.RemoveProvider)删除规则 - 这是有效的,但我宁愿不使用ModelMetaData类,如果可以的话,将它们全部保存在一起?

希望不要太过分了:)

为任何帮助干杯, 特里

希望支持此案的其他信息:

[Serializable]
[MetadataType(typeof(AddressMetadata))]
public class Address
{
    [NonSerialized]
    private readonly AssociatedMetadataTypeTypeDescriptionProvider metadataAddress;

    public string House { get; private set; }
    public string SubPremises { get; private set; }
    public string Street { get; private set; }
    public string Town { get; private set; }
    public string County { get; private set; }
    public string Country { get; private set; }
    public string Postcode { get; private set; }


    internal Address()
    {
        metadataAddress             = new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Address), typeof(AddressMetadata));
        TypeDescriptor.AddProviderTransparent(metadataAddress, typeof(Address));
    }

    internal Address(AddressDto address) : this()
    {
        this.House          = address.House;
        this.SubPremises    = address.SubPremises;
        this.Street         = address.Street;
        this.Town           = address.Town;
        this.County         = address.County;
        this.Country        = address.Country;
        this.Postcode       = address.Postcode;
    }

    public Address(string house, string subPremises, string street, string town, string county, string country, string postcode) : this()
    {
        SetAddress(house, subPremises, street, town, county, country, postcode);
    }


    #region ------------------------------------------------------------------------------------- Methods --
    public bool IsSet()
    {
        return !((String.IsNullOrEmpty(House) && String.IsNullOrEmpty(this.SubPremises)) && String.IsNullOrEmpty(Postcode) && String.IsNullOrEmpty(Street));
    }

    /// <exception cref="ValidationException">Thrown when one of the fields doesn't match the business complexity rules</exception>
    public void SetAddress(string house, string subPremises, string street, string town, string county, string country, string postcode)
    {
        Validator.ValidateProperty(house, new ValidationContext(this, null, null) { MemberName = "House" });
        Validator.ValidateProperty(street, new ValidationContext(this, null, null) { MemberName = "Street" });
        Validator.ValidateProperty(postcode, new ValidationContext(this, null, null) { MemberName = "Postcode" });

        House = house;
        SubPremises = subPremises;
        Street = street;
        Town = town;
        County = county;
        Country = country;
        Postcode = postcode;
    }


    /// <exception cref="ValidationException">Thrown when one of the fields doesn't match the business complexity rules</exception>
    public void SetAddress(Address newAddress)
    {
        this.SetAddress(newAddress.House, newAddress.SubPremises, newAddress.Street, newAddress.Town, newAddress.County, newAddress.Country, newAddress.Postcode);
    }


    public string FirstLineOfAddress
    {
        get
        {
            return String.Format("{0}{1}, {2}",
                                    (String.IsNullOrEmpty(this.SubPremises) ? "" : this.SubPremises),
                                    (String.IsNullOrEmpty(this.SubPremises) ? this.House : ", " + this.House),
                                    this.Street
                );
        }
    }

    public override string ToString()
    {
        return String.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}",
                                House,
                                SubPremises,
                                Street,
                                Town,
                                County,
                                Postcode,
                                Country);
    }

    public AddressDto ToAddressDto()
    {
        return new AddressDto { House = this.House, SubPremises = this.SubPremises, Street = this.Street,
                                    Town = this.Town, County = this.County, Country = this.Country, Postcode = this.Postcode };
    }

    public void DisableValidation()
    {
        TypeDescriptor.RemoveProviderTransparent(metadataAddress, typeof(Address));
    }
    #endregion

}

为了简单起见,我们假设“House”上的AddressMetadata具有[Required]属性,但显然我也有邮政编码的正则表达式等。

让我们假设我在同一个程序集中工作,因此内部修饰符并不重要。

现在,如果我打电话:

Address stuff = new Address("", "", "", "", "", "", "");

然后验证(通过SetAddress)完全没问题。

如果我这样做:

Address stuff = new Address();
stuff.DisableValidation();
stuff.SetAddress("", "", "", "", "", "", "");

我希望禁用验证。

这有助于任何人提供更多信息吗?

0 个答案:

没有答案