/// <summary> /// Registers the model type metadata. /// </summary> /// <param name="modelType">Type of the model.</param> /// <param name="metadataItem">The metadata.</param> public virtual void RegisterModel([NotNull] Type modelType, [NotNull] ModelMetadataItem metadataItem) { Invariant.IsNotNull(modelType, "modelType"); Invariant.IsNotNull(metadataItem, "metadataItem"); var item = GetOrCreate(modelType); item.ClassMetadata = metadataItem; }
private ModelMetadataItem Append([NotNull] string property) { //Invariant.IsNotNull(property, "property"); var item = new ModelMetadataItem(); configurations[property] = item; return(item); }
private ModelMetadata CreatePropertyMetadata( Type containerType, string propertyName, Type propertyType, ModelMetadataItem propertyMetadata, Func <object> modelAccessor) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, containerType, modelAccessor, propertyType, propertyName, propertyMetadata); if (propertyMetadata != null) { Copy(propertyMetadata, modelMetadata); } return(modelMetadata); }
public static TSetting GetAdditionalSettingOrCreateNew <TSetting>([NotNull] this ModelMetadataItem item) where TSetting : class, IModelMetadataAdditionalSetting, new() { var setting = item.GetAdditionalSetting <TSetting>(); if (setting == null) { setting = new TSetting(); item.AdditionalSettings.Add(setting); } return(setting); }
private static void SyncValidations([NotNull] ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { if (metadataFrom.Validations.Count > 0) { var types = new HashSet <Type>(); foreach (var metadata in metadataTo.Validations) { var type = metadata.GetType(); if (!types.Contains(type)) { types.Add(type); } } //TODO: how about Deleged validation? just rewrite it completely? foreach (var validation in metadataFrom.Validations) { if (!types.Contains(validation.GetType())) { metadataTo.Validations.Add(validation); } } } if (metadataTo.IsRequired == null && metadataFrom.IsRequired.HasValue && metadataFrom.IsRequired.Value) { metadataTo.IsRequired = metadataFrom.IsRequired.Value; } // ensure that Required attribute is removed if (metadataFrom.IsRequired.HasValue && !metadataFrom.IsRequired.Value) { var requiredValidation = metadataTo.GetValidation <RequiredValidationMetadata>(); if (requiredValidation != null) { metadataTo.Validations.Remove(requiredValidation); } metadataTo.IsRequired = false; } }
private static void SyncAdditionalSettings([NotNull] ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { if (metadataFrom.AdditionalSettings.Count > 0) { var additionalSettingsTypes = new HashSet <Type>(); foreach (var setting in metadataTo.AdditionalSettings) { var type = setting.GetType(); if (!additionalSettingsTypes.Contains(type)) { additionalSettingsTypes.Add(type); } } foreach (var setting in metadataFrom.AdditionalSettings) { if (!additionalSettingsTypes.Contains(setting.GetType())) { metadataTo.AdditionalSettings.Add(setting); } } } }
private void ApplyMetadataConvenstions([NotNull] Type modelType, [NotNull] ModelMetadataRegistryItem item) { var properties = modelType.GetProperties(); foreach (var convention in conventions) { var metadataConvention = convention; foreach (var pi in properties.Where(metadataConvention.CanBeAccepted)) { ModelMetadataItem metadataItem; if (!item.PropertiesMetadata.TryGetValue(pi.Name, out metadataItem)) { metadataItem = new ModelMetadataItem(); item.PropertiesMetadata.Add(pi.Name, metadataItem); } var conventionMetadata = convention.CreateMetadataRules(pi); conventionMetadata.MergeTo(metadataItem); } } item.IsConventionsApplied = true; }
/// <summary> /// Merge metadata items /// </summary> public static void MergeTo([NotNull] this ModelMetadataItem metadataFrom, [NotNull] ModelMetadataItem metadataTo) { SetMetadataProperties(metadataFrom, metadataTo); SyncValidations(metadataFrom, metadataTo); SyncAdditionalSettings(metadataFrom, metadataTo); }
private static void SetMetadataProperties(ModelMetadataItem metadataFrom, ModelMetadataItem metadataTo) { // by default the value is true, so will not set it to true if original value was set to false if (metadataFrom.ShowForDisplay == false) { metadataTo.ShowForDisplay = metadataFrom.ShowForDisplay; } // by default the value is false, so will not set it to false if original value was set to true if (metadataFrom.ApplyFormatInEditMode) { metadataTo.ApplyFormatInEditMode = metadataFrom.ApplyFormatInEditMode; } if (metadataTo.DisplayName == null && metadataFrom.DisplayName != null) { metadataTo.DisplayName = metadataFrom.DisplayName; } if (metadataTo.ShortDisplayName == null && metadataFrom.ShortDisplayName != null) { metadataTo.ShortDisplayName = metadataFrom.ShortDisplayName; } if (string.IsNullOrEmpty(metadataTo.TemplateName) && !string.IsNullOrEmpty(metadataFrom.TemplateName)) { metadataTo.TemplateName = metadataFrom.TemplateName; } if (metadataTo.Description == null && metadataFrom.Description != null) { metadataTo.Description = metadataFrom.Description; } if (metadataTo.NullDisplayText == null && metadataFrom.NullDisplayText != null) { metadataTo.NullDisplayText = metadataFrom.NullDisplayText; } if (metadataTo.Watermark == null && metadataFrom.Watermark != null) { metadataTo.Watermark = metadataFrom.Watermark; } if (metadataTo.HideSurroundingHtml == null && metadataFrom.HideSurroundingHtml.HasValue) { metadataTo.HideSurroundingHtml = metadataFrom.HideSurroundingHtml.Value; } if (metadataTo.RequestValidationEnabled == null && metadataFrom.RequestValidationEnabled.HasValue) { metadataTo.RequestValidationEnabled = metadataFrom.RequestValidationEnabled.Value; } if (metadataTo.IsReadOnly == null && metadataFrom.IsReadOnly.HasValue) { metadataTo.IsReadOnly = metadataFrom.IsReadOnly.Value; } if (metadataTo.ShowForEdit == null && metadataFrom.ShowForEdit.HasValue) { metadataTo.ShowForEdit = metadataFrom.ShowForEdit.Value; } if (metadataTo.EditFormat == null && metadataFrom.EditFormat != null) { metadataTo.EditFormat = metadataFrom.EditFormat; } if (metadataTo.Order == null && metadataFrom.Order.HasValue) { metadataTo.Order = metadataFrom.Order; } if (metadataTo.DisplayFormat == null && metadataFrom.DisplayFormat != null) { metadataTo.DisplayFormat = metadataFrom.DisplayFormat; } if (metadataTo.ConvertEmptyStringToNull == null && metadataFrom.ConvertEmptyStringToNull.HasValue) { metadataTo.ConvertEmptyStringToNull = metadataFrom.ConvertEmptyStringToNull; } }
public static TSetting GetAdditionalSetting <TSetting>([NotNull] this ModelMetadataItem item) where TSetting : IModelMetadataAdditionalSetting { return(item.AdditionalSettings.OfType <TSetting>().FirstOrDefault()); }
/// <summary> /// Initializes a new instance of the <see cref="ExtendedModelMetadata"/> class. /// </summary> /// <param name="provider">The provider.</param> /// <param name="containerType">Type of the container.</param> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">Type of the model.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="metadata">The metadata.</param> public ExtendedModelMetadata(ModelMetadataProvider provider, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName, ModelMetadataItem metadata) : base(provider, containerType, modelAccessor, modelType, propertyName) { Metadata = metadata; }
private ModelMetadata CreateModelMetadata(Type modelType, Func <object> modelAccessor, ModelMetadataItem metadataItem) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, null, modelAccessor, modelType, null, metadataItem); if (metadataItem != null) { Copy(metadataItem, modelMetadata); } return(modelMetadata); }
private static void Copy(ModelMetadataItem metadataItem, ModelMetadata metadata) { metadata.ShowForDisplay = metadataItem.ShowForDisplay; if (metadataItem.DisplayName != null) { metadata.DisplayName = metadataItem.DisplayName(); } if (metadataItem.ShortDisplayName != null) { metadata.ShortDisplayName = metadataItem.ShortDisplayName(); } if (!string.IsNullOrEmpty(metadataItem.TemplateName)) { metadata.TemplateHint = metadataItem.TemplateName; } if (metadataItem.Description != null) { metadata.Description = metadataItem.Description(); } if (metadataItem.NullDisplayText != null) { metadata.NullDisplayText = metadataItem.NullDisplayText(); } if (metadataItem.Watermark != null) { metadata.Watermark = metadataItem.Watermark(); } if (metadataItem.HideSurroundingHtml.HasValue) { metadata.HideSurroundingHtml = metadataItem.HideSurroundingHtml.Value; } if (metadataItem.RequestValidationEnabled.HasValue) { metadata.RequestValidationEnabled = metadataItem.RequestValidationEnabled.Value; } if (metadataItem.IsReadOnly.HasValue) { metadata.IsReadOnly = metadataItem.IsReadOnly.Value; } if (metadataItem.IsRequired.HasValue) { metadata.IsRequired = metadataItem.IsRequired.Value; } if (metadataItem.ShowForEdit.HasValue) { metadata.ShowForEdit = metadataItem.ShowForEdit.Value; } else { metadata.ShowForEdit = !metadata.IsReadOnly; } if (metadataItem.Order.HasValue) { metadata.Order = metadataItem.Order.Value; } if (metadataItem.DisplayFormat != null) { metadata.DisplayFormatString = metadataItem.DisplayFormat(); } if (metadataItem.ApplyFormatInEditMode && metadata.ShowForEdit && metadataItem.EditFormat != null) { metadata.EditFormatString = metadataItem.EditFormat(); } if (metadataItem.ConvertEmptyStringToNull.HasValue) { metadata.ConvertEmptyStringToNull = metadataItem.ConvertEmptyStringToNull.Value; } FluentModelMetadataTransformer.Value.Transform(metadata); DisplayNameTransformer.Value.Transform(metadata); }
/// <summary> /// Initializes a new instance of the <see cref="ModelMetadataItemBuilder{TValue}"/> class. /// </summary> /// <param name="item">The item.</param> public ModelMetadataItemBuilder([NotNull] ModelMetadataItem item) { //Invariant.IsNotNull(item, "item"); Item = item; }
private ModelMetadata CreatePropertyMetadata( Type containerType, string propertyName, Type propertyType, ModelMetadataItem propertyMetadata, Func<object> modelAccessor) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, containerType, modelAccessor, propertyType, propertyName, propertyMetadata); if (propertyMetadata != null) { Copy(propertyMetadata, modelMetadata); } return modelMetadata; }
private ModelMetadata CreateModelMetadata(Type modelType, Func<object> modelAccessor, ModelMetadataItem metadataItem) { ModelMetadata modelMetadata = new ExtendedModelMetadata(this, null, modelAccessor, modelType, null, metadataItem); if (metadataItem != null) { Copy(metadataItem, modelMetadata); } return modelMetadata; }
public static TValidationMetadata GetValidationOrCreateNew <TValidationMetadata>([NotNull] this ModelMetadataItem item) where TValidationMetadata : class, IModelValidationMetadata, new() { var validation = item.GetValidation <TValidationMetadata>(); if (validation == null) { validation = new TValidationMetadata(); item.Validations.Add(validation); } return(validation); }
public static TValidationMetadata GetValidation <TValidationMetadata>([NotNull] this ModelMetadataItem item) where TValidationMetadata : IModelValidationMetadata { return(item.Validations.OfType <TValidationMetadata>().SingleOrDefault()); }
/// <summary> /// Initializes a new instance of the <see cref="ExtendedModelMetadata"/> class. /// </summary> /// <param name="provider">The provider.</param> /// <param name="containerType">Type of the container.</param> /// <param name="modelAccessor">The model accessor.</param> /// <param name="modelType">Type of the model.</param> /// <param name="propertyName">Name of the property.</param> /// <param name="metadata">The metadata.</param> public ExtendedModelMetadata(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName, ModelMetadataItem metadata) : base(provider, containerType, modelAccessor, modelType, propertyName) { Metadata = metadata; }