forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedModelMetadata.cs
More file actions
132 lines (118 loc) · 4.44 KB
/
Copy pathCachedModelMetadata.cs
File metadata and controls
132 lines (118 loc) · 4.44 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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.
namespace System.Web.Http.Metadata.Providers
{
// This class assumes that model metadata is expensive to create, and allows the user to
// stash a cache object that can be copied around as a prototype to make creation and
// computation quicker. It delegates the retrieval of values to getter methods, the results
// of which are cached on a per-metadata-instance basis.
//
// This allows flexible caching strategies: either caching the source of information across
// instances or caching of the actual information itself, depending on what the developer
// decides to put into the prototype cache.
public abstract class CachedModelMetadata<TPrototypeCache> : ModelMetadata
{
private bool _convertEmptyStringToNull;
private string _description;
private bool _isReadOnly;
private bool _isComplexType;
private bool _convertEmptyStringToNullComputed;
private bool _descriptionComputed;
private bool _isReadOnlyComputed;
private bool _isComplexTypeComputed;
// Constructor for creating real instances of the metadata class based on a prototype
protected CachedModelMetadata(CachedModelMetadata<TPrototypeCache> prototype, Func<object> modelAccessor)
: base(prototype.Provider, prototype.ContainerType, modelAccessor, prototype.ModelType, prototype.PropertyName, prototype.CacheKey)
{
PrototypeCache = prototype.PrototypeCache;
_isComplexType = prototype.IsComplexType;
_isComplexTypeComputed = true;
}
// Constructor for creating the prototype instances of the metadata class
protected CachedModelMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Type modelType, string propertyName, TPrototypeCache prototypeCache)
: base(provider, containerType, null /* modelAccessor */, modelType, propertyName)
{
PrototypeCache = prototypeCache;
}
public sealed override bool ConvertEmptyStringToNull
{
get
{
if (!_convertEmptyStringToNullComputed)
{
_convertEmptyStringToNull = ComputeConvertEmptyStringToNull();
_convertEmptyStringToNullComputed = true;
}
return _convertEmptyStringToNull;
}
set
{
_convertEmptyStringToNull = value;
_convertEmptyStringToNullComputed = true;
}
}
public sealed override string Description
{
get
{
if (!_descriptionComputed)
{
_description = ComputeDescription();
_descriptionComputed = true;
}
return _description;
}
set
{
_description = value;
_descriptionComputed = true;
}
}
public sealed override bool IsReadOnly
{
get
{
if (!_isReadOnlyComputed)
{
_isReadOnly = ComputeIsReadOnly();
_isReadOnlyComputed = true;
}
return _isReadOnly;
}
set
{
_isReadOnly = value;
_isReadOnlyComputed = true;
}
}
public sealed override bool IsComplexType
{
get
{
if (!_isComplexTypeComputed)
{
_isComplexType = ComputeIsComplexType();
_isComplexTypeComputed = true;
}
return _isComplexType;
}
}
protected TPrototypeCache PrototypeCache { get; set; }
protected virtual bool ComputeConvertEmptyStringToNull()
{
return base.ConvertEmptyStringToNull;
}
protected virtual string ComputeDescription()
{
return base.Description;
}
protected virtual bool ComputeIsReadOnly()
{
return base.IsReadOnly;
}
protected virtual bool ComputeIsComplexType()
{
return base.IsComplexType;
}
}
}