Example #1
0
        /// <summary>
        /// File joined table instance to field that marked foreign key
        /// </summary>
        /// <param name="fieldName"></param>
        /// <param name="value"></param>
        public virtual void SetJoinedObjValue(string fieldName, IModel obj)
        {
            TableInfo ti    = GetTableInfo();
            FieldInfo field = ti[fieldName];

            Action <IModel, object> setter = field.Setter;

            setter(this, obj);

            join = true;
        }
Example #2
0
        public void Init <T>() where T : IModel
        {
            TableInfo tbl = new TableInfo
            {
                FieldName   = typeof(T).Name,
                Constructor = Serializer.CreateCtor <T>(),
                Name        = typeof(T).Name
            };
            int index = 0;


            PropertyInfo[] property_infos = (typeof(T)).GetProperties(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo info in property_infos)
            {
                object[]  attrs = info.GetCustomAttributes(true);
                FieldInfo ci    = new FieldInfo
                {
                    Name = info.Name
                };

                bool isField = false;
                foreach (object attr in attrs)
                {
                    Column columnName = attr as Column;
                    if (columnName != null)
                    {
                        ci.FieldName = columnName.ColumnName;
                        isField      = true;
                    }
                }

                if (!isField)
                {
                    continue;
                }

                if (!info.PropertyType.IsArray)
                {
                    if (info.CanRead && info.CanWrite)
                    {
                        ci.MethodDelegates = info;
                        ci.Setter          = Serializer.CreateSetter(info);
                        ci.Getter          = Serializer.CreateGetter(info);
                        tbl.AddMap(info.Name, index++);

                        if (info.PropertyType.IsSubclassOf(typeof(ModelTable)))
                        {
                            ci.IsForeignKey = true;
                            ci.ForeignClass = info.PropertyType;
                        }
                        else
                        {
                            ci.IsForeignKey = false;
                            ci.ForeignClass = null;
                        }
                    }
                }

                tbl[info.Name] = ci;
            }

            ModelFactory mf = ModelFactory.GetInstance();

            mf[tbl.Name] = tbl;
        }