开发者

Any way to disable validation when using System.ComponentModel.DataAnnotations?

The other queries on here don't unfortunately answer my question, and I'm hoping for a stroke of genius on this one.

The problem currently is I have a domain object 'GamePlayer' which has a number of DataAnnotations to provide model validation on data incoming from the UI. These work a treat, and are fully testable etc.

We however have a database where the data has been allowed to grow haphazardly over a period of years (approximately 660,000 user accounts), and I'd say between 20,000 and 50,000 accounts currently violate the rules that we've put in place on the GamePlayer object.

So, what I hope to be able to do is still use Data Annotations, but be able to 'disable' them on the way from the DB up to the population of the model.

I know that if I use the ModelMetaData stuff and store the validation attributes in another class and associate the main class with [MetadataType(typeof(GamePlayerMetadata))], I can do:

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

and hence, do the reverse (.RemoveProvider) to remove the rules - this works, though I'd rather not use the ModelMetaData class and keep it all in one if I can?

Hopefully not waffled too much :)

Cheers for any help, Terry

Additional Information to hopefully support the case:

[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.Sub开发者_Python百科Premises),
                                    (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

}

Lets assume for simplicity's sake that AddressMetadata on 'House' has a [Required] attribute, though obviously I have regular expressions for postcode, etc. etc. too.

Lets assume I'm working in the same assembly so the internal modifier isn't important.

Now if I call:

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

then the validation kicks in (via SetAddress) no problem at all.

If I do:

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

I expect the validation to be disabled.

Does that help anyone provide more information?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜