Пример #1
0
        protected static AttributeCollection AddDefaultAttributes(ColumnProvider columnProvider, AttributeCollection attributes) {
            List<Attribute> extraAttributes = new List<Attribute>();

            // If there is no required attribute and the Provider says required, add one
            var requiredAttribute = attributes.FirstOrDefault<RequiredAttribute>();
            if (requiredAttribute == null && !columnProvider.Nullable) {
                extraAttributes.Add(new RequiredAttribute());
            }

            // If there is no StringLength attribute and it's a string, add one
            var stringLengthAttribute = attributes.FirstOrDefault<StringLengthAttribute>();
            int maxLength = columnProvider.MaxLength;
            if (stringLengthAttribute == null && columnProvider.ColumnType == typeof(String) && maxLength > 0) {
                extraAttributes.Add(new StringLengthAttribute(maxLength));
            }

            // If we need any extra attributes, create a new collection
            if (extraAttributes.Count > 0) {
                attributes = AttributeCollection.FromExisting(attributes, extraAttributes.ToArray());
            }

            return attributes;
        }