From 6f1d965f03b78eb1ea6b6917c8f17be967d4741a Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 2 Jul 2015 16:55:31 +0800 Subject: [PATCH 1/2] add support interface IMetadataAware --- .../ModelMetadata/ModelMetadataItem.cs | 14 +++++-- .../ModelMetadataItemExtensions.cs | 42 ++++++++++++++++++- .../ExtendedModelMetadataProvider.cs | 7 ++++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItem.cs b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItem.cs index 0deca2f..da9883d 100644 --- a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItem.cs +++ b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItem.cs @@ -3,8 +3,10 @@ // This source is subject to the Microsoft Public License. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. // All other rights reserved. -#endregion - +#endregion + +using System.Web.Mvc; + namespace MvcExtensions { using System; @@ -25,6 +27,7 @@ public ModelMetadataItem() ShowForDisplay = true; Validations = new List(); AdditionalSettings = new List(); + MetadataAwares = new List(); } /// @@ -165,7 +168,12 @@ public IList AdditionalSettings { get; private set; - } + } + + /// + /// Gets the MetadataAwares + /// + public IList MetadataAwares { get; private set; } /// /// Gets or sets the order. diff --git a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItemExtensions.cs b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItemExtensions.cs index c10960b..0fa5ce5 100644 --- a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItemExtensions.cs +++ b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataItemExtensions.cs @@ -3,8 +3,10 @@ // This source is subject to the Microsoft Public License. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. // All other rights reserved. -#endregion - +#endregion + +using System.Web.Mvc; + namespace MvcExtensions { using System.Linq; @@ -85,6 +87,42 @@ public static TSetting GetAdditionalSetting([NotNull] this ModelMetada where TSetting : IModelMetadataAdditionalSetting { return item.AdditionalSettings.OfType().FirstOrDefault(); + } + + + /// + /// Returns model metadataAware of type associated with this . + /// New model setting will be created if no one is found. + /// + /// + /// + /// Model validation metadata of type + [NotNull] + public static TSetting GetMetadataAwareOrCreateNew([NotNull] this ModelMetadataItem item) + where TSetting : class, IMetadataAware, new() + { + var setting = item.GetMetadataAware(); + + if (setting == null) + { + setting = new TSetting(); + item.MetadataAwares.Add(setting); + } + + return setting; + } + + /// + /// Returns model metadataAware of type associated with this or null. + /// + /// + /// + /// Model validation metadata of type or null + [CanBeNull] + public static TSetting GetMetadataAware([NotNull] this ModelMetadataItem item) + where TSetting : IMetadataAware + { + return item.MetadataAwares.OfType().FirstOrDefault(); } } } \ No newline at end of file diff --git a/src/MvcExtensions.FluentMetadata/Providers/ExtendedModelMetadataProvider.cs b/src/MvcExtensions.FluentMetadata/Providers/ExtendedModelMetadataProvider.cs index 83b0dda..d044221 100644 --- a/src/MvcExtensions.FluentMetadata/Providers/ExtendedModelMetadataProvider.cs +++ b/src/MvcExtensions.FluentMetadata/Providers/ExtendedModelMetadataProvider.cs @@ -212,6 +212,11 @@ private static void Copy(ModelMetadataItem metadataItem, ModelMetadata metadata) metadata.ConvertEmptyStringToNull = metadataItem.ConvertEmptyStringToNull.Value; } + foreach (var item in metadataItem.MetadataAwares) + { + item.OnMetadataCreated(metadata); + } + FluentModelMetadataTransformer.Transform(metadata); MvcExtensions.DisplayNameTransformer.Transform(metadata); } @@ -224,6 +229,7 @@ private ModelMetadata CreateModelMetadata(Type modelType, Func modelAcce if (metadataItem != null) { Copy(metadataItem, modelMetadata); + } return modelMetadata; @@ -238,6 +244,7 @@ private ModelMetadata CreatePropertyMetadata( if (propertyMetadata != null) { Copy(propertyMetadata, modelMetadata); + } return modelMetadata; From 4663420db952cae18ed72bfac826413e8c562094 Mon Sep 17 00:00:00 2001 From: eric Date: Fri, 23 Oct 2015 13:54:31 +0800 Subject: [PATCH 2/2] allow configured by multiple times --- .../ModelMetadata/ModelMetadataConfiguration.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataConfiguration.cs b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataConfiguration.cs index f14d03f..73c0716 100644 --- a/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataConfiguration.cs +++ b/src/MvcExtensions.FluentMetadata/ModelMetadata/ModelMetadataConfiguration.cs @@ -99,7 +99,12 @@ protected virtual ModelMetadataItem Append([NotNull] Expression