Пример #1
0
        /// <summary>
        /// Carrega os relacionamentos do grupo de chaves estrangeiras informado.
        /// </summary>
        /// <param name="info">Informações do grupo das chaves estrangeiras.</param>
        /// <returns>Lista da com os relacionamentos da foreignKey.</returns>
        internal static List <ForeignKeyMapper> LoadRelationships(Type typeModel, GroupOfRelationshipInfo info)
        {
            List <GroupOfRelationshipInfo> groups = MappingManager.GetForeignKeyAttributes(typeModel);
            int index = groups.FindIndex(delegate(GroupOfRelationshipInfo gri) {
                return(gri == info);
            });

            if (index >= 0)
            {
                return(groups[index].ForeignKeys);
            }
            else
            {
                return(new List <ForeignKeyMapper>());
            }
        }
Пример #2
0
        /// <summary>
        /// Carrega a lista das propriedades mapeadas.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="mapping">Lista para armazenar a propriedades mapeadas.</param>
        /// <param name="fkMapping"></param>
        /// <param name="fmMapping">Mapeamento dos membros estrangeiros.</param>
        private static void LoadPersistencePropertyAttributes(Type type, List <Mapper> mapping, List <GroupOfRelationshipInfo> fkMapping, List <ForeignMemberMapper> fmMapping)
        {
            PropertyInfo[] properties   = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            var            classMapping = Mapping.MappingData.GetMapping(type);

            foreach (PropertyInfo property in properties)
            {
                Mapping.PropertyMapping pMapping = null;
                if (classMapping != null)
                {
                    pMapping = classMapping.Properties.Find(f => f.Name == property.Name);
                }
                PersistencePropertyAttribute ppa = GetPersistenceProperty(property);
                Mapper currentMapper             = null;
                if (ppa != null)
                {
                    currentMapper = new Mapper(type, ppa, property);
                    mapping.Add(currentMapper);
                }
                object[] attrs = property.GetCustomAttributes(typeof(PersistenceForeignKeyAttribute), true);
                if ((attrs == null || attrs.Length == 0) && pMapping != null && pMapping.ForeignKey != null)
                {
                    attrs = new object[] {
                        pMapping.ForeignKey.GetPersistenceForeignKey()
                    }
                }
                ;
                foreach (object obj in attrs)
                {
                    PersistenceForeignKeyAttribute fk    = (PersistenceForeignKeyAttribute)obj;
                    GroupOfRelationshipInfo        group = new GroupOfRelationshipInfo(type, fk.TypeOfClassRelated, fk.GroupOfRelationship);
                    int index = fkMapping.FindIndex(delegate(GroupOfRelationshipInfo gri) {
                        return(gri == group);
                    });
                    if (index < 0)
                    {
                        fkMapping.Add(group);
                    }
                    else
                    {
                        group = fkMapping[index];
                    }
                    ForeignKeyMapper fkm = new ForeignKeyMapper(fk, property);
                    group.AddForeignKey(fkm);
                    if (currentMapper != null)
                    {
                        currentMapper.AddForeignKey(fkm);
                    }
                }
                attrs = property.GetCustomAttributes(typeof(ValidatorAttribute), true);
                if ((attrs == null || attrs.Length == 0) && pMapping != null && pMapping.Validators.Count > 0)
                {
                    attrs = new object[pMapping.Validators.Count];
                    for (int i = 0; i < attrs.Length; i++)
                    {
                        attrs[i] = pMapping.Validators[i].GetValidator();
                    }
                }
                if (attrs.Length > 0)
                {
                    ValidatorAttribute[] vAttrs = new ValidatorAttribute[attrs.Length];
                    for (int i = 0; i < attrs.Length; i++)
                    {
                        vAttrs[i] = (ValidatorAttribute)attrs[i];
                    }
                    currentMapper.Validation = new ValidationMapper(currentMapper, vAttrs);
                }
                attrs = property.GetCustomAttributes(typeof(PersistenceForeignMemberAttribute), true);
                if ((attrs == null || attrs.Length == 0) && pMapping != null && pMapping.ForeignMember != null)
                {
                    attrs = new object[] {
                        pMapping.ForeignMember.GetPersistenceForeignMember()
                    }
                }
                ;
                if (attrs.Length > 0)
                {
                    fmMapping.Add(new ForeignMemberMapper((PersistenceForeignMemberAttribute)attrs[0], property));
                }
            }
            if (!type.IsInterface)
            {
                Type[] interfaces = type.GetInterfaces();
                PersistenceClassAttribute pca;
                for (int i = 0; i < interfaces.Length; i++)
                {
                    pca = GetPersistenceClassAttribute(interfaces[i]);
                    if (pca != null)
                    {
                        LoadPersistencePropertyAttributes(interfaces[i], mapping, fkMapping, fmMapping);
                    }
                }
            }
        }