File: ColumnProvider.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (157 lines) | stat: -rw-r--r-- 6,120 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Web.DynamicData;
using System.Web.DynamicData.Util;

namespace System.Web.DynamicData.ModelProviders {
    /// <summary>
    /// Base provider class for columns.
    /// Each provider type (e.g. Linq To Sql, Entity Framework, 3rd party) extends this class.
    /// </summary>
    public abstract class ColumnProvider {
        private bool? _isReadOnly;
    
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="table">the table this column belongs to</param>
        protected ColumnProvider(TableProvider table) {
            if (table == null) {
                throw new ArgumentNullException("table");
            }

            Table = table;
        }

        /// <summary>
        /// readable representation
        /// </summary>
        /// <returns></returns>
        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        public override string ToString() {
            // To help identifying objects in debugger
            return Name ?? base.ToString();
        }

        internal virtual PropertyDescriptor PropertyDescriptor {
            get {
                return Table.GetTypeDescriptor().GetProperties().Find(Name, true/*ignoreCase*/);
            }
        }

        public virtual AttributeCollection Attributes {
            get {
                var propertyDescriptor = PropertyDescriptor;
                var attributes = propertyDescriptor != null ? propertyDescriptor.Attributes : AttributeCollection.Empty;
                return AddDefaultAttributes(this, attributes);
            }
        }

        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;
        }

        /// <summary>
        /// The name of the column
        /// </summary>
        public virtual string Name { get; protected set; }

        /// <summary>
        /// The CLR type of the column
        /// </summary>
        public virtual Type ColumnType { get; protected set; }

        /// <summary>
        /// Is this column a primary key in its table
        /// </summary>
        public virtual bool IsPrimaryKey { get; protected set; }

        /// <summary>
        /// Specifies if this column is read only
        /// </summary>
        public virtual bool IsReadOnly {
            get {
                if (_isReadOnly == null) {
                    var propertyDescriptor = PropertyDescriptor;
                    _isReadOnly = propertyDescriptor != null ? propertyDescriptor.IsReadOnly : false;
                }
                return _isReadOnly.Value;
            }
            protected set {
                _isReadOnly = value;
            }
        }

        /// <summary>
        /// Is it a database generated column
        /// </summary>
        public virtual bool IsGenerated { get; protected set; }

        /// <summary>
        /// Returns whether the underlying model supports sorting of the table on this column
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Sortable", Justification="It's a valid word")]
        public virtual bool IsSortable { get; protected set; }

        /// <summary>
        /// The maximun length allowed for this column (applies to string columns)
        /// </summary>
        public virtual int MaxLength { get; protected set; }

        /// <summary>
        /// Does it allow null values (meaning it is not required)
        /// </summary>
        public virtual bool Nullable { get; protected set; }

        /// <summary>
        /// meant to indicate that a member is an extra property that was declared in a partial class
        /// </summary>
        public virtual bool IsCustomProperty { get; protected set; }

        /// <summary>
        /// If the column represents and association with anther table, this returns the association information.
        /// Otherwise, null is returned.
        /// </summary>
        public virtual AssociationProvider Association { get; protected set; }

        /// <summary>
        /// The table that this column belongs to
        /// </summary>
        public TableProvider Table { get; private set; }

        /// <summary>
        /// The PropertyInfo of the property that represents this column on the entity type
        /// </summary>
        public virtual PropertyInfo EntityTypeProperty { get; protected set; }

        /// <summary>
        /// This is set for columns that are part of a foreign key. Note that it is NOT set for
        /// the strongly typed entity ref columns (though those columns 'use' one or more columns
        /// where IsForeignKeyComponent is set).
        /// </summary>
        public virtual bool IsForeignKeyComponent { get; protected set; }
    }
}