Mercurial > altnet-hispano
changeset 84:ee4e699e4551
refactoring menor
author | nelopauselli |
---|---|
date | Wed, 25 May 2011 01:18:29 -0300 |
parents | 7e9ffde4022d |
children | 8a4135f019dd |
files | Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/DataAnnotationAndResolveByConventionModelMetadataProvider.cs Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/IResolverByConvention.cs Agendas/trunk/src/Agendas.Web/CustomModelMetadataProvider/ResolverThroughResource.cs Agendas/trunk/src/Agendas.Web/Global.asax.cs Agendas/trunk/src/Agendas.Web/ModelMetadataWithDefaultsProvider.asax.cs |
diffstat | 6 files changed, 112 insertions(+), 125 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <DependentUpon>Global.asax</DependentUpon> </Compile> <Compile Include="HttpContextIdentityProvider.cs" /> - <Compile Include="ModelMetadataWithDefaultsProvider.asax.cs" /> + <Compile Include="CustomModelMetadataProvider\DataAnnotationAndResolveByConventionModelMetadataProvider.cs" /> + <Compile Include="CustomModelMetadataProvider\IResolverByConvention.cs" /> + <Compile Include="CustomModelMetadataProvider\ResolverThroughResource.cs" /> <Compile Include="Models\AccountModels.cs" /> <Compile Include="Models\EventoModel.cs" /> <Compile Include="Models\PropuestaModel.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<Attribute> _attributeList; + + public DataAnnotationAndResolveByConventionModelMetadataProvider(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; + } + } +} \ No newline at end of file
--- /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
--- /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<T> : 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
--- 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<DataAnnotationResources>()); + ModelMetadataProviders.Current = + new DataAnnotationAndResolveByConventionModelMetadataProvider( + new ResolverThroughResource<DataAnnotationResources>()); AreaRegistration.RegisterAllAreas();
--- 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<ModelMetadata> GetMetadataForProperties(object container, Type containerType) - { - return _base.GetMetadataForProperties(container, containerType); - } - - public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName) - { - var modelMetadata = _base.GetMetadataForProperty(modelAccessor, containerType, propertyName); - return modelMetadata; - } - - public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType) - { - return _base.GetMetadataForType(modelAccessor, modelType); - } - } - - public class DataAnnotationsModelMetadataProviderWithAttributes : DataAnnotationsModelMetadataProvider - { - private readonly IModelMetadataResolver _modelMetadataResolver; - private List<Attribute> _attributeList; - - public DataAnnotationsModelMetadataProviderWithAttributes(IModelMetadataResolver modelMetadataResolver) - { - _modelMetadataResolver = modelMetadataResolver; - } - - 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 = _modelMetadataResolver.GetDisplayName(propertyName); - - if (string.IsNullOrWhiteSpace(modelMetadata.Description)) - modelMetadata.Description = _modelMetadataResolver.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 ?? _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<T> : 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