public CachedDataAnnotationsModelMetadata2(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
     : base(prototype, modelAccessor)
 {
     var prototype2 = prototype as CachedDataAnnotationsModelMetadata2;
     if (prototype2 != null)
     {
         this.Attributes = prototype2.Attributes;
     }
 }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(
            CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var metadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            var type = metadata.ModelType;
            if (type.IsEnum ||
                (type.IsGenericType(typeof(Nullable<>)) &&
                type.GetGenericArguments().Length == 1 && type.GetGenericArguments()[0].IsEnum))
                metadata.TemplateHint = "Enum";

            return metadata;
        }
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var metadataFromPrototype = base.CreateMetadataFromPrototype(prototype, modelAccessor);
            foreach (var keyValuePair in prototype.AdditionalValues)
            {
                metadataFromPrototype.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            // we need to preserve DisplayName fetched during prototype creation
            metadataFromPrototype.DisplayName = prototype.DisplayName;

            return metadataFromPrototype;
        }
		protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
		{
			var result = base.CreateMetadataFromPrototype(prototype, modelAccessor);

			if (prototype.AdditionalValues.Count > 0)
			{
				var attrs = prototype.AdditionalValues.Where(x => x.Value is IModelAttribute).ToArray();
				if (attrs.Any())
				{
					result.AdditionalValues.AddRange(attrs);
				}
			}

			return result;		
		}
Exemplo n.º 5
0
        protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
        {
            var modelMetaData = base.CreateMetadataFromPrototype(prototype, modelAccessor);

            Composite composite;
            if (this.compositeByModelType.TryGetValue(modelMetaData.ModelType, out composite))
            {
                // CompositeType
                modelMetaData.SetComposite(composite);

                foreach (var filter in this.typeAnnotations)
                {
                    filter.OnTypeMetadataCreated(modelMetaData);
                }
            }
            else if (modelMetaData.ContainerType != null && this.compositeByModelType.TryGetValue(modelMetaData.ContainerType, out composite))
            {
                modelMetaData.SetComposite(composite);

                // Path
                if (composite != null)
                {
                    Dictionary<string, Path> pathByPropertyName;
                    if (this.pathByPropertyNameByModelType.TryGetValue(modelMetaData.ContainerType, out pathByPropertyName))
                    {
                        Path path;
                        if (pathByPropertyName.TryGetValue(modelMetaData.PropertyName, out path))
                        {
                            modelMetaData.SetPath(path);
                        }
                    }
                }

                foreach (var filter in this.propertyAnnotations)
                {
                    filter.OnPropertyMetadataCreated(modelMetaData);
                }
            }

            return modelMetaData;
        }
        public void AttributesOverrideMetadataStrings(Attribute attribute, Func<ModelMetadata, string> accessor)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(ClassWithDisplayableColumn),
                propertyName: null,
                attributes: attributes)
            {
                Model = new ClassWithDisplayableColumn { Property = "value" },
            };
            var result = accessor(metadata);

            // Assert
            Assert.Equal("value", result);
        }
        public void Constructor_DefersDefaultsToBaseModelMetadata()
        {
            // Arrange
            var attributes = Enumerable.Empty<Attribute>();
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(object),
                propertyName: null,
                attributes: attributes);

            // Assert
            Assert.True(metadata.ConvertEmptyStringToNull);
            Assert.False(metadata.IsReadOnly);
            Assert.False(metadata.IsRequired);

            Assert.Null(metadata.Description);
            Assert.Null(metadata.DisplayName);
            Assert.Null(metadata.NullDisplayText);
        }
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype,Func<object> modelAccessor)
 {
     CachedDataAnnotationsModelMetadata modelMetadata = base.CreateMetadataFromPrototype(prototype, modelAccessor);
     modelMetadata.DisplayName = prototype.DisplayName;
     return modelMetadata;
 }
 protected override CachedDataAnnotationsModelMetadata CreateMetadataFromPrototype(CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor)
 {
     return new CachedDataAnnotationsModelMetadata2(prototype, modelAccessor);
 }
        public void AttributesOverrideMetadataBooleans(
            Attribute attribute,
            Func<ModelMetadata, bool> accessor,
            bool expectedResult)
        {
            // Arrange
            var attributes = new[] { attribute };
            var provider = new DataAnnotationsModelMetadataProvider();

            // Act
            var metadata = new CachedDataAnnotationsModelMetadata(
                provider,
                containerType: null,
                modelType: typeof(object),
                propertyName: null,
                attributes: attributes);
            var result = accessor(metadata);

            // Assert
            Assert.Equal(expectedResult, result);
        }
        private ModelMetadata CloneMetadata(Func<object> modelAccessor)
        {
            var cachedMetadata = _metadata as CachedDataAnnotationsModelMetadata;
            var annotationsMetadata = _metadata as DataAnnotationsModelMetadata;
            ModelMetadata clonedMetadata;
            if (cachedMetadata != null)
            {
                clonedMetadata = new CachedDataAnnotationsModelMetadata(cachedMetadata, modelAccessor);
            }
            else if (annotationsMetadata != null)
            {
                var provider = (_oldMetadataProvider as DataAnnotationsModelMetadataProvider) ?? AnnotationsProvider;
                clonedMetadata = new DataAnnotationsModelMetadata(
                    provider,
                    annotationsMetadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    annotationsMetadata.PropertyName,
                    displayColumnAttribute: null);      // Copying SimpleDisplayText below compensates for null here.
            }
            else
            {
                clonedMetadata = new ModelMetadata(
                    _oldMetadataProvider,
                    _metadata.ContainerType,
                    modelAccessor,
                    _modelType,
                    _metadata.PropertyName);
            }

            // Undo all the lazy-initialization of ModelMetadata and CachedDataAnnotationsModelMetadata...
            clonedMetadata.Container = _metadata.Container;        // May be incorrect.
            clonedMetadata.ConvertEmptyStringToNull = _metadata.ConvertEmptyStringToNull;
            clonedMetadata.DataTypeName = _metadata.DataTypeName;
            clonedMetadata.Description = _metadata.Description;
            clonedMetadata.DisplayFormatString = _metadata.DisplayFormatString;
            clonedMetadata.DisplayName = _metadata.DisplayName;
            clonedMetadata.EditFormatString = _metadata.EditFormatString;
            clonedMetadata.HasNonDefaultEditFormat = _metadata.HasNonDefaultEditFormat;
            clonedMetadata.HideSurroundingHtml = _metadata.HideSurroundingHtml;
            clonedMetadata.HtmlEncode = _metadata.HtmlEncode;
            clonedMetadata.IsReadOnly = _metadata.IsReadOnly;
            clonedMetadata.IsRequired = _metadata.IsRequired;
            clonedMetadata.NullDisplayText = _metadata.NullDisplayText;
            clonedMetadata.Order = _metadata.Order;
            clonedMetadata.RequestValidationEnabled = _metadata.RequestValidationEnabled;
            clonedMetadata.ShortDisplayName = _metadata.ShortDisplayName;
            clonedMetadata.ShowForDisplay = _metadata.ShowForDisplay;
            clonedMetadata.ShowForEdit = _metadata.ShowForEdit;
            clonedMetadata.SimpleDisplayText = _metadata.SimpleDisplayText;
            clonedMetadata.TemplateHint = _metadata.TemplateHint;
            clonedMetadata.Watermark = _metadata.Watermark;
            foreach (var keyValuePair in _metadata.AdditionalValues)
            {
                clonedMetadata.AdditionalValues.Add(keyValuePair.Key, keyValuePair.Value);
            }

            return clonedMetadata;
        }