This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
ValidationVisitorModelWithValidatedProperties.cs
92 lines (77 loc) · 3.07 KB
/
ValidationVisitorModelWithValidatedProperties.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using BenchmarkDotNet.Attributes;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
namespace Microsoft.AspNetCore.Mvc.Performance
{
public class ValidationVisitorModelWithValidatedProperties : ValidationVisitorBenchmarkBase
{
public class Person
{
[Required]
public int Id { get; set; }
[Required]
[StringLength(20)]
public string Name { get; set; }
public string Description { get; set; }
public IList<Address> Address { get; set; }
}
public class Address
{
[Required]
public string Street { get; set; }
public string Street2 { get; set; }
public string Type { get; set; }
[Required]
public string Zip { get; set; }
}
public override object Model { get; } = new Person
{
Id = 10,
Name = "Test",
Address = new List<Address>
{
new Address
{
Street = "1 Microsoft Way",
Type = "Work",
Zip = "98056",
},
new Address
{
Street = "15701 NE 39th St",
Type = "Home",
Zip = "98052",
}
},
};
[Benchmark(Baseline = true, Description = "validation for a model with some validated properties - baseline", OperationsPerInvoke = Iterations)]
public void Visit_TypeWithSomeValidatedProperties_Baseline()
{
// Baseline for validating a typical model with some properties that require validation.
// This executes without the ModelMetadata.HasValidators optimization.
var validationVisitor = new ValidationVisitor(
ActionContext,
CompositeModelValidatorProvider,
ValidatorCache,
BaselineModelMetadataProvider,
new ValidationStateDictionary());
validationVisitor.Validate(BaselineModelMetadata, "key", Model);
}
[Benchmark(Description = "validation for a model with some validated properties", OperationsPerInvoke = Iterations)]
public void Visit_TypeWithSomeValidatedProperties()
{
// Validating a typical model with some properties that require validation.
// This executes with the ModelMetadata.HasValidators optimization.
var validationVisitor = new ValidationVisitor(
ActionContext,
CompositeModelValidatorProvider,
ValidatorCache,
ModelMetadataProvider,
new ValidationStateDictionary());
validationVisitor.Validate(ModelMetadata, "key", Model);
}
}
}