Self-assessment metadata using validation application block

Can I use the selfvalidation attribute with my validations located in the metadata? I am using Application Application Application Block.

+3
source share
2 answers

As I explained in my other answer, this is not supported out of the box. However, this can be achieved by connecting to the infrastructure using an injection of influence and replacing the existing implementation AttributeValidatorFactory. I wrote a post on my blog about how to do this: Block application for checking mixes using DataAnnotation: What is SelfValidation?

Hope this helps.

+2
source

VAB ( ). EntLib. , , , , [SelfValidation] , . , , , . .

- . :

[MetadataType(typeof(InvoiceMetaData))]
[HasSelfValidation]
public partial class Invoice
{
    public string Name{ get; set; }

    public int Price { get; set; }

    [SelfValidation]
    public void CustomValidate(ValidationResults results)
    {
        // Call into the meta data class
        InvoiceMetaData.Validate(this, results);
    }
}

public class InvoiceMetaData
{
    [StringLengthValidator(1, 10, Tag = "Name")]
    string Name { get; set; }

    [RangeValidator(0, RangeBoundaryType.Inclusive, 0,
        RangeBoundaryType.Ignore, Tag = "Price")]
    int Price { get; set; }

    public static void CustomValidate(Invoice instance,
        ValidationResults results)
    {
        results.AddResult(new ValidationResult("ErrorMessage1",
            instance, "", "", null));
    }
}

, , . VAB, , , 5.0 . , AttributeValidationFactory , . , .

+1

Source: https://habr.com/ru/post/1764213/


All Articles