Exemplo n.º 1
0
        public override MetaDataMember GetDataMember(MemberInfo mi)
        {
            if (mi == null)
            {
                throw Error.ArgumentNull("mi");
            }
            this.InitDataMembers();
            if (this.dataMemberMap == null)
            {
                lock (this.locktarget)
                {
                    if (this.dataMemberMap == null)
                    {
                        Dictionary <object, MetaDataMember> map = new Dictionary <object, MetaDataMember>();
                        foreach (MetaDataMember mm in this.dataMembers)
                        {
                            map.Add(InheritanceRules.DistinguishedMemberName(mm.Member), mm);
                        }
                        this.dataMemberMap = map;
                    }
                }
            }
            object         dn = InheritanceRules.DistinguishedMemberName(mi);
            MetaDataMember mdm;

            this.dataMemberMap.TryGetValue(dn, out mdm);
            return(mdm);
        }
Exemplo n.º 2
0
        private void Validate()
        {
            var dictionary = new Dictionary <object, string>();

            foreach (MetaType type in InheritanceTypes)
            {
                foreach (MetaDataMember member in type.PersistentDataMembers)
                {
                    if (member.IsDeclaredBy(type))
                    {
                        if (member.IsDiscriminator && !HasInheritance)
                        {
                            throw Error.NonInheritanceClassHasDiscriminator(type);
                        }
                        if (!member.IsAssociation && !string.IsNullOrEmpty(member.MappedName))
                        {
                            string str;
                            object key = InheritanceRules.DistinguishedMemberName(member.Member);
                            if (dictionary.TryGetValue(key, out str))
                            {
                                if (str != member.MappedName)
                                {
                                    throw Error.MemberMappedMoreThanOnce(member.Member.Name);
                                }
                            }
                            else
                            {
                                dictionary.Add(key, member.MappedName);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        public override MetaDataMember GetDataMember(MemberInfo mi)
        {
            MetaDataMember member2;

            if (mi == null)
            {
                throw Error.ArgumentNull("mi");
            }
            InitDataMembers();
            if (dataMemberMap == null)
            {
                lock (locktarget)
                {
                    if (dataMemberMap == null)
                    {
                        var dictionary = new Dictionary <object, MetaDataMember>();
                        foreach (var member in dataMembers)
                        {
                            dictionary.Add(InheritanceRules.DistinguishedMemberName(member.Member), member);
                        }
                        dataMemberMap = dictionary;
                    }
                }
            }
            object key = InheritanceRules.DistinguishedMemberName(mi);

            dataMemberMap.TryGetValue(key, out member2);
            return(member2);
        }
Exemplo n.º 4
0
        private void Validate()
        {
            var memberToColumn = new Dictionary <object, string>();

            foreach (var inheritanceType in InheritanceTypes)
            {
                if (inheritanceType != this)
                {
                    var attrs = ((AttributedMetaModel)Model).GetTableAttributes(inheritanceType.Type, false);
                    if (attrs.Any())
                    {
                        throw Error.InheritanceSubTypeIsAlsoRoot(inheritanceType.Type);
                    }
                }

                foreach (var persistentDataMember in inheritanceType.PersistentDataMembers)
                {
                    if (!persistentDataMember.IsDeclaredBy(inheritanceType))
                    {
                        continue;
                    }

                    if (persistentDataMember.IsDiscriminator && !HasInheritance)
                    {
                        throw Error.NonInheritanceClassHasDiscriminator(inheritanceType);
                    }

                    if (persistentDataMember.IsAssociation)
                    {
                        continue;
                    }

                    // validate that no database column is mapped twice
                    if (string.IsNullOrEmpty(persistentDataMember.MappedName))
                    {
                        continue;
                    }

                    string column;
                    var    distinguishedMemberName = InheritanceRules.DistinguishedMemberName(persistentDataMember.Member);
                    if (memberToColumn.TryGetValue(distinguishedMemberName, out column))
                    {
                        if (column != persistentDataMember.MappedName)
                        {
                            throw Error.MemberMappedMoreThanOnce(persistentDataMember.Member.Name);
                        }
                    }
                    else
                    {
                        memberToColumn.Add(distinguishedMemberName, persistentDataMember.MappedName);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void Validate()
        {
            Dictionary <object, string> dictionary = new Dictionary <object, string>();

            foreach (MetaType type in this.InheritanceTypes)
            {
                if (type != this)
                {
                    /*
                     * TableAttribute[] customAttributes = (TableAttribute[])type.Type.GetCustomAttributes(typeof(TableAttribute), false);
                     * if (customAttributes.Length > 0)
                     * {
                     *  throw Mapping.Error.InheritanceSubTypeIsAlsoRoot(type.Type);
                     * }
                     */
                    var tableAttribute = model.AttributeProvider.GetTableAttribute(type.Type);
                    if (tableAttribute != null)
                    {
                        throw Mapping.Error.InheritanceSubTypeIsAlsoRoot(type.Type);
                    }
                }
                foreach (MetaDataMember member in type.PersistentDataMembers)
                {
                    if (member.IsDeclaredBy(type))
                    {
                        if (member.IsDiscriminator && !this.HasInheritance)
                        {
                            throw Mapping.Error.NonInheritanceClassHasDiscriminator(type);
                        }
                        if (!member.IsAssociation && !string.IsNullOrEmpty(member.MappedName))
                        {
                            string str;
                            object key = InheritanceRules.DistinguishedMemberName(member.Member);
                            if (dictionary.TryGetValue(key, out str))
                            {
                                if (str != member.MappedName)
                                {
                                    throw Mapping.Error.MemberMappedMoreThanOnce(member.Member.Name);
                                }
                            }
                            else
                            {
                                dictionary.Add(key, member.MappedName);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 6
0
        private static int BuildFieldDeclarations(MetaType type, Dictionary <object, string> memberNameToMappedName, StringBuilder sb)
        {
            int n = 0;

            foreach (MetaDataMember mm in type.DataMembers)
            {
                // Only generate declarations for the current type.
                if (mm.IsDeclaredBy(type))
                {
                    if (!mm.IsAssociation)
                    {
                        if (mm.IsPersistent)
                        {
                            object dn = InheritanceRules.DistinguishedMemberName(mm.Member);
                            string mappedName;
                            if (memberNameToMappedName.TryGetValue(dn, out mappedName))
                            {
                                if (mappedName == mm.MappedName)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                memberNameToMappedName.Add(dn, mm.MappedName);
                            }
                            if (sb.Length > 0)
                            {
                                sb.Append(", ");
                            }
                            sb.AppendLine();
                            sb.Append(string.Format(Globalization.CultureInfo.InvariantCulture, "  {0} ", SqlIdentifier.QuoteCompoundIdentifier(mm.MappedName)));
                            if (!string.IsNullOrEmpty(mm.Expression))
                            {
                                // Generate "AS <expression>" for computed columns
                                sb.Append("AS " + mm.Expression);
                            }
                            else
                            {
                                sb.Append(GetDbType(mm));
                            }
                            n++;
                        }
                    }
                }
            }
            return(n);
        }
Exemplo n.º 7
0
        private void Validate()
        {
            Dictionary <object, string> memberToColumn = new Dictionary <object, string>();

            foreach (MetaType type in this.InheritanceTypes)
            {
                if (type != this)
                {
                    TableAttribute[] attrs = (TableAttribute[])type.Type.GetCustomAttributes(typeof(TableAttribute), false);
                    if (attrs.Length > 0)
                    {
                        throw Error.InheritanceSubTypeIsAlsoRoot(type.Type);
                    }
                }
                foreach (MetaDataMember mem in type.PersistentDataMembers)
                {
                    if (mem.IsDeclaredBy(type))
                    {
                        if (mem.IsDiscriminator && !this.HasInheritance)
                        {
                            throw Error.NonInheritanceClassHasDiscriminator(type);
                        }
                        if (!mem.IsAssociation)
                        {
                            // validate that no database column is mapped twice
                            if (!string.IsNullOrEmpty(mem.MappedName))
                            {
                                string column;
                                object dn = InheritanceRules.DistinguishedMemberName(mem.Member);
                                if (memberToColumn.TryGetValue(dn, out column))
                                {
                                    if (column != mem.MappedName)
                                    {
                                        throw Error.MemberMappedMoreThanOnce(mem.Member.Name);
                                    }
                                }
                                else
                                {
                                    memberToColumn.Add(dn, mem.MappedName);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        public override MetaDataMember GetDataMember(MemberInfo mi)
        {
            MetaDataMember member;

            if (mi == null)
            {
                throw Error.ArgumentNull("mi");
            }
            if (this.dataMemberMap.TryGetValue(InheritanceRules.DistinguishedMemberName(mi), out member))
            {
                return(member);
            }
            if (mi.DeclaringType.IsInterface)
            {
                throw Error.MappingOfInterfacesMemberIsNotSupported(mi.DeclaringType.Name, mi.Name);
            }
            throw Error.UnmappedClassMember(mi.DeclaringType.Name, mi.Name);
        }
Exemplo n.º 9
0
 private int BuildFieldDeclarations(MetaType type, IDictionary<object, string> memberNameToMappedName,
                                    StringBuilder sb)
 {
     int num = 0;
     foreach (MetaDataMember member in type.DataMembers)
     {
         string str;
         if ((!member.IsDeclaredBy(type) || member.IsAssociation) || !member.IsPersistent)
         {
             continue;
         }
         object key = InheritanceRules.DistinguishedMemberName(member.Member);
         if (memberNameToMappedName.TryGetValue(key, out str))
         {
             if (!(str == member.MappedName))
             {
                 goto Label_0075;
             }
             continue;
         }
         memberNameToMappedName.Add(key, member.MappedName);
     Label_0075:
         if (sb.Length > 0)
         {
             sb.Append(", ");
         }
         sb.AppendLine();
         sb.Append(string.Format(CultureInfo.InvariantCulture, "  {0} ",
                                 new object[] { SqlIdentifier.QuoteCompoundIdentifier(member.MappedName) }));
         if (!string.IsNullOrEmpty(member.Expression))
         {
             sb.Append("AS " + member.Expression);
         }
         else
         {
             sb.Append(GetDbType(member));
         }
         num++;
     }
     return num;
 }
Exemplo n.º 10
0
        private void Validate()
        {
            Dictionary <object, string> memberToColumn = new Dictionary <object, string>();

            foreach (MetaType type in this.InheritanceTypes)
            {
                // NB: Table node in XML can have only one Type node -- enforced by XSD

                foreach (MetaDataMember mem in type.PersistentDataMembers)
                {
                    if (mem.IsDeclaredBy(type))
                    {
                        if (mem.IsDiscriminator && !this.HasInheritance)
                        {
                            throw Error.NonInheritanceClassHasDiscriminator(type);
                        }
                        if (!mem.IsAssociation)
                        {
                            // validate that no database column is mapped twice
                            if (!string.IsNullOrEmpty(mem.MappedName))
                            {
                                string column;
                                object dn = InheritanceRules.DistinguishedMemberName(mem.Member);
                                if (memberToColumn.TryGetValue(dn, out column))
                                {
                                    if (column != mem.MappedName)
                                    {
                                        throw Error.MemberMappedMoreThanOnce(mem.Member.Name);
                                    }
                                }
                                else
                                {
                                    memberToColumn.Add(dn, mem.MappedName);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 11
0
        private void InitDataMembers()
        {
            if (this.dataMembers == null)
            {
                Dictionary <object, MetaDataMember> map = new Dictionary <object, MetaDataMember>();
                List <MetaDataMember> dMembers          = new List <MetaDataMember>();
                int          ordinal = 0;
                BindingFlags flags   = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;

                // Map of valid mapped names.
                Dictionary <string, MemberMapping> names = new Dictionary <string, MemberMapping>();
                Type currentType = this.type;
                for (TypeMapping tm = this.typeMapping; tm != null; tm = tm.BaseType)
                {
                    foreach (MemberMapping mmap in tm.Members)
                    {
                        names[mmap.MemberName + ":" + currentType.Name] = mmap;
                    }
                    currentType = currentType.BaseType;
                }

                HashSet <string> namesSeen = new HashSet <string>();               // Keep track of which names from the mapping file have been seen.
                FieldInfo[]      fis       = TypeSystem.GetAllFields(this.type, flags).ToArray();
                if (fis != null)
                {
                    foreach (FieldInfo fi in fis)
                    {
                        MemberMapping mmap;
                        string        name = fi.Name + ":" + fi.DeclaringType.Name;
                        if (names.TryGetValue(name, out mmap))
                        {
                            namesSeen.Add(name);
                            object         dn = InheritanceRules.DistinguishedMemberName(fi);
                            MetaDataMember mm;
                            if (!map.TryGetValue(dn, out mm))
                            {
                                mm = new MappedDataMember(this, fi, mmap, ordinal);
                                map.Add(InheritanceRules.DistinguishedMemberName(mm.Member), mm);
                                dMembers.Add(mm);
                                this.InitSpecialMember(mm);
                            }
                            ValidatePrimaryKeyMember(mm);
                            ordinal++;
                        }
                    }
                }

                PropertyInfo[] pis = TypeSystem.GetAllProperties(this.type, flags).ToArray();
                if (pis != null)
                {
                    foreach (PropertyInfo pi in pis)
                    {
                        MemberMapping mmap;
                        string        name = pi.Name + ":" + pi.DeclaringType.Name;
                        if (names.TryGetValue(name, out mmap))
                        {
                            namesSeen.Add(name);
                            MetaDataMember mm;
                            object         dn = InheritanceRules.DistinguishedMemberName(pi);
                            if (!map.TryGetValue(dn, out mm))
                            {
                                mm = new MappedDataMember(this, pi, mmap, ordinal);
                                map.Add(InheritanceRules.DistinguishedMemberName(mm.Member), mm);
                                dMembers.Add(mm);
                                this.InitSpecialMember(mm);
                            }
                            ValidatePrimaryKeyMember(mm);
                            ordinal++;
                        }
                    }
                }

                this.dataMembers   = dMembers.AsReadOnly();
                this.dataMemberMap = map;

                // Finally, make sure that all types in the mapping file were consumed.
                foreach (string name in namesSeen)
                {
                    names.Remove(name);
                }
                foreach (var orphan in names)
                {
                    Type aboveRoot = inheritanceRoot.Type.BaseType;
                    while (aboveRoot != null)
                    {
                        foreach (MemberInfo mi in aboveRoot.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                        {
                            if (String.Compare(mi.Name, orphan.Value.MemberName, StringComparison.Ordinal) == 0)
                            {
                                throw Error.MappedMemberHadNoCorrespondingMemberInType(orphan.Value.MemberName, type.Name);
                            }
                        }
                        aboveRoot = aboveRoot.BaseType;
                    }
                }
            }
        }
Exemplo n.º 12
0
 private void InitDataMembers()
 {
     if (dataMembers == null)
     {
         var dictionary           = new Dictionary <object, MetaDataMember>();
         var list                 = new List <MetaDataMember>();
         int ordinal              = 0;
         const BindingFlags flags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
         var  dictionary2         = new Dictionary <string, MemberMapping>();
         Type baseType            = type;
         for (TypeMapping mapping = this.typeMapping; mapping != null; mapping = mapping.BaseType)
         {
             foreach (MemberMapping mapping2 in mapping.Members)
             {
                 dictionary2[mapping2.MemberName + ":" + baseType.Name] = mapping2;
             }
             baseType = baseType.BaseType;
         }
         var         set       = new HashSet <string>();
         FieldInfo[] infoArray = TypeSystem.GetAllFields(this.type, flags).ToArray <FieldInfo>();
         if (infoArray != null)
         {
             foreach (FieldInfo info in infoArray)
             {
                 MemberMapping mapping3;
                 string        key = info.Name + ":" + info.DeclaringType.Name;
                 if (dictionary2.TryGetValue(key, out mapping3))
                 {
                     MetaDataMember member;
                     set.Add(key);
                     object obj2 = InheritanceRules.DistinguishedMemberName(info);
                     if (!dictionary.TryGetValue(obj2, out member))
                     {
                         member = new MappedDataMember(this, info, mapping3, ordinal);
                         dictionary.Add(InheritanceRules.DistinguishedMemberName(member.Member), member);
                         list.Add(member);
                         this.InitSpecialMember(member);
                     }
                     this.ValidatePrimaryKeyMember(member);
                     ordinal++;
                 }
             }
         }
         PropertyInfo[] infoArray2 = TypeSystem.GetAllProperties(this.type, flags).ToArray <PropertyInfo>();
         if (infoArray2 != null)
         {
             foreach (PropertyInfo info2 in infoArray2)
             {
                 MemberMapping mapping4;
                 string        str2 = info2.Name + ":" + info2.DeclaringType.Name;
                 if (dictionary2.TryGetValue(str2, out mapping4))
                 {
                     MetaDataMember member2;
                     set.Add(str2);
                     object obj3 = InheritanceRules.DistinguishedMemberName(info2);
                     if (!dictionary.TryGetValue(obj3, out member2))
                     {
                         member2 = new MappedDataMember(this, info2, mapping4, ordinal);
                         dictionary.Add(InheritanceRules.DistinguishedMemberName(member2.Member), member2);
                         list.Add(member2);
                         this.InitSpecialMember(member2);
                     }
                     this.ValidatePrimaryKeyMember(member2);
                     ordinal++;
                 }
             }
         }
         this.dataMembers   = list.AsReadOnly();
         this.dataMemberMap = dictionary;
         foreach (string str3 in set)
         {
             dictionary2.Remove(str3);
         }
         foreach (KeyValuePair <string, MemberMapping> pair in dictionary2)
         {
             for (Type type2 = this.inheritanceRoot.Type.BaseType; type2 != null; type2 = type2.BaseType)
             {
                 foreach (MemberInfo info3 in type2.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
                 {
                     if (string.Compare(info3.Name, pair.Value.MemberName, StringComparison.Ordinal) == 0)
                     {
                         throw Error.MappedMemberHadNoCorrespondingMemberInType(pair.Value.MemberName, this.type.Name);
                     }
                 }
             }
         }
     }
 }