コード例 #1
0
        public void OverwrittenTimestampMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity2));

            PropertyDescriptor descriptor = properties["Timestamp"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for Timestamp.");
            Assert.AreEqual(typeof(DateTime), descriptor.PropertyType,
                            "The Timestamp type should be a DateTime.");

            TimestampAttribute timestampAttribute = (TimestampAttribute)descriptor.Attributes[typeof(TimestampAttribute)];

            Assert.IsNotNull(timestampAttribute,
                             "The Timestamp should have a TimestampAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The Timestamp should have an EditableAttribute.");
            Assert.IsTrue(editableAttribute.AllowEdit,
                          "The Timestamp should allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The Timestamp should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The Timestamp should have an DisplayAttribute.");
            Assert.AreEqual("T", displayAttribute.Name,
                            "The Timestamp names should be equal.");
        }
コード例 #2
0
        public void ETagProperty()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity1));

            PropertyDescriptor descriptor = properties["ETag"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for ETag.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The ETag type should be a string.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The ETag should have an EditableAttribute.");
            Assert.IsFalse(editableAttribute.AllowEdit,
                           "The ETag should not allow editing.");
            Assert.IsFalse(editableAttribute.AllowInitialValue,
                           "The ETag should not allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The ETag should have an DisplayAttribute.");
            Assert.IsFalse(displayAttribute.AutoGenerateField,
                           "The ETag should not be included in autogeneration.");

            ConcurrencyCheckAttribute concurrencyCheckAttribute = (ConcurrencyCheckAttribute)descriptor.Attributes[typeof(ConcurrencyCheckAttribute)];

            Assert.IsNotNull(concurrencyCheckAttribute,
                             "The ETag should have an ConcurrencyCheckAttribute.");
        }
コード例 #3
0
        public void OverwrittenRowKeyMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity2));

            PropertyDescriptor descriptor = properties["RowKey"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for RowKey.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The RowKey type should be a string.");

            KeyAttribute keyAttribute = (KeyAttribute)descriptor.Attributes[typeof(KeyAttribute)];

            Assert.IsNotNull(keyAttribute,
                             "The RowKey should have a KeyAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The RowKey should have an EditableAttribute.");
            Assert.IsTrue(editableAttribute.AllowEdit,
                          "The RowKey should allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The RowKey should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The RowKey should have an DisplayAttribute.");
            Assert.AreEqual("RK", displayAttribute.Name,
                            "The RowKey names shoulds be equal.");
        }
コード例 #4
0
        private static RouteValueDictionary FormHtmlAttributes(LambdaExpression expression, object attributes,
                                                               string cssClass)
        {
            RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(attributes);

            if (!htmlAttributes.ContainsKey("autocomplete"))
            {
                htmlAttributes["autocomplete"] = "off";
            }
            htmlAttributes["class"] = (cssClass + " " + htmlAttributes["class"]).Trim();
            if (htmlAttributes.ContainsKey("readonly"))
            {
                return(htmlAttributes);
            }

            var memberExpression = expression.Body as MemberExpression;

            if (memberExpression != null && memberExpression.Member.IsDefined(typeof(EditableAttribute), false))
            {
                EditableAttribute editable = memberExpression.Member.GetCustomAttribute <EditableAttribute>(false);
                if (!editable.AllowEdit)
                {
                    htmlAttributes["readonly"] = "readonly";
                }
            }

            return(htmlAttributes);
        }
コード例 #5
0
        public void Ctor(bool value)
        {
            var attribute = new EditableAttribute(value);

            Assert.Equal(value, attribute.AllowEdit);
            Assert.Equal(value, attribute.AllowInitialValue);
        }
コード例 #6
0
        public void DefaultRowKeyMetadata()
        {
            // Create the DSD to register type descriptors
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TMPT_DomainService));

            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(TMPT_MockEntity1));

            PropertyDescriptor descriptor = properties["RowKey"];

            Assert.IsNotNull(descriptor,
                             "There should be a property descriptor for RowKey.");
            Assert.AreEqual(typeof(string), descriptor.PropertyType,
                            "The RowKey type should be a string.");

            KeyAttribute keyAttribute = (KeyAttribute)descriptor.Attributes[typeof(KeyAttribute)];

            Assert.IsNotNull(keyAttribute,
                             "The RowKey should have a KeyAttribute.");

            EditableAttribute editableAttribute = (EditableAttribute)descriptor.Attributes[typeof(EditableAttribute)];

            Assert.IsNotNull(editableAttribute,
                             "The RowKey should have an EditableAttribute.");
            Assert.IsFalse(editableAttribute.AllowEdit,
                           "The RowKey should not allow editing.");
            Assert.IsTrue(editableAttribute.AllowInitialValue,
                          "The RowKey should allow an initial value.");

            DisplayAttribute displayAttribute = (DisplayAttribute)descriptor.Attributes[typeof(DisplayAttribute)];

            Assert.IsNotNull(displayAttribute,
                             "The RowKey should have an DisplayAttribute.");
            Assert.IsFalse(displayAttribute.AutoGenerateField,
                           "The RowKey should not be included in autogeneration.");
        }
コード例 #7
0
        private static RouteValueDictionary FormHtmlAttributes(LambdaExpression expression, Object attributes, String cssClass)
        {
            RouteValueDictionary htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(attributes);

            htmlAttributes["class"] = String.Format("{0} {1}", cssClass, htmlAttributes["class"]).Trim();
            if (!htmlAttributes.ContainsKey("autocomplete"))
            {
                htmlAttributes.Add("autocomplete", "off");
            }
            if (htmlAttributes.ContainsKey("readonly"))
            {
                return(htmlAttributes);
            }

            MemberExpression memberExpression = expression.Body as MemberExpression;

            if (memberExpression == null)
            {
                throw new InvalidOperationException("Expression must be a member expression.");
            }

            EditableAttribute editable = memberExpression.Member.GetCustomAttribute <EditableAttribute>();

            if (editable != null && !editable.AllowEdit)
            {
                htmlAttributes.Add("readonly", "readonly");
            }

            return(htmlAttributes);
        }
コード例 #8
0
        /// <summary>
        /// Returns a value indicating whether the specified property is read-only.
        /// </summary>
        /// <remarks>
        /// This method determines read only state by checking for appropriately configured
        /// <see cref="EditableAttribute"/>s.
        /// </remarks>
        /// <param name="propertyInfo">The property to determine whether it is read-only</param>
        /// <returns><c>true</c> if the property is marked read-only; <c>false</c> otherwise.</returns>
        private static bool IsReadOnly(PropertyInfo propertyInfo)
        {
            EditableAttribute editableAttribute =
                propertyInfo.GetCustomAttributes(typeof(EditableAttribute), false)
                .Cast <EditableAttribute>().FirstOrDefault();

            return(editableAttribute != null && !editableAttribute.AllowEdit);
        }
コード例 #9
0
        public void EditableAttribute_AllowInitialValue_Defaults_To_Match_AllowEdit()
        {
            EditableAttribute attr = new EditableAttribute(false);

            Assert.IsFalse(attr.AllowInitialValue, "AllowInitialValue should default to false when AllowEdit is set to false");

            attr = new EditableAttribute(true);
            Assert.IsTrue(attr.AllowInitialValue, "AllowInitialValue should default to true when AllowEdit is set to true");
        }
コード例 #10
0
        public void EditableAttribute_AllowInitialValue_Can_Be_True()
        {
            EditableAttribute attr = new EditableAttribute(false)
            {
                AllowInitialValue = true
            };

            Assert.IsTrue(attr.AllowInitialValue, "AllowInitialValue should be true after being set so");
        }
コード例 #11
0
        public void EditableAttribute_AllowEdit_Matches_Constructor_Argument()
        {
            EditableAttribute attr = new EditableAttribute(false);

            Assert.IsFalse(attr.AllowEdit, "AllowEdit should be false");

            attr = new EditableAttribute(true);
            Assert.IsTrue(attr.AllowEdit, "AllowEdit should be true");
        }
コード例 #12
0
        public void EditableAttribute_AllowInitialValue_Can_Be_False()
        {
            EditableAttribute attr = new EditableAttribute(true)
            {
                AllowInitialValue = false
            };

            Assert.IsFalse(attr.AllowInitialValue, "AllowInitialValue should be false after being set so");
        }
コード例 #13
0
        public void Properties_ChangingOneProperty_DoesNotAffectTheOther(bool value)
        {
            var attribute = new EditableAttribute(value);
            Assert.Equal(value, attribute.AllowEdit);
            Assert.Equal(value, attribute.AllowInitialValue);

            attribute.AllowInitialValue = !value;
            Assert.Equal(value, attribute.AllowEdit);
            Assert.Equal(!value, attribute.AllowInitialValue);
        }
コード例 #14
0
        public void Properties_ChangingOneProperty_DoesNotAffectTheOther(bool value)
        {
            var attribute = new EditableAttribute(value);

            Assert.Equal(value, attribute.AllowEdit);
            Assert.Equal(value, attribute.AllowInitialValue);

            attribute.AllowInitialValue = !value;
            Assert.Equal(value, attribute.AllowEdit);
            Assert.Equal(!value, attribute.AllowInitialValue);
        }
コード例 #15
0
        public bool GetPropertyIsReadOnly(string propertyName)
        {
            if (this.DataType != null)
            {
                if (!string.IsNullOrEmpty(propertyName))
                {
                    Type          propertyType  = this.DataType;
                    PropertyInfo  propertyInfo  = null;
                    List <string> propertyNames = TypeHelper.SplitPropertyPath(propertyName);
                    for (int i = 0; i < propertyNames.Count; i++)
                    {
                        if (propertyType.GetTypeInfo().GetIsReadOnly())
                        {
                            return(true);
                        }

                        object[] index = null;
                        propertyInfo = propertyType.GetPropertyOrIndexer(propertyNames[i], out index);
                        if (propertyInfo == null || propertyInfo.GetIsReadOnly())
                        {
                            // Either the property doesn't exist or it does exist but is read-only.
                            return(true);
                        }

                        // Check if EditableAttribute is defined on the property and if it indicates uneditable
                        EditableAttribute editableAttribute = null;
                        editableAttribute = propertyInfo.GetCustomAttributes().OfType <EditableAttribute>().FirstOrDefault();
                        if (editableAttribute != null && !editableAttribute.AllowEdit)
                        {
                            return(true);
                        }

                        propertyType = propertyInfo.PropertyType.GetNonNullableType();
                    }

                    return(propertyInfo == null || !propertyInfo.CanWrite || !this.AllowEdit || !CanEdit(propertyType));
                }
                else
                {
                    if (this.DataType.GetTypeInfo().GetIsReadOnly())
                    {
                        return(true);
                    }
                }
            }

            return(!this.AllowEdit);
        }
コード例 #16
0
        private IInsqlEntityMap CreateAnnotationEntityMap(Type entityType)
        {
            TableAttribute tableAttribute = (TableAttribute)entityType.GetCustomAttribute(typeof(TableAttribute), true);

            IInsqlEntityMap resultMap = new InsqlEntityMap(entityType, tableAttribute.Name, tableAttribute.Schema);

            var columnMaps = entityType.GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(propInfo =>
            {
                ColumnAttribute columnAttribute       = (ColumnAttribute)propInfo.GetCustomAttribute(typeof(ColumnAttribute), true);
                KeyAttribute keyAttribute             = (KeyAttribute)propInfo.GetCustomAttribute(typeof(KeyAttribute), true);
                NotMappedAttribute notMappedAttribute = (NotMappedAttribute)propInfo.GetCustomAttribute(typeof(NotMappedAttribute), true);
                EditableAttribute editableAttribute   = (EditableAttribute)propInfo.GetCustomAttribute(typeof(EditableAttribute), true);
                DatabaseGeneratedAttribute databaseGeneratedAttribute = (DatabaseGeneratedAttribute)propInfo.GetCustomAttribute(typeof(DatabaseGeneratedAttribute), true);

                InsqlPropertyMap propertyMap = new InsqlPropertyMap(propInfo, columnAttribute?.Name);

                if (keyAttribute != null)
                {
                    propertyMap.IsKey = true;
                }
                if (notMappedAttribute != null)
                {
                    propertyMap.IsIgnored = true;
                }
                if (databaseGeneratedAttribute != null)
                {
                    if (databaseGeneratedAttribute.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
                    {
                        propertyMap.IsIdentity = true;
                    }
                }
                if (editableAttribute != null && !editableAttribute.AllowEdit)
                {
                    propertyMap.IsReadonly = true;
                }

                return(propertyMap);
            });

            foreach (var columnMap in columnMaps)
            {
                resultMap.Properties.Add(columnMap);
            }

            InsqlEntityValidator.Instance.Validate(resultMap);

            return(resultMap);
        }
コード例 #17
0
        /// <summary>
        /// Determine if the property is a read only hyperlink.
        /// </summary>
        /// <param name="modelMetadata">The <see cref="ModelMetadata"/> instance.</param>
        /// <returns><c>true</c> if the property is a read only hyperlink; otherwise, <c>false</c>.</returns>
        public static bool IsReadOnlyHyperlink(this ModelMetadata modelMetadata)
        {
#if DEBUG
            var step = MiniProfiler.Current.Step("ModelMetadata.IsReadOnlyHyperlink");

            try
            {
#endif
            // Check property is readonly and has a link or external link
            if (modelMetadata.IsReadOnly && !modelMetadata.IsViewModel() && (modelMetadata.HasLink() || modelMetadata.HasExternalLink()))
            {
                EditableAttribute editable = modelMetadata.GetAttribute <EditableAttribute>();
                ReadOnlyAttribute readOnly = modelMetadata.GetAttribute <ReadOnlyAttribute>();

                // Property must ALWAYS be read only to render as a hyperlink
                if ((editable != null && !editable.AllowEdit) || (readOnly != null && readOnly.IsReadOnly))
                {
                    var notDefault = ComparisonType.NotEqualTo.Compare(modelMetadata.Model, modelMetadata.ModelType.GetDefaultValue());
                    var selectList = modelMetadata.Model as IEnumerable <SelectListItem>;

                    if (selectList != null)
                    {
                        // Handle "not default value" for IEnumerable<SelectListItem> properties based on whether there are selections
                        notDefault = selectList.Any(p => p.Selected);
                    }

                    // Only a hyperlink if the model has a value
                    if (modelMetadata.ModelType == typeof(string) ? !string.IsNullOrEmpty(modelMetadata.Model as string) : notDefault)
                    {
                        return(true);
                    }
                }
            }

            return(false);

#if DEBUG
        }

        finally
        {
            if (step != null)
            {
                step.Dispose();
            }
        }
#endif
        }
コード例 #18
0
        public void EditableAttribute_Can_Discover_Properties_With_Specified_AllowInitialValue_Overriding_Derived_Class()
        {
            Type              employeeType = typeof(SuperDuperEmployee);
            PropertyInfo      prop         = employeeType.GetProperty("FirstNameProperty");
            EditableAttribute attr         = prop.GetCustomAttributes(typeof(EditableAttribute), true).FirstOrDefault() as EditableAttribute;

            Assert.IsNotNull(attr, "Should have found an editable attribute on FirstNameProperty");
            Assert.IsTrue(attr.AllowEdit, "AllowEdit should be true for FirstNameProperty");
            Assert.IsFalse(attr.AllowInitialValue, "AllowInitialValue should be false for FirstNameProperty");

            prop = employeeType.GetProperty("EmployeeIdProperty");
            attr = prop.GetCustomAttributes(typeof(EditableAttribute), true).FirstOrDefault() as EditableAttribute;
            Assert.IsNotNull(attr, "Should have found an editable attribute on EmployeeIdProperty");
            Assert.IsFalse(attr.AllowEdit, "AllowEdit should be false for EmployeeIdProperty");
            Assert.IsTrue(attr.AllowInitialValue, "AllowInitialValue should be true for EmployeeIdProperty");
        }
コード例 #19
0
        public void EditableAttribute_Can_Discover_Fields_With_Specified_AllowInitialValue()
        {
            Type              employeeType = typeof(Employee);
            FieldInfo         field        = employeeType.GetField("EmployeeNumberField");
            EditableAttribute attr         = field.GetCustomAttributes(typeof(EditableAttribute), true).FirstOrDefault() as EditableAttribute;

            Assert.IsNotNull(attr, "Should have found an editable attribute on EmployeeNumberField");
            Assert.IsFalse(attr.AllowEdit, "AllowEdit should be false for EmployeeNumberField");
            Assert.IsTrue(attr.AllowInitialValue, "AllowInitialValue should be true for EmployeeNumberField");

            field = employeeType.GetField("UsernameField");
            attr  = field.GetCustomAttributes(typeof(EditableAttribute), true).FirstOrDefault() as EditableAttribute;
            Assert.IsNotNull(attr, "Should have found an editable attribute on UsernameField");
            Assert.IsTrue(attr.AllowEdit, "AllowEdit should be true for UsernameField");
            Assert.IsFalse(attr.AllowInitialValue, "AllowInitialValue should be false for UsernameField");
        }
コード例 #20
0
        public bool GetProperties(WebServer server, HttpListenerContext context)
        {
            this.Authenticate(server, context);

            try
            {
                // Init
                Entity obj = _getObjectForContext(context);

                // Setup a list of editable properties
                List <EditableProperty> props = new List <EditableProperty>();
                {
                    EditableProperty prop = new EditableProperty();
                    prop.Name     = "Id";
                    prop.Value    = obj.RowId.ToString();
                    prop.Readonly = "readonly";
                    props.Add(prop);
                }

                // Populate list using reflection
                foreach (var propInfo in obj.GetType().GetProperties())
                {
                    object[] attributes = propInfo.GetCustomAttributes(typeof(EditableAttribute), true);
                    if (attributes.Length == 1)
                    {
                        // Attribute
                        EditableAttribute attribute = attributes[0] as EditableAttribute;
                        // Get value
                        object val = propInfo.GetValue(obj);
                        // roperty with my custom attribute
                        EditableProperty prop = new EditableProperty();
                        prop.Name        = propInfo.Name;
                        prop.Value       = val == null ? "" : val.ToString();
                        prop.Placeholder = attribute.Placeholder;
                        // Readonly?
                        prop.Readonly = propInfo.GetCustomAttributes(typeof(ReadonlyAttribute), true).Length == 1 ? "readonly" : "";
                        props.Add(prop);
                    }
                }

                return(context.JsonResponse(props));
            }
            catch (Exception ex)
            {
                return(HandleError(context, ex, (int)HttpStatusCode.InternalServerError));
            }
        }
        public void EFMetadataProvider_AttributeInference()
        {
            HttpConfiguration        configuration        = new HttpConfiguration();
            HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor
            {
                Configuration  = configuration,
                ControllerType = typeof(NorthwindEFTestController),
            };
            DataControllerDescription    description = GetDataControllerDescription(typeof(NorthwindEFTestController));
            PropertyDescriptorCollection properties  = TypeDescriptor.GetProperties(typeof(Microsoft.Web.Http.Data.Test.Models.EF.Product));

            // verify key attribute
            Assert.NotNull(properties["ProductID"].Attributes[typeof(KeyAttribute)]);
            Assert.Null(properties["ProductName"].Attributes[typeof(KeyAttribute)]);

            // verify StringLengthAttribute
            StringLengthAttribute sla = (StringLengthAttribute)properties["ProductName"].Attributes[typeof(StringLengthAttribute)];

            Assert.NotNull(sla);
            Assert.Equal(40, sla.MaximumLength);

            // verify RequiredAttribute
            RequiredAttribute ra = (RequiredAttribute)properties["ProductName"].Attributes[typeof(RequiredAttribute)];

            Assert.NotNull(ra);
            Assert.False(ra.AllowEmptyStrings);

            // verify association attribute
            AssociationAttribute aa = (AssociationAttribute)properties["Category"].Attributes[typeof(AssociationAttribute)];

            Assert.NotNull(aa);
            Assert.Equal("Category_Product", aa.Name);
            Assert.True(aa.IsForeignKey);
            Assert.Equal("CategoryID", aa.ThisKey);
            Assert.Equal("CategoryID", aa.OtherKey);

            // verify metadata from "buddy class"
            PropertyDescriptor pd = properties["QuantityPerUnit"];

            sla = (StringLengthAttribute)pd.Attributes[typeof(StringLengthAttribute)];
            Assert.NotNull(sla);
            Assert.Equal(777, sla.MaximumLength);
            EditableAttribute ea = (EditableAttribute)pd.Attributes[typeof(EditableAttribute)];

            Assert.False(ea.AllowEdit);
            Assert.True(ea.AllowInitialValue);
        }
コード例 #22
0
        public void GetDisplayDetails_EditableAttributeFalse_SetsReadOnlyTrue()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var editable = new EditableAttribute(allowEdit: false);

            var attributes = new Attribute[] { editable };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new BindingMetadataProviderContext(key, attributes);

            // Act
            provider.GetBindingMetadata(context);

            // Assert
            Assert.True(context.BindingMetadata.IsReadOnly);
        }
コード例 #23
0
        public void CreateBindingMetadata_EditableAttributeTrue_SetsReadOnlyFalse()
        {
            // Arrange
            var provider = new DataAnnotationsMetadataProvider();

            var editable = new EditableAttribute(allowEdit: true);

            var attributes = new Attribute[] { editable };
            var key        = ModelMetadataIdentity.ForType(typeof(string));
            var context    = new BindingMetadataProviderContext(key, new ModelAttributes(attributes));

            // Act
            provider.CreateBindingMetadata(context);

            // Assert
            Assert.False(context.BindingMetadata.IsReadOnly);
        }
コード例 #24
0
        public static bool IsColumnEditable(object descriptor)
        {
            if (IsObjectOfType <PropertyDescriptor>(descriptor))
            {
                var propertyDescriptor = GetObjectAsType <PropertyDescriptor>(descriptor);
                EditableAttribute editableAttribute = null;
                return(TryGetAttribute(propertyDescriptor, out editableAttribute) && editableAttribute.AllowEdit);
            }

            if (IsObjectOfType <PropertyInfo>(descriptor))
            {
                var propertyInfo = GetObjectAsType <PropertyInfo>(descriptor);
                EditableAttribute editableAttribute = null;
                return(TryGetAttribute(propertyInfo, out editableAttribute) && editableAttribute.AllowEdit);
            }
            return(false);
        }
コード例 #25
0
        public ModelMetadata BuildUp(ModelMetadata metadata, IEnumerable <Attribute> attributes,
                                     Type containerType,
                                     Func <object> modelAccessor,
                                     Type modelType,
                                     string propertyName)
        {
            // Prefer [Display(Name="")] to [DisplayName]
            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (display != null)
            {
                string name = display.GetName();
                if (name != null)
                {
                    metadata.DisplayName = name;
                }

                // There was no 3.5 way to set these values
                metadata.Description      = display.GetDescription();
                metadata.ShortDisplayName = display.GetShortName();
                metadata.Watermark        = display.GetPrompt();
            }

            // Prefer [Editable] to [ReadOnly]
            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                metadata.IsReadOnly = !editable.AllowEdit;
            }

            // If [DisplayFormat(HtmlEncode=false)], set a data type name of "Html"
            // (if they didn't already set a data type)
            DisplayFormatAttribute displayFormat = attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormat != null &&
                !displayFormat.HtmlEncode &&
                String.IsNullOrWhiteSpace(metadata.DataTypeName))
            {
                metadata.DataTypeName = DataType.Html.ToString();
            }

            return(metadata);
        }
コード例 #26
0
 public EditingProperty(PropertyInfo propertyInfo, EditableAttribute editableAttribute, object editingObject)
 {
     if (propertyInfo == null)
     {
         throw new ArgumentNullException("propertyInfo");
     }
     if (editableAttribute == null)
     {
         throw new ArgumentNullException("editableAttribute");
     }
     if (editingObject == null)
     {
         throw new ArgumentNullException("editingObject");
     }
     _propertyInfo      = propertyInfo;
     _editableAttribute = editableAttribute;
     _editingObject     = editingObject;
     _originalValue     = GetValue();
 }
コード例 #27
0
        /// <summary>
        /// Returns true if the specified property is read-only by virtue of having
        /// an appropriately configured ReadOnlyAttribute or EditableAttribute applied.
        /// </summary>
        /// <param name="property">The property to check for editability.</param>
        /// <returns>True if the specified property is read-only, false otherwise.</returns>
        protected bool IsPropertyReadOnly(PropertyDescriptor property)
        {
            // Here, we continue to respect the [ReadOnly] attribute because TypeDescriptor
            // will materialize this when a property setter is not available.
            ReadOnlyAttribute readOnlyAttr = property.Attributes[typeof(ReadOnlyAttribute)] as ReadOnlyAttribute;

            if (readOnlyAttr != null && readOnlyAttr.IsReadOnly)
            {
                return(true);
            }

            EditableAttribute editableAttribute = property.Attributes[typeof(EditableAttribute)] as EditableAttribute;

            if (editableAttribute != null && !editableAttribute.AllowEdit)
            {
                return(true);
            }

            return(false);
        }
コード例 #28
0
        public bool GetPropertyIsReadOnly(string propertyName)
        {
            if (this.DataType != null)
            {
                if (!String.IsNullOrEmpty(propertyName))
                {
                    Type          propertyType  = this.DataType;
                    PropertyInfo  propertyInfo  = null;
                    List <string> propertyNames = TypeHelper.SplitPropertyPath(propertyName);
                    for (int i = 0; i < propertyNames.Count; i++)
                    {
                        object[] index = null;
                        propertyInfo = propertyType.GetPropertyOrIndexer(propertyNames[i], out index);
                        if (propertyInfo == null || propertyType.GetIsReadOnly() || propertyInfo.GetIsReadOnly())
                        {
                            // Either the data type is read-only, the property doesn't exist, or it does exist but is read-only
                            return(true);
                        }

                        // Check if EditableAttribute is defined on the property and if it indicates uneditable
                        object[] attributes = propertyInfo.GetCustomAttributes(typeof(EditableAttribute), true);
                        if (attributes != null && attributes.Length > 0)
                        {
                            EditableAttribute editableAttribute = attributes[0] as EditableAttribute;
                            Debug.Assert(editableAttribute != null);
                            if (!editableAttribute.AllowEdit)
                            {
                                return(true);
                            }
                        }
                        propertyType = propertyInfo.PropertyType.GetNonNullableType();
                    }
                    return(propertyInfo == null || !propertyInfo.CanWrite || !this.AllowEdit || !CanEdit(propertyType));
                }
                else if (this.DataType.GetIsReadOnly())
                {
                    return(true);
                }
            }
            return(!this.AllowEdit);
        }
コード例 #29
0
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType,
                                                        Func <object> modelAccessor, Type modelType, string propertyName)
        {
            ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName);

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();

            if (display != null)
            {
                string name = display.GetName();
                if (name != null)
                {
                    metadata.DisplayName = name;
                }

                metadata.Description      = display.GetDescription();
                metadata.ShortDisplayName = display.GetShortName();
                metadata.Watermark        = display.GetPrompt();
            }

            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                metadata.IsReadOnly = !editable.AllowEdit;
            }

            DisplayFormatAttribute displayFormat = attributes.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormat != null && !displayFormat.HtmlEncode && String.IsNullOrWhiteSpace(metadata.DataTypeName))
            {
                metadata.DataTypeName = DataType.Html.ToString();
            }

            foreach (IMetadataAware awareAttribute in attributes.OfType <IMetadataAware>())
            {
                awareAttribute.OnMetadataCreated(metadata);
            }

            return(metadata);
        }
コード例 #30
0
        /// <summary>
        /// Generates a <see cref="AttributeDeclaration"/> representation of an
        /// <see cref="EditableAttribute"/> instance.
        /// </summary>
        /// <param name="attribute">The <see cref="EditableAttribute"/>.</param>
        /// <returns>A <see cref="AttributeDeclaration"/> representation of
        /// <paramref name="attribute"/>.</returns>
        /// <exception cref="InvalidCastException">if <paramref name="attribute"/> is
        /// not a <see cref="EditableAttribute"/>.</exception>
        public override AttributeDeclaration GetAttributeDeclaration(Attribute attribute)
        {
            EditableAttribute    editableAttribute    = (EditableAttribute)attribute;
            AttributeDeclaration attributeDeclaration = new AttributeDeclaration(typeof(EditableAttribute));

            bool allowEdit         = editableAttribute.AllowEdit;
            bool allowInitialValue = editableAttribute.AllowInitialValue;

            // [EditableAttribute( {true|false} )]
            attributeDeclaration.ConstructorArguments.Add(allowEdit);

            // Only add the 'AllowInitialValue' parameter if its value does not match with
            // the 'AllowEdit' value.  See the documentation of EditableAttribute for more info.
            if (allowEdit != allowInitialValue)
            {
                // [EditableAttribute( {true|false}, AllowInitialValue = {true|false} )]
                attributeDeclaration.NamedParameters.Add("AllowInitialValue", allowInitialValue);
            }

            return(attributeDeclaration);
        }
コード例 #31
0
        /// <summary>
        /// Returns a collection of all the <see cref="Attribute"/>s we infer from the metadata associated
        /// with the metadata member corresponding to the given property descriptor
        /// </summary>
        /// <param name="pd">A <see cref="System.ComponentModel.PropertyDescriptor"/> to examine</param>
        /// <returns>A collection of attributes inferred from the metadata in the given descriptor.</returns>
        protected override IEnumerable <Attribute> GetMemberAttributes(PropertyDescriptor pd)
        {
            var attributes = new List <Attribute>();

            // Exclude any EntityState, EntityReference, etc. members
            if (ShouldExcludeEntityMember(pd))
            {
                // for these members, we don't want to do any attribute inference
                return(attributes.ToArray());
            }

            EditableAttribute editableAttribute = null;
            var inferRoundtripOriginalAttribute = false;

            var hasKeyAttribute = (pd.Attributes[typeof(KeyAttribute)] != null);
            var isEntity        = EdmType.BuiltInTypeKind == BuiltInTypeKind.EntityType;

            if (isEntity)
            {
                var entityType = (EntityType)EdmType;
                var keyMember  = entityType.KeyMembers.SingleOrDefault(k => k.Name == pd.Name);
                if (keyMember != null && !hasKeyAttribute)
                {
                    attributes.Add(new KeyAttribute());
                    hasKeyAttribute = true;
                }
            }

            var member = EdmType.Members.SingleOrDefault(p => p.Name == pd.Name) as EdmProperty;

            if (member != null)
            {
                if (hasKeyAttribute)
                {
                    ;

                    // key members that aren't also FK members are non-editable (but allow an initial value)
                    if (!_keyIsEditable)
                    {
                        editableAttribute = new EditableAttribute(false)
                        {
                            AllowInitialValue = true
                        };
                    }
                }

                // Check if the member is DB generated and add the DatabaseGeneratedAttribute to it if not already present.
                if (pd.Attributes[typeof(DatabaseGeneratedAttribute)] == null)
                {
                    var md = ObjectContextUtilities.GetStoreGeneratedPattern(member);
                    if (md != null)
                    {
                        if ((string)md.Value == "Computed")
                        {
                            attributes.Add(new DatabaseGeneratedAttribute(DatabaseGeneratedOption.Computed));
                        }
                        else if ((string)md.Value == "Identity")
                        {
                            attributes.Add(new DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity));
                        }
                    }
                }

                // Add implicit ConcurrencyCheck attribute to metadata if ConcurrencyMode is anything other than ConcurrencyMode.None
                var facet = member.TypeUsage.Facets.SingleOrDefault(p => p.Name == "ConcurrencyMode");
                if (facet != null && facet.Value != null && (ConcurrencyMode)facet.Value != ConcurrencyMode.None &&
                    pd.Attributes[typeof(ConcurrencyCheckAttribute)] == null)
                {
                    attributes.Add(new ConcurrencyCheckAttribute());
                    inferRoundtripOriginalAttribute = true;
                }

                var isStringType = pd.PropertyType == typeof(string) || pd.PropertyType == typeof(char[]);

                // Add Required attribute to metdata if the member cannot be null and it is either a reference type or a Nullable<T>
                if (!member.Nullable && (!pd.PropertyType.IsValueType || IsNullableType(pd.PropertyType)) &&
                    pd.Attributes[typeof(RequiredAttribute)] == null)
                {
                    attributes.Add(new RequiredAttribute());
                }

                if (isStringType &&
                    pd.Attributes[typeof(StringLengthAttribute)] == null)
                {
                    facet = member.TypeUsage.Facets.SingleOrDefault(p => p.Name == "MaxLength");
                    if (facet != null && facet.Value != null && facet.Value is int)
                    {
                        // need to test for Type int, since the value can also be of type
                        // System.Data.Metadata.Edm.EdmConstants.Unbounded
                        var maxLength = (int)facet.Value;
                        attributes.Add(new StringLengthAttribute(maxLength));
                    }
                }

                var hasTimestampAttribute = (pd.Attributes[typeof(TimestampAttribute)] != null);

                if (_timestampMember == member && !hasTimestampAttribute)
                {
                    attributes.Add(new TimestampAttribute());
                    hasTimestampAttribute = true;
                }

                // All members marked with TimestampAttribute (inferred or explicit) need to
                // have [Editable(false)] and [RoundtripOriginal] applied
                if (hasTimestampAttribute)
                {
                    if (editableAttribute == null)
                    {
                        editableAttribute = new EditableAttribute(false);
                    }
                }
            }

            // Add the Editable attribute if required
            if (editableAttribute != null && pd.Attributes[typeof(EditableAttribute)] == null)
            {
                attributes.Add(editableAttribute);
            }

            if (isEntity)
            {
                AddAssociationAttributes(pd, attributes);
            }

            return(attributes.ToArray());
        }
コード例 #32
0
 public void Ctor(bool value)
 {
     var attribute = new EditableAttribute(value);
     Assert.Equal(value, attribute.AllowEdit);
     Assert.Equal(value, attribute.AllowInitialValue);
 }