Ejemplo n.º 1
0
        public TypeTable(Type type)
        {
            ViewSource     = type.Name;
            UpdateSource   = type.Name;
            TypeName       = type.FullName;
            m_assemblyName = type.Assembly.FullName;

            var tattr = type.GetCustomAttributes(true);

            foreach (var attr in tattr.Where(a => a.GetType() == typeof(Source)))
            {
                var sourceAttribute = attr as Source;
                if (sourceAttribute == null)
                {
                    continue;
                }

                var view   = sourceAttribute.View;
                var update = sourceAttribute.Update;

                ViewSource   = view == string.Empty ? type.Name : view;
                UpdateSource = update == string.Empty ? type.Name : update;
            }

            var minfoArray = type.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (var info in minfoArray.Where(i => i.GetCustomAttributes(typeof(DataField), true).Length > 0))
            {
                //Get DataFields
                var mattr = info.GetCustomAttributes(typeof(DataField), true);

                var dataField = mattr[0] as DataField;
                if (dataField == null)
                {
                    continue;
                }


                var columnName = dataField.FieldName;
                var memberName = dataField.MemberName ?? info.Name;

                var isPrimaryKey = dataField.PrimaryKey;
                var isIdentity   = dataField.Identity;
                var size         = dataField.Size;

                var memberInfo = GetMemberInfo(type, memberName);

                var viewOnly = dataField.ViewOnly;

                switch (memberInfo.MemberType)
                {
                case MemberTypes.Property:
                    Add(new TypeColumn(columnName, info.Name, (memberInfo as PropertyInfo), viewOnly, isPrimaryKey, isIdentity, size));
                    break;

                case MemberTypes.Field:
                    Add(new TypeColumn(columnName, info.Name, (memberInfo as FieldInfo), viewOnly, isPrimaryKey, isIdentity, size));
                    break;
                }
            }

            //Get Relations
            foreach (var info in minfoArray.Where(i => i.GetCustomAttributes(typeof(Association), true).Length > 0))
            {
                var rattr = info.GetCustomAttributes(typeof(Association), true);

                var association = rattr[0] as Association;
                if (association == null)
                {
                    continue;
                }

                var          memberInfo = GetMemberInfo(type, association.SourceMember);
                TypeRelation relation   = null;


                var sourceKeyColumn = this.First(col => col.Name == association.SourceKey);

                switch (memberInfo.MemberType)
                {
                case MemberTypes.Property:
                    var pInfo = memberInfo as PropertyInfo;
                    if (pInfo == null)
                    {
                        break;
                    }
                    relation = new TypeRelation(association.Relationship,
                                                pInfo.PropertyType,
                                                association.SourceMember,
                                                association.SourceKey,
                                                association.ReferenceKey,
                                                memberInfo,
                                                sourceKeyColumn);
                    break;

                case MemberTypes.Field:
                    var fInfo = memberInfo as FieldInfo;
                    if (fInfo == null)
                    {
                        break;
                    }
                    relation = new TypeRelation(association.Relationship,
                                                fInfo.FieldType,
                                                association.SourceMember,
                                                association.SourceKey,
                                                association.ReferenceKey,
                                                memberInfo,
                                                sourceKeyColumn);
                    break;
                }
                Associations.Add(relation);
            }

            MappedObjectType = type;
        }
Ejemplo n.º 2
0
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                //Get table attributes
                TypeName     = reader.GetAttribute("Type");
                ViewSource   = reader.GetAttribute("View");
                UpdateSource = reader.GetAttribute("Update");
                string assembly = reader.GetAttribute("Assembly");

                //TODO find another way not to include assembly
                if ((ViewSource == null && UpdateSource == null) || assembly == null)
                {
                    //TODO throw exception here
                }

                if (ViewSource == null && UpdateSource != null)
                {
                    ViewSource = UpdateSource;
                }

                if (UpdateSource == null && ViewSource != null)
                {
                    UpdateSource = ViewSource;
                }


                //TODO Add try catch here
                var type = Type.GetType(string.Format("{0}, {1}", TypeName, assembly), true);

                if (type == null)
                {
                    throw new Exception(string.Format("Unknown Type {0}.{1}", assembly, TypeName));
                }


                MappedObjectType = type;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "Fields")
                    {
                        if (reader.IsEmptyElement)
                        {
                            continue;
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "DataField")
                            {
                                var column = new TypeColumn();
                                column.ReadXml(reader, type);
                                Add(column);
                            }
                            else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Fields")
                            {
                                break;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Element && reader.Name == "Associations")
                    {
                        if (reader.IsEmptyElement)
                        {
                            continue;
                        }
                        while (reader.Read())
                        {
                            if (reader.NodeType == XmlNodeType.Element && reader.Name == "Association")
                            {
                                var relation = new TypeRelation();

                                relation.ReadXml(reader, type, this);
                                Associations.Add(relation);
                            }
                            else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Associations")
                            {
                                break;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "DataObject")
                    {
                        break;
                    }
                }
            }
        }