# HG changeset patch # User nelopauselli # Date 1306283826 10800 # Node ID 7e9ffde4022d9333da86519cd9683117c28f902d # Parent c8897b1c6f49aedcfd76a68fb7269ac7b500dc27 Implementando DataAnnotation desde recursos y por convención diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Resources/Agendas.Resources.csproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Resources/Agendas.Resources.csproj Tue May 24 21:37:06 2011 -0300 @@ -0,0 +1,64 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA} + Library + Properties + AltNetHispano.Agendas.Resources + AltNetHispano.Agendas.Resources + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + True + True + DataAnnotationResources.resx + + + + + PublicResXFileCodeGenerator + DataAnnotationResources.Designer.cs + + + + + \ No newline at end of file diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Resources/Properties/AssemblyInfo.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Resources/Properties/AssemblyInfo.cs Tue May 24 21:37:06 2011 -0300 @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Agendas.Resources")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Microsoft")] +[assembly: AssemblyProduct("Agendas.Resources")] +[assembly: AssemblyCopyright("Copyright © Microsoft 2011")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("f1f30a73-9935-4d8f-9a40-6a1fdcb9ff95")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Resources/Properties/DataAnnotationResources.Designer.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Resources/Properties/DataAnnotationResources.Designer.cs Tue May 24 21:37:06 2011 -0300 @@ -0,0 +1,126 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.225 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace AltNetHispano.Agendas.Resources.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + public class DataAnnotationResources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal DataAnnotationResources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AltNetHispano.Agendas.Resources.Properties.DataAnnotationResources", typeof(DataAnnotationResources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + public static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to debe ingresar la fecha del evento. + /// + public static string FechaRequired { + get { + return ResourceManager.GetString("FechaRequired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to debe ingresar el nombre del ponente. + /// + public static string PonenteRequired { + get { + return ResourceManager.GetString("PonenteRequired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Título. + /// + public static string Titulo { + get { + return ResourceManager.GetString("Titulo", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Título del evento, el mismo lo identifica entre todos los eventos. + /// + public static string TituloDescription { + get { + return ResourceManager.GetString("TituloDescription", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to debe ingresar el título. + /// + public static string TituloRequired { + get { + return ResourceManager.GetString("TituloRequired", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Url de la invitación. + /// + public static string UrlInvitacion { + get { + return ResourceManager.GetString("UrlInvitacion", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to la url ingresada no es válida. + /// + public static string UrlInvitacionUrl { + get { + return ResourceManager.GetString("UrlInvitacionUrl", resourceCulture); + } + } + } +} diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Resources/Properties/DataAnnotationResources.resx --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Resources/Properties/DataAnnotationResources.resx Tue May 24 21:37:06 2011 -0300 @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + debe ingresar la fecha del evento + + + debe ingresar el nombre del ponente + + + Título + + + Título del evento, el mismo lo identifica entre todos los eventos + + + debe ingresar el título + + + Url de la invitación + + + la url ingresada no es válida + + \ No newline at end of file diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj --- a/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Tue May 24 19:31:10 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Agendas.Web.csproj Tue May 24 21:37:06 2011 -0300 @@ -69,6 +69,7 @@ Global.asax + @@ -156,6 +157,10 @@ {306DDA8A-49A5-42E5-A639-A9D3D521865F} Agendas.Factories + + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA} + Agendas.Resources + diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Web/Global.asax.cs --- a/Agendas/trunk/src/Agendas.Web/Global.asax.cs Tue May 24 19:31:10 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Global.asax.cs Tue May 24 21:37:06 2011 -0300 @@ -2,6 +2,7 @@ using System.Web.Routing; using AltNetHispano.Agendas.Domain; using AltNetHispano.Agendas.Factories; +using AltNetHispano.Agendas.Resources.Properties; namespace AltNetHispano.Agendas.Web { @@ -30,6 +31,8 @@ protected void Application_Start() { + ModelMetadataProviders.Current = new ModelMetadataWithDefaultsProvider(new ModelMetadataLocalizable()); + AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Web/ModelMetadataWithDefaultsProvider.asax.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Web/ModelMetadataWithDefaultsProvider.asax.cs Tue May 24 21:37:06 2011 -0300 @@ -0,0 +1,121 @@ +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 diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs --- a/Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs Tue May 24 19:31:10 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Models/EventoModel.cs Tue May 24 21:37:06 2011 -0300 @@ -13,13 +13,13 @@ public class EventoNewModel { - [Required(ErrorMessage = "debe ingresar el título")] + [Required] public string Titulo { get; set; } - [Required(ErrorMessage = "debe ingresar el nombre del ponente")] + [Required] public string Ponente { get; set; } - [Required(ErrorMessage = "debe ingresar la fecha del evento")] + [Required] public DateTime Fecha { get; set; } [Url] @@ -31,13 +31,13 @@ [HiddenInput(DisplayValue = false)] public string Id { get; set; } - [Required(ErrorMessage = "debe ingresar el título")] + [Required] public string Titulo { get; set; } - [Required(ErrorMessage = "debe ingresar el nombre del ponente")] + [Required] public string Ponente { get; set; } - [Required(ErrorMessage = "debe ingresar la fecha del evento")] + [Required] public DateTime? Fecha { get; set; } [Url] diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.Web/Models/PropuestaModel.cs --- a/Agendas/trunk/src/Agendas.Web/Models/PropuestaModel.cs Tue May 24 19:31:10 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Web/Models/PropuestaModel.cs Tue May 24 21:37:06 2011 -0300 @@ -12,7 +12,7 @@ public class PropuestaNewModel { - [Required(ErrorMessage = "debe ingresar el título")] + [Required] public string Titulo { get; set; } public string Ponente { get; set; } @@ -26,12 +26,12 @@ [HiddenInput(DisplayValue = false)] public string Id { get; set; } - [Required(ErrorMessage = "debe ingresar el título")] + [Required] public string Titulo { get; set; } public string Ponente { get; set; } - [Url(ErrorMessage = "La url ingresada no es válida")] + [Url] public string UrlInvitacion { get; set; } } diff -r c8897b1c6f49 -r 7e9ffde4022d Agendas/trunk/src/Agendas.sln --- a/Agendas/trunk/src/Agendas.sln Tue May 24 19:31:10 2011 -0300 +++ b/Agendas/trunk/src/Agendas.sln Tue May 24 21:37:06 2011 -0300 @@ -28,6 +28,8 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.NHibernate", "Agendas.NHibernate\Agendas.NHibernate.csproj", "{9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agendas.Resources", "Agendas.Resources\Agendas.Resources.csproj", "{444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -74,6 +76,10 @@ {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Debug|Any CPU.Build.0 = Debug|Any CPU {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.ActiveCfg = Release|Any CPU {9519A43A-9D5E-4BFD-9F88-AFFC53C9973A}.Release|Any CPU.Build.0 = Release|Any CPU + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {444D25FB-FC1C-48DC-9EAD-D4C78F2A10CA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE