Created
August 8, 2012 20:16
-
-
Save AlexBar/3298230 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #region Copyright | |
| // Copyright (c) 2009 - 2011, Kazi Manzur Rashid <kazimanzurrashid@gmail.com>, hazzik <hazzik@gmail.com>. | |
| // This source is subject to the Microsoft Public License. | |
| // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL. | |
| // All other rights reserved. | |
| #endregion | |
| namespace MvcExtensions | |
| { | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Web.Mvc; | |
| /// <summary> | |
| /// Defines a class which is used to register the default <seealso cref="ModelMetadataProvider"/>. | |
| /// </summary> | |
| public static class MvcExtensionsMetadataRegistrar | |
| { | |
| private static ModelMetadataRegistrar registrar; | |
| /// <summary> | |
| /// Instance of <see cref="ModelMetadataRegistrar"/> type | |
| /// </summary> | |
| private static ModelMetadataRegistrar Current | |
| { | |
| get | |
| { | |
| return registrar ?? (registrar = new ModelMetadataRegistrar(new ModelMetadataRegistry(), new ConfigurationScanner())); | |
| } | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| /// <param name="configurationFactory"></param> | |
| /// <returns></returns> | |
| public static ModelMetadataRegistrar ConstructMetadataUsing(Func<Type, IModelMetadataConfiguration> configurationFactory) | |
| { | |
| return Current.ConstructMetadataUsing(configurationFactory); | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| public static void Register() | |
| { | |
| Current.Register(); | |
| } | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| public class ModelMetadataRegistrar | |
| { | |
| private readonly IModelMetadataRegistry registry; | |
| private readonly IConfigurationScanner configurationScanner; | |
| private Func<Type, IModelMetadataConfiguration> configurationFactory; | |
| /// <summary> | |
| /// Creates <see cref="ModelMetadataRegistrar"/> instance | |
| /// </summary> | |
| /// <param name="registry"></param> | |
| /// <param name="configurationScanner"> </param> | |
| public ModelMetadataRegistrar(IModelMetadataRegistry registry, IConfigurationScanner configurationScanner) | |
| { | |
| this.registry = registry; | |
| this.configurationScanner = configurationScanner; | |
| } | |
| /// <summary> | |
| /// | |
| /// </summary> | |
| /// <param name="configurationFactory"></param> | |
| /// <returns></returns> | |
| public ModelMetadataRegistrar ConstructMetadataUsing(Func<Type, IModelMetadataConfiguration> configurationFactory) | |
| { | |
| this.configurationFactory = configurationFactory; | |
| return this; | |
| } | |
| /// <summary> | |
| /// Registers metadata provider | |
| /// </summary> | |
| public virtual void Register() | |
| { | |
| configurationFactory = configurationFactory ?? (t => (IModelMetadataConfiguration)Activator.CreateInstance(t)); | |
| var configurations = configurationScanner.GetConfigurationTypes() | |
| .Select(configurationFactory); | |
| Register(configurations); | |
| } | |
| /// <summary> | |
| /// Registers metadata provider and metadata configuration class | |
| /// </summary> | |
| private void Register(IEnumerable<IModelMetadataConfiguration> configurations) | |
| { | |
| Invariant.IsNotNull(configurations, "configurations"); | |
| foreach (var configuration in configurations) | |
| { | |
| registry.RegisterModelProperties(configuration.ModelType, configuration.Configurations); | |
| } | |
| IList<ModelValidatorProvider> validatorProviders = new List<ModelValidatorProvider>(ModelValidatorProviders.Providers); | |
| validatorProviders.Insert(0, new ExtendedModelValidatorProvider()); | |
| var compositeModelValidatorProvider = new CompositeModelValidatorProvider(validatorProviders.ToArray()); | |
| ModelMetadataProviders.Current = new ExtendedModelMetadataProvider(registry); | |
| ModelValidatorProviders.Providers.Clear(); | |
| ModelValidatorProviders.Providers.Add(compositeModelValidatorProvider); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment