PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 12ms app.codeStats 1ms

/Source/MvcFutures/Mvc/CachedDataAnnotationsModelMetadata.cs

#
C# | 246 lines | 201 code | 38 blank | 7 comment | 49 complexity | 143231b3533d6d23f0cede9d48222daf MD5 | raw file
Possible License(s): Apache-2.0
  1. namespace Microsoft.Web.Mvc {
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.DataAnnotations;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Reflection;
  8. using Microsoft.Web.Resources;
  9. public class CachedDataAnnotationsModelMetadata : CachedModelMetadata<CachedDataAnnotationsMetadataAttributes> {
  10. public CachedDataAnnotationsModelMetadata(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
  11. : base(prototype, modelAccessor) {
  12. }
  13. public CachedDataAnnotationsModelMetadata(CachedDataAnnotationsModelMetadataProvider provider, Type containerType, Type modelType, string propertyName, IEnumerable<Attribute> attributes)
  14. : base(provider, containerType, modelType, propertyName, new CachedDataAnnotationsMetadataAttributes(attributes.ToArray())) {
  15. }
  16. protected override bool ComputeConvertEmptyStringToNull() {
  17. return PrototypeCache.DisplayFormat != null
  18. ? PrototypeCache.DisplayFormat.ConvertEmptyStringToNull
  19. : base.ComputeConvertEmptyStringToNull();
  20. }
  21. protected override string ComputeDataTypeName() {
  22. if (PrototypeCache.DataType != null) {
  23. return PrototypeCache.DataType.ToDataTypeName();
  24. }
  25. if (PrototypeCache.DisplayFormat != null && !PrototypeCache.DisplayFormat.HtmlEncode) {
  26. return DataTypeUtil.HtmlTypeName;
  27. }
  28. return base.ComputeDataTypeName();
  29. }
  30. protected override string ComputeDescription() {
  31. return PrototypeCache.Display != null
  32. ? PrototypeCache.Display.GetDescription()
  33. : base.ComputeDescription();
  34. }
  35. protected override string ComputeDisplayFormatString() {
  36. return PrototypeCache.DisplayFormat != null
  37. ? PrototypeCache.DisplayFormat.DataFormatString
  38. : base.ComputeDisplayFormatString();
  39. }
  40. protected override string ComputeDisplayName() {
  41. string result = null;
  42. if (PrototypeCache.Display != null) {
  43. result = PrototypeCache.Display.GetName();
  44. }
  45. if (result == null && PrototypeCache.DisplayName != null) {
  46. result = PrototypeCache.DisplayName.DisplayName;
  47. }
  48. return result ?? base.ComputeDisplayName();
  49. }
  50. protected override string ComputeEditFormatString() {
  51. if (PrototypeCache.DisplayFormat != null && PrototypeCache.DisplayFormat.ApplyFormatInEditMode) {
  52. return PrototypeCache.DisplayFormat.DataFormatString;
  53. }
  54. return base.ComputeEditFormatString();
  55. }
  56. protected override bool ComputeHideSurroundingHtml() {
  57. return PrototypeCache.HiddenInput != null
  58. ? !PrototypeCache.HiddenInput.DisplayValue
  59. : base.ComputeHideSurroundingHtml();
  60. }
  61. protected override bool ComputeIsReadOnly() {
  62. if (PrototypeCache.Editable != null) {
  63. return !PrototypeCache.Editable.AllowEdit;
  64. }
  65. if (PrototypeCache.ReadOnly != null) {
  66. return PrototypeCache.ReadOnly.IsReadOnly;
  67. }
  68. return base.ComputeIsReadOnly();
  69. }
  70. protected override bool ComputeIsRequired() {
  71. return PrototypeCache.Required != null
  72. ? true
  73. : base.ComputeIsRequired();
  74. }
  75. protected override string ComputeNullDisplayText() {
  76. return PrototypeCache.DisplayFormat != null
  77. ? PrototypeCache.DisplayFormat.NullDisplayText
  78. : base.ComputeNullDisplayText();
  79. }
  80. protected override int ComputeOrder() {
  81. int? result = null;
  82. if (PrototypeCache.Display != null) {
  83. result = PrototypeCache.Display.GetOrder();
  84. }
  85. return result ?? base.ComputeOrder();
  86. }
  87. protected override string ComputeShortDisplayName() {
  88. return PrototypeCache.Display != null
  89. ? PrototypeCache.Display.GetShortName()
  90. : base.ComputeShortDisplayName();
  91. }
  92. protected override bool ComputeShowForDisplay() {
  93. return PrototypeCache.ScaffoldColumn != null
  94. ? PrototypeCache.ScaffoldColumn.Scaffold
  95. : base.ComputeShowForDisplay();
  96. }
  97. protected override bool ComputeShowForEdit() {
  98. return PrototypeCache.ScaffoldColumn != null
  99. ? PrototypeCache.ScaffoldColumn.Scaffold
  100. : base.ComputeShowForEdit();
  101. }
  102. protected override string ComputeSimpleDisplayText() {
  103. if (Model != null) {
  104. if (PrototypeCache.DisplayColumn != null && !String.IsNullOrEmpty(PrototypeCache.DisplayColumn.DisplayColumn)) {
  105. PropertyInfo displayColumnProperty = ModelType.GetProperty(PrototypeCache.DisplayColumn.DisplayColumn, BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance);
  106. ValidateDisplayColumnAttribute(PrototypeCache.DisplayColumn, displayColumnProperty, ModelType);
  107. object simpleDisplayTextValue = displayColumnProperty.GetValue(Model, new object[0]);
  108. if (simpleDisplayTextValue != null) {
  109. return simpleDisplayTextValue.ToString();
  110. }
  111. }
  112. }
  113. return base.ComputeSimpleDisplayText();
  114. }
  115. protected override string ComputeTemplateHint() {
  116. if (PrototypeCache.UIHint != null) {
  117. return PrototypeCache.UIHint.UIHint;
  118. }
  119. if (PrototypeCache.HiddenInput != null) {
  120. return "HiddenInput";
  121. }
  122. return base.ComputeTemplateHint();
  123. }
  124. protected override string ComputeWatermark() {
  125. return PrototypeCache.Display != null
  126. ? PrototypeCache.Display.GetPrompt()
  127. : base.ComputeWatermark();
  128. }
  129. private static void ValidateDisplayColumnAttribute(DisplayColumnAttribute displayColumnAttribute, PropertyInfo displayColumnProperty, Type modelType) {
  130. if (displayColumnProperty == null) {
  131. throw new InvalidOperationException(
  132. String.Format(
  133. CultureInfo.CurrentCulture,
  134. MvcResources.DataAnnotationsModelMetadataProvider_UnknownProperty,
  135. modelType.FullName, displayColumnAttribute.DisplayColumn
  136. )
  137. );
  138. }
  139. if (displayColumnProperty.GetGetMethod() == null) {
  140. throw new InvalidOperationException(
  141. String.Format(
  142. CultureInfo.CurrentCulture,
  143. MvcResources.DataAnnotationsModelMetadataProvider_UnreadableProperty,
  144. modelType.FullName, displayColumnAttribute.DisplayColumn
  145. )
  146. );
  147. }
  148. }
  149. }
  150. // This is an exact copy of the DataTypeUtil class in System.Web.Mvc.
  151. // This class can be removed once CachedDataAnnotationsModelMetadata gets moved into System.Web.Mvc
  152. internal static class DataTypeUtil {
  153. internal static readonly string CurrencyTypeName = DataType.Currency.ToString();
  154. internal static readonly string DateTypeName = DataType.Date.ToString();
  155. internal static readonly string DateTimeTypeName = DataType.DateTime.ToString();
  156. internal static readonly string DurationTypeName = DataType.Duration.ToString();
  157. internal static readonly string EmailAddressTypeName = DataType.EmailAddress.ToString();
  158. internal static readonly string HtmlTypeName = DataType.Html.ToString();
  159. internal static readonly string ImageUrlTypeName = DataType.ImageUrl.ToString();
  160. internal static readonly string MultiLineTextTypeName = DataType.MultilineText.ToString();
  161. internal static readonly string PasswordTypeName = DataType.Password.ToString();
  162. internal static readonly string PhoneNumberTypeName = DataType.PhoneNumber.ToString();
  163. internal static readonly string TextTypeName = DataType.Text.ToString();
  164. internal static readonly string TimeTypeName = DataType.Time.ToString();
  165. internal static readonly string UrlTypeName = DataType.Url.ToString();
  166. // This is a faster version of GetDataTypeName(). It internally calls ToString() on the enum
  167. // value, which can be quite slow because of value verification.
  168. internal static string ToDataTypeName(this DataTypeAttribute attribute, Func<DataTypeAttribute, Boolean> isDataType = null) {
  169. if (isDataType == null) {
  170. isDataType = t => t.GetType().Equals(typeof(DataTypeAttribute));
  171. }
  172. // GetDataTypeName is virtual, so this is only safe if they haven't derived from DataTypeAttribute.
  173. // However, if they derive from DataTypeAttribute, they can help their own perf by overriding GetDataTypeName
  174. // and returning an appropriate string without invoking the ToString() on the enum.
  175. if (isDataType(attribute)) {
  176. switch (attribute.DataType) {
  177. case DataType.Currency:
  178. return CurrencyTypeName;
  179. case DataType.Date:
  180. return DateTypeName;
  181. case DataType.DateTime:
  182. return DateTimeTypeName;
  183. case DataType.Duration:
  184. return DurationTypeName;
  185. case DataType.EmailAddress:
  186. return EmailAddressTypeName;
  187. case DataType.Html:
  188. return HtmlTypeName;
  189. case DataType.ImageUrl:
  190. return ImageUrlTypeName;
  191. case DataType.MultilineText:
  192. return MultiLineTextTypeName;
  193. case DataType.Password:
  194. return PasswordTypeName;
  195. case DataType.PhoneNumber:
  196. return PhoneNumberTypeName;
  197. case DataType.Text:
  198. return TextTypeName;
  199. case DataType.Time:
  200. return TimeTypeName;
  201. case DataType.Url:
  202. return UrlTypeName;
  203. }
  204. }
  205. return attribute.GetDataTypeName();
  206. }
  207. }
  208. }