public static void ResolveFkAttributes(Type foreignType, ColumnMeta fkColumn)
        {
            var foreignMeta = _metaCache.GetOrAdd(foreignType, AddMeta);

            fkColumn.PrimaryTable = foreignMeta;
            fkColumn.Template     = "Lookup";
        }
        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();

            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 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);
                    }
                }
                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);
            return(tm);
        }
Beispiel #3
0
 public void Add(ColumnMeta column)
 {
     Columns.Add(column);
 }