.net 在使用System.ComponentModel.DataAnnotations时,是否有禁用验证的方法?
不幸的是,这里的其他问题并没有回答我的问题,我希望在这个问题上有一点天才 目前的问题是我有一个域对象“GamePlayer”,它有许多DataAnnotation,用于对从UI传入的数据进行模型验证。这些工作是一种享受,并且是完全可测试的等等 然而,我们有一个数据库,其中的数据在几年内被允许随意增长,大约660000个用户帐户,我想说目前有20000到50000个帐户违反了我们在GamePlayer对象上制定的规则 因此,我希望能够做到的是仍然使用数据注释,但是能够在从数据库到模型填充的过程中“禁用”它们 我知道,如果我使用ModelMetaData,将验证属性存储在另一个类中,并将主类与[MetadataTypeOfGamePlayerMetadata]关联,我可以:.net 在使用System.ComponentModel.DataAnnotations时,是否有禁用验证的方法?,.net,validation,model,data-annotations,.net,Validation,Model,Data Annotations,不幸的是,这里的其他问题并没有回答我的问题,我希望在这个问题上有一点天才 目前的问题是我有一个域对象“GamePlayer”,它有许多DataAnnotation,用于对从UI传入的数据进行模型验证。这些工作是一种享受,并且是完全可测试的等等 然而,我们有一个数据库,其中的数据在几年内被允许随意增长,大约660000个用户帐户,我想说目前有20000到50000个帐户违反了我们在GamePlayer对象上制定的规则 因此,我希望能够做到的是仍然使用数据注释,但是能够在从数据库到模型填充的过程中“
TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(GamePlayer), typeof(GamePlayerMetadata)), typeof(GamePlayer));
因此,使用reverse.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
}
我希望验证被禁用
这有助于提供更多信息吗?如果我问了一个糟糕的问题,很高兴有人寻求澄清。基本上,当使用[Required]和[StringLength]验证属性时,只是想寻找一种方法来禁用某些情况下的验证。您能提供一些域对象的示例代码吗?我想看看您是如何处理属性设置器上的验证的。您是否在setters上显式调用Validator.ValidateProperty?在上面添加了一些澄清代码-希望能有所帮助?如果我问了一个糟糕的问题,很高兴有人寻求澄清。基本上,当使用[Required]和[StringLength]验证属性时,只是想寻找一种方法来禁用某些情况下的验证。您能提供一些域对象的示例代码吗?我想看看您是如何处理属性设置器上的验证的。您是否在setters上显式调用Validator.ValidateProperty?在上面添加了一些澄清代码-希望这会有所帮助?
Address stuff = new Address();
stuff.DisableValidation();
stuff.SetAddress("", "", "", "", "", "", "");