comparison Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndConventionModelMetadataProvider.cs @ 85:8a4135f019dd

cambio de nombre de una clase
author nelopauselli
date Wed, 25 May 2011 01:20:24 -0300
parents
children c57ca21064f2
comparison
equal deleted inserted replaced
84:ee4e699e4551 85:8a4135f019dd
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel.DataAnnotations;
4 using System.Linq;
5 using System.Web.Mvc;
6
7 namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider
8 {
9 public class DataAnnotationAndConventionModelMetadataProvider : DataAnnotationsModelMetadataProvider
10 {
11 private readonly IResolverByConvention _resolverByConvention;
12 private List<Attribute> _attributeList;
13
14 public DataAnnotationAndConventionModelMetadataProvider(IResolverByConvention resolverByConvention)
15 {
16 _resolverByConvention = resolverByConvention;
17 }
18
19 protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
20 {
21 _attributeList = new List<Attribute>(attributes);
22
23 var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);
24
25 if (propertyName != null)
26 {
27 if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName))
28 modelMetadata.DisplayName = _resolverByConvention.GetDisplayName(propertyName);
29
30 if (string.IsNullOrWhiteSpace(modelMetadata.Description))
31 modelMetadata.Description = _resolverByConvention.GetDescription(propertyName);
32
33 var validators = _attributeList.OfType<ValidationAttribute>();
34 foreach (var validator in validators)
35 {
36 if (string.IsNullOrWhiteSpace(validator.ErrorMessage) &&
37 string.IsNullOrWhiteSpace(validator.ErrorMessageResourceName))
38 {
39 var resourceName = propertyName + validator.GetType().Name;
40 if (resourceName.EndsWith("Attribute"))
41 resourceName = resourceName.Substring(0, resourceName.Length - 9);
42 var resourceType = validator.ErrorMessageResourceType ?? _resolverByConvention.ResourceType;
43 var prop = resourceType.GetProperty(resourceName);
44 if (prop != null)
45 {
46 validator.ErrorMessageResourceType = resourceType;
47 validator.ErrorMessageResourceName = resourceName;
48 }
49 }
50 }
51 }
52
53 return modelMetadata;
54 }
55 }
56 }