view Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndConventionModelMetadataProvider.cs @ 120:c3573defd18f

Agregado de Fecha, Usuario y Successful en TrackLog Modificacion de publicador de blog para usar TrackLog Agregado de TrackLog en el test de crud de eventos Fix en publicador de twitter para que no twitee si ya se encuentra en TrackLog pero con Successful en true
author Jorge@Jorge-PC
date Fri, 24 Jun 2011 21:18:21 -0300
parents 8a4135f019dd
children c57ca21064f2
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;

namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider
{
	public class DataAnnotationAndConventionModelMetadataProvider : DataAnnotationsModelMetadataProvider
	{
		private readonly IResolverByConvention _resolverByConvention;
		private List<Attribute> _attributeList;

		public DataAnnotationAndConventionModelMetadataProvider(IResolverByConvention resolverByConvention)
		{
			_resolverByConvention = resolverByConvention;
		}

		protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName)
		{
			_attributeList = new List<Attribute>(attributes);

			var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

			if (propertyName != null)
			{
				if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName))
					modelMetadata.DisplayName = _resolverByConvention.GetDisplayName(propertyName);

				if (string.IsNullOrWhiteSpace(modelMetadata.Description))
					modelMetadata.Description = _resolverByConvention.GetDescription(propertyName);

				var validators = _attributeList.OfType<ValidationAttribute>();
				foreach (var validator in validators)
				{
					if (string.IsNullOrWhiteSpace(validator.ErrorMessage) &&
					    string.IsNullOrWhiteSpace(validator.ErrorMessageResourceName))
					{
						var resourceName = propertyName + validator.GetType().Name;
						if (resourceName.EndsWith("Attribute"))
							resourceName = resourceName.Substring(0, resourceName.Length - 9);
						var resourceType = validator.ErrorMessageResourceType ?? _resolverByConvention.ResourceType;
						var prop = resourceType.GetProperty(resourceName);
						if (prop != null)
						{
							validator.ErrorMessageResourceType = resourceType;
							validator.ErrorMessageResourceName = resourceName;
						}
					}
				}
			}

			return modelMetadata;
		}
	}
}