コード例 #1
0
        public static void ResolveFkAttributes(Type foreignType, ColumnMeta fkColumn)
        {
            var foreignMeta = _metaCache.GetOrAdd(foreignType, AddMeta);

            fkColumn.PrimaryTable = foreignMeta;
            fkColumn.Template     = "Lookup";
        }
コード例 #2
0
 public void Add(ColumnMeta column)
 {
     Columns.Add(column);
 }
コード例 #3
0
        private static TableMeta AddMeta(Type _type)
        {
            var tm = new TableMeta();

            tm.Caption         = _type.GetCustomAttribute <DisplayNameAttribute>();
            tm.DisplayColumn   = _type.GetCustomAttribute <DisplayColumnAttribute>();
            tm.ActualTableName = _type.GetCustomAttribute <TableAttribute>()?.Name ?? _type.Name;

            List <PropertyInfo> ShowColumns = new List <PropertyInfo>();

            PropertyInfo[] columns      = _type.GetProperties();
            ColumnMeta     actualMaster = null;

            foreach (var col in columns)
            {
                var colMeta = new ColumnMeta(col);
                tm.Add(colMeta);
            }

            var removeList = new List <ColumnMeta>();

            foreach (var colMeta in tm.Columns)
            {
                var col        = colMeta.Property;
                var skipColumn = false;
                foreach (var attr in col.GetCustomAttributes())
                {
                    if (attr is KeyAttribute)
                    {
                        tm.PrimaryColumn = col;
                    }
                    else if (attr is DisplayAttribute)
                    {
                        colMeta.Display = attr as DisplayAttribute;
                    }
                    else if (attr is RequiredAttribute)
                    {
                        colMeta.Required = true;
                    }
                    else if (attr is NotMappedAttribute)
                    {
                        colMeta.NotMapped = true;
                    }
                    else if (attr is ReadOnlyAttribute)
                    {
                        colMeta.IsReadonly = true;
                    }
                    else if (attr is ForeignKeyAttribute)
                    {
                        var fkAttr = attr as ForeignKeyAttribute;
                        if (fkAttr != null)
                        {
                            // Ilgili kolonu bul
                            var fkCol = tm.Columns.First(c => c.Property.Name == (fkAttr as ForeignKeyAttribute).Name);

                            ResolveFkAttributes(col.PropertyType, fkCol);
                            // bu kolonu atla
                            skipColumn = true;
                        }
                    }
                    else if (attr is DataTypeAttribute)
                    {
                        colMeta.DataType = attr as DataTypeAttribute;
                    }
                    else if (attr is ScaffoldColumnAttribute)
                    {
                        colMeta.IsVisible = (attr as ScaffoldColumnAttribute).Scaffold;
                    }
                    else if (attr is UIHintAttribute)
                    {
                        colMeta.UIHint = (attr as UIHintAttribute);
                    }
                    else if (attr is UIAttribute)
                    {
                        var cpcn = (attr as UIAttribute).CascadeParentColumnName;

                        colMeta.ParentColumn = tm.Columns.First(c => c.Property.Name == cpcn);
                        if ((attr as UIAttribute).IsMaster)
                        {
                            actualMaster = colMeta;
                        }
                    }
                }
                if (skipColumn)
                {
                    removeList.Add(colMeta);
                    continue;
                }
                colMeta.InnerType = Nullable.GetUnderlyingType(col.PropertyType);
                if (colMeta.InnerType != null)
                {
                    colMeta.Required = false;
                }
                else
                {
                    colMeta.InnerType = col.PropertyType;
                }
                // Eğer enum varsa
                if (colMeta.InnerType.IsEnum)
                {
                    var names  = Enum.GetNames(colMeta.InnerType).AsQueryable();
                    var values = Enum.GetValues(colMeta.InnerType).Cast <int>().AsQueryable();

                    colMeta.SetManualLookup(
                        names.Zip(values, (n, v) => new ListItem {
                        Id = Convert.ToInt32(v), Text = n
                    }).ToList()
                        );
                }

                // Datatype verilmemişse, anlamaya çalış.
                if (colMeta.DataType == null)
                {
                    DataType dt;
                    if (_type2dt.TryGetValue(colMeta.InnerType, out dt))
                    {
                        colMeta.DataType = new DataTypeAttribute(dt);
                    }
                }
            }
            removeList.ForEach(c => tm.Columns.Remove(c));

            // Order'a göre sırala.
            tm.Columns.Sort(defaultComparer);
            // Nesnenin bariz master ı varsa bul ve set et.
            var fks = tm.Columns.Where(c => c.PrimaryTable != null).ToArray();

            switch (fks.Length)
            {
            case 0:
                break;

            case 1:
                tm.MasterFk = fks[0];
                break;

            default:
                tm.MasterFk = actualMaster;
                break;
            }
            if (!_metaCacheByName.ContainsKey(tm.ActualTableName))
            {
                _metaCacheByName.Add(tm.ActualTableName, tm);
            }

            return(tm);
        }