Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndConventionModelMetadataProvider.cs @ 300:48ab8788bd19
#200: Patrocinadores: Agregar link al sitio web.
author | juanjose.montesdeocaarbos |
---|---|
date | Mon, 27 Feb 2012 14:45:12 -0300 |
parents | 8b0c62c255cd |
children |
rev | line source |
---|---|
85 | 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 | |
167 | 19 protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, |
20 Func<object> modelAccessor, Type modelType, string propertyName) | |
85 | 21 { |
22 _attributeList = new List<Attribute>(attributes); | |
23 | |
24 var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); | |
25 | |
26 if (propertyName != null) | |
27 { | |
167 | 28 // Si no hay displayName asignado, lo buscamos entre los recursos |
289
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
29 // Primero lo buscarmos por el Assembly FullName y el propertyName |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
30 // Si no lo encuentra, buscamos por el propertyName unicamente |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
31 if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName)) |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
32 { |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
33 if (containerType != null && containerType.FullName != null) |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
34 modelMetadata.DisplayName = |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
35 _resolverByConvention.GetDisplayName(string.Format("{0}{1}", containerType.FullName.Replace(".", string.Empty), |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
36 propertyName)); |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
37 } |
85 | 38 if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName)) |
39 modelMetadata.DisplayName = _resolverByConvention.GetDisplayName(propertyName); | |
40 | |
167 | 41 // Si no hay description asignado, lo buscamos entre los recursos |
289
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
42 // Primero lo buscarmos por el Assembly FullName y el propertyName |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
43 // Si no lo encuentra, buscamos por el propertyName unicamente |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
44 if (string.IsNullOrWhiteSpace(modelMetadata.Description)) |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
45 { |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
46 if (containerType != null && containerType.FullName != null) |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
47 modelMetadata.Description = |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
48 _resolverByConvention.GetDescription(string.Format("{0}{1}", containerType.FullName.Replace(".", string.Empty), |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
49 propertyName)); |
8b0c62c255cd
Se modifica DataAnnotationAndConventionModelMetadataProvider.cs para que lea Recursos en base al Assembly Fullname
juanjose.montesdeocaarbos
parents:
167
diff
changeset
|
50 } |
85 | 51 if (string.IsNullOrWhiteSpace(modelMetadata.Description)) |
52 modelMetadata.Description = _resolverByConvention.GetDescription(propertyName); | |
53 | |
167 | 54 // Auto UIHint por property name. |
55 // TODO: Sería ideal chequear si existe el template antes de asignarlo, ¿cómo? | |
56 if (string.IsNullOrWhiteSpace(modelMetadata.TemplateHint)) | |
57 modelMetadata.TemplateHint = propertyName; | |
152
c57ca21064f2
Auto UIHint por el nombre de la propiedad, hay un TODO para mejorarlo
Nelo@Guinea.neluz.int
parents:
85
diff
changeset
|
58 |
167 | 59 // Para cada validador sin texto asignado, tomamos asignamos los valores para ser buscados |
60 // entre los recursos | |
85 | 61 var validators = _attributeList.OfType<ValidationAttribute>(); |
62 foreach (var validator in validators) | |
63 { | |
64 if (string.IsNullOrWhiteSpace(validator.ErrorMessage) && | |
65 string.IsNullOrWhiteSpace(validator.ErrorMessageResourceName)) | |
66 { | |
67 var resourceName = propertyName + validator.GetType().Name; | |
68 if (resourceName.EndsWith("Attribute")) | |
69 resourceName = resourceName.Substring(0, resourceName.Length - 9); | |
70 var resourceType = validator.ErrorMessageResourceType ?? _resolverByConvention.ResourceType; | |
71 var prop = resourceType.GetProperty(resourceName); | |
72 if (prop != null) | |
73 { | |
74 validator.ErrorMessageResourceType = resourceType; | |
75 validator.ErrorMessageResourceName = resourceName; | |
76 } | |
77 } | |
78 } | |
79 } | |
80 | |
81 return modelMetadata; | |
82 } | |
83 } | |
84 } |