# HG changeset patch # User nelopauselli # Date 1306297109 10800 # Node ID ee4e699e455131edf500ccb76ac19f26cd1ffc23 # Parent 7e9ffde4022d9333da86519cd9683117c28f902d refactoring menor diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj --- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Tue May 24 21:37:06 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Wed May 25 01:18:29 2011 -0300 @@ -69,7 +69,9 @@ Global.asax - + + + diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndResolveByConventionModelMetadataProvider.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndResolveByConventionModelMetadataProvider.cs Wed May 25 01:18:29 2011 -0300 @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Web.Mvc; + +namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider +{ + public class DataAnnotationAndResolveByConventionModelMetadataProvider : DataAnnotationsModelMetadataProvider + { + private readonly IResolverByConvention _resolverByConvention; + private List _attributeList; + + public DataAnnotationAndResolveByConventionModelMetadataProvider(IResolverByConvention resolverByConvention) + { + _resolverByConvention = resolverByConvention; + } + + protected override ModelMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName) + { + _attributeList = new List(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(); + 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; + } + } +} \ No newline at end of file diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/IResolverByConvention.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/IResolverByConvention.cs Wed May 25 01:18:29 2011 -0300 @@ -0,0 +1,11 @@ +using System; + +namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider +{ + public interface IResolverByConvention + { + string GetDisplayName(string propertyName); + string GetDescription(string propertyName); + Type ResourceType { get; } + } +} \ No newline at end of file diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/ResolverThroughResource.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/ResolverThroughResource.cs Wed May 25 01:18:29 2011 -0300 @@ -0,0 +1,36 @@ +using System; + +namespace AltNetHispano.Agendas.Web.CustomModelMetadataProvider +{ + public class ResolverThroughResource : IResolverByConvention + { + public string GetDisplayName(string propertyName) + { + return SearchResource(propertyName); + } + + public string GetDescription(string propertyName) + { + return SearchResource(propertyName + "Description"); + } + + public Type ResourceType + { + get { return typeof(T); } + } + + private static string SearchResource(string resourceName) + { + string displayName = null; + var resourceType = typeof(T); + var prop = resourceType.GetProperty(resourceName); + if (prop != null) + { + var value = prop.GetValue(resourceType, null); + displayName = value != null ? value.ToString() : resourceName; + } + return displayName; + } + + } +} \ No newline at end of file diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/Global.asax.cs --- a/Agendas/trunk/src/Agendas.Web/Global.asax.cs Tue May 24 21:37:06 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Global.asax.cs Wed May 25 01:18:29 2011 -0300 @@ -3,6 +3,7 @@ using AltNetHispano.Agendas.Domain; using AltNetHispano.Agendas.Factories; using AltNetHispano.Agendas.Resources.Properties; +using AltNetHispano.Agendas.Web.CustomModelMetadataProvider; namespace AltNetHispano.Agendas.Web { @@ -11,13 +12,13 @@ public class MvcApplication : System.Web.HttpApplication { - public static void RegisterGlobalFilters(GlobalFilterCollection filters) + private static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(AttributeFactory.GetNHibernateSessionPerAction()); } - public static void RegisterRoutes(RouteCollection routes) + private static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); @@ -31,7 +32,9 @@ protected void Application_Start() { - ModelMetadataProviders.Current = new ModelMetadataWithDefaultsProvider(new ModelMetadataLocalizable()); + ModelMetadataProviders.Current = + new DataAnnotationAndResolveByConventionModelMetadataProvider( + new ResolverThroughResource()); AreaRegistration.RegisterAllAreas(); diff -r 7e9ffde4022d -r ee4e699e4551 Agendas/trunk/src/Agendas.Web/ModelMetadataWithDefaultsProvider.asax.cs --- a/Agendas/trunk/src/Agendas.Web/ModelMetadataWithDefaultsProvider.asax.cs Tue May 24 21:37:06 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,121 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Web.Mvc; - -namespace AltNetHispano.Agendas.Web -{ - public class ModelMetadataWithDefaultsProvider : ModelMetadataProvider - { - private readonly DataAnnotationsModelMetadataProvider _base; - - public ModelMetadataWithDefaultsProvider(IModelMetadataResolver modelMetadataResolver) - { - _base = new DataAnnotationsModelMetadataProviderWithAttributes(modelMetadataResolver); - } - - public override IEnumerable GetMetadataForProperties(object container, Type containerType) - { - return _base.GetMetadataForProperties(container, containerType); - } - - public override ModelMetadata GetMetadataForProperty(Func modelAccessor, Type containerType, string propertyName) - { - var modelMetadata = _base.GetMetadataForProperty(modelAccessor, containerType, propertyName); - return modelMetadata; - } - - public override ModelMetadata GetMetadataForType(Func modelAccessor, Type modelType) - { - return _base.GetMetadataForType(modelAccessor, modelType); - } - } - - public class DataAnnotationsModelMetadataProviderWithAttributes : DataAnnotationsModelMetadataProvider - { - private readonly IModelMetadataResolver _modelMetadataResolver; - private List _attributeList; - - public DataAnnotationsModelMetadataProviderWithAttributes(IModelMetadataResolver modelMetadataResolver) - { - _modelMetadataResolver = modelMetadataResolver; - } - - protected override ModelMetadata CreateMetadata(IEnumerable attributes, Type containerType, Func modelAccessor, Type modelType, string propertyName) - { - _attributeList = new List(attributes); - - var modelMetadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); - - if (propertyName != null) - { - if (string.IsNullOrWhiteSpace(modelMetadata.DisplayName)) - modelMetadata.DisplayName = _modelMetadataResolver.GetDisplayName(propertyName); - - if (string.IsNullOrWhiteSpace(modelMetadata.Description)) - modelMetadata.Description = _modelMetadataResolver.GetDescription(propertyName); - - var validators = _attributeList.OfType(); - 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 ?? _modelMetadataResolver.ResourceType; - var prop = resourceType.GetProperty(resourceName); - if (prop != null) - { - validator.ErrorMessageResourceType = resourceType; - validator.ErrorMessageResourceName = resourceName; - } - } - } - } - - return modelMetadata; - } - } - - public interface IModelMetadataResolver - { - string GetDisplayName(string propertyName); - string GetDescription(string propertyName); - Type ResourceType { get; } - } - - public class ModelMetadataLocalizable : IModelMetadataResolver - { - public string GetDisplayName(string propertyName) - { - return SearchResource(propertyName); - } - - public string GetDescription(string propertyName) - { - return SearchResource(propertyName + "Description"); - } - - public Type ResourceType - { - get { return typeof(T); } - } - - private static string SearchResource(string resourceName) - { - string displayName = null; - var resourceType = typeof(T); - var prop = resourceType.GetProperty(resourceName); - if (prop != null) - { - var value = prop.GetValue(resourceType, null); - displayName = value != null ? value.ToString() : resourceName; - } - return displayName; - } - - } -} \ No newline at end of file