Exemplo n.º 1
0
        internal MetaTable GetTableNoLocks(Type rowType)
        {
            MetaTable table;

            if (!this.metaTables.TryGetValue(rowType, out table))
            {
                Type             root  = GetRoot(rowType) ?? rowType;
                TableAttribute[] attrs = (TableAttribute[])root.GetCustomAttributes(typeof(TableAttribute), true);
                if (attrs.Length == 0)
                {
                    this.metaTables.Add(rowType, null);
                }
                else
                {
                    if (!this.metaTables.TryGetValue(root, out table))
                    {
                        table = new AttributedMetaTable(this, attrs[0], root);
                        foreach (MetaType mt in table.RowType.InheritanceTypes)
                        {
                            this.metaTables.Add(mt.Type, table);
                        }
                    }
                    // catch case of derived type that is not part of inheritance
                    if (table.RowType.GetInheritanceType(rowType) == null)
                    {
                        this.metaTables.Add(rowType, null);
                        return(null);
                    }
                }
            }
            return(table);
        }
Exemplo n.º 2
0
 internal MetaTable GetTableNoLocks(Type rowType)
 {
     if (!metaTables.TryGetValue(rowType, out MetaTable value))
     {
         Type             type  = GetRoot(rowType) ?? rowType;
         TableAttribute[] array = (TableAttribute[])type.GetCustomAttributes(typeof(TableAttribute), true);
         if (array.Length == 0)
         {
             metaTables.Add(rowType, null);
         }
         else
         {
             if (!metaTables.TryGetValue(type, out value))
             {
                 value = new AttributedMetaTable(this, array[0], type);
                 foreach (MetaType inheritanceType in value.RowType.InheritanceTypes)
                 {
                     metaTables.Add(inheritanceType.Type, value);
                 }
             }
             if (value.RowType.GetInheritanceType(rowType) == null)
             {
                 metaTables.Add(rowType, null);
                 return(null);
             }
         }
     }
     return(value);
 }
        internal MetaTable GetTableNoLocks(Type rowType)
        {
            MetaTable table;

            if (metaTables.TryGetValue(rowType, out table))
            {
                return(table);
            }

            var root  = GetRoot(rowType) ?? rowType;
            var attrs = GetTableAttributes(root, true);

            if (!attrs.Any())
            {
                metaTables.Add(rowType, null);
                return(null);
            }

            if (!metaTables.TryGetValue(root, out table))
            {
                table = new AttributedMetaTable(this, attrs.First(), root);
                foreach (var inheritanceType in table.RowType.InheritanceTypes)
                {
                    metaTables.Add(inheritanceType.Type, table);
                }
            }

            // catch case of derived type that is not part of inheritance
            if (table.RowType.GetInheritanceType(rowType) == null)
            {
                metaTables.Add(rowType, null);
                return(null);
            }

            return(table);
        }
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null) {

            // check for inheritance and create all other types
            InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (inheritanceInfo.Length > 0) {
                if (this.Discriminator == null) {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type)) {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary<Type, MetaType>();
                this.types.Add(type, this); // add self
                this.codeMap = new Dictionary<object, MetaType>();

                // initialize inheritance types
                foreach (InheritanceMappingAttribute attr in inheritanceInfo) {
                    if (!type.IsAssignableFrom(attr.Type)) {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type);
                    }
                    if (attr.Type.IsAbstract) {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type);
                    }
                    AttributedMetaType mt = this.CreateInheritedType(type, attr.Type);
                    if (attr.Code == null) {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (mt.inheritanceCode != null) {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type);
                    }
                    object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type);                
                    foreach (object d in codeMap.Keys) {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 &&
                            d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) ||
                            object.Equals(d, codeValue)) {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    mt.inheritanceCode = codeValue;
                    this.codeMap.Add(codeValue, mt);
                    if (attr.IsDefault) {
                        if (this.inheritanceDefault != null) {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = mt;
                    }
                }

                if (this.inheritanceDefault == null) {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            if (this.types != null) {
                this.inheritanceTypes = this.types.Values.ToList().AsReadOnly();
            }
            else {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }
            this.Validate();
        }
 internal MetaTable GetTableNoLocks(Type rowType) {
     MetaTable table;
     if (!this.metaTables.TryGetValue(rowType, out table)) {
         Type root = GetRoot(rowType) ?? rowType;
         TableAttribute[] attrs = (TableAttribute[])root.GetCustomAttributes(typeof(TableAttribute), true);
         if (attrs.Length == 0) {
             this.metaTables.Add(rowType, null);
         }
         else {
             if (!this.metaTables.TryGetValue(root, out table)) {
                 table = new AttributedMetaTable(this, attrs[0], root);
                 foreach (MetaType mt in table.RowType.InheritanceTypes) {
                     this.metaTables.Add(mt.Type, table);
                 }
             }
             // catch case of derived type that is not part of inheritance
             if (table.RowType.GetInheritanceType(rowType) == null) {
                 this.metaTables.Add(rowType, null);
                 return null;
             }
         }
     }
     return table;
 }
Exemplo n.º 6
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            InheritanceMappingAttribute[] inheritanceInfo = (InheritanceMappingAttribute[])type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
            if (inheritanceInfo.Length > 0)
            {
                if (this.Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(this.Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(this.Discriminator.DeclaringType.Name, this.Discriminator.Name, this.Discriminator.Type);
                }
                this.types = new Dictionary <Type, MetaType>();
                this.types.Add(type, this);                 // add self
                this.codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (InheritanceMappingAttribute attr in inheritanceInfo)
                {
                    if (!type.IsAssignableFrom(attr.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(attr.Type, type);
                    }
                    if (attr.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(attr.Type);
                    }
                    AttributedMetaType mt = this.CreateInheritedType(type, attr.Type);
                    if (attr.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (mt.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(attr.Type);
                    }
                    object codeValue = DBConvert.ChangeType(attr.Code, this.Discriminator.Type);
                    foreach (object d in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue.GetType() == typeof(string) && ((string)codeValue).Trim().Length == 0 &&
                             d.GetType() == typeof(string) && ((string)d).Trim().Length == 0) ||
                            object.Equals(d, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    mt.inheritanceCode = codeValue;
                    this.codeMap.Add(codeValue, mt);
                    if (attr.IsDefault)
                    {
                        if (this.inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        this.inheritanceDefault = mt;
                    }
                }

                if (this.inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            if (this.types != null)
            {
                this.inheritanceTypes = this.types.Values.ToList().AsReadOnly();
            }
            else
            {
                this.inheritanceTypes = new MetaType[] { this }.ToList().AsReadOnly();
            }
            this.Validate();
        }
Exemplo n.º 7
0
 internal virtual AttributedRootType CreateRootType(AttributedMetaTable table, Type type)
 {
     return(new AttributedRootType(this, table, type));
 }
Exemplo n.º 8
0
        internal AttributedRootType(AttributedMetaModel model, AttributedMetaTable table, Type type)
            : base(model, table, type, null)
        {
            // check for inheritance and create all other types
            var inheritanceAttributes = GetInheritanceMappingAttributes(type, model);

            if (inheritanceAttributes.Count > 0)
            {
                if (Discriminator == null)
                {
                    throw Error.NoDiscriminatorFound(type);
                }
                if (!MappingSystem.IsSupportedDiscriminatorType(Discriminator.Type))
                {
                    throw Error.DiscriminatorClrTypeNotSupported(
                              Discriminator.DeclaringType.Name,
                              Discriminator.Name,
                              Discriminator.Type);
                }

                types = new Dictionary <Type, MetaType>();
                types.Add(type, this);                 // add self
                var codeMap = new Dictionary <object, MetaType>();

                // initialize inheritance types
                foreach (var inheritanceAttribute in inheritanceAttributes)
                {
                    if (!type.IsAssignableFrom(inheritanceAttribute.Type))
                    {
                        throw Error.InheritanceTypeDoesNotDeriveFromRoot(inheritanceAttribute.Type, type);
                    }
                    if (inheritanceAttribute.Type.IsAbstract)
                    {
                        throw Error.AbstractClassAssignInheritanceDiscriminator(inheritanceAttribute.Type);
                    }

                    var inheritedType = CreateInheritedType(type, inheritanceAttribute.Type);
                    if (inheritanceAttribute.Code == null)
                    {
                        throw Error.InheritanceCodeMayNotBeNull();
                    }
                    if (inheritedType.inheritanceCode != null)
                    {
                        throw Error.InheritanceTypeHasMultipleDiscriminators(inheritanceAttribute.Type);
                    }

                    var codeValue = DBConvert.ChangeType(inheritanceAttribute.Code, Discriminator.Type);
                    foreach (var codeMapKey in codeMap.Keys)
                    {
                        // if the keys are equal, or if they are both strings containing only spaces
                        // they are considered equal
                        if ((codeValue is string &&
                             ((string)codeValue).Trim().Length == 0 &&
                             codeMapKey is string &&
                             ((string)codeMapKey).Trim().Length == 0) ||
                            Equals(codeMapKey, codeValue))
                        {
                            throw Error.InheritanceCodeUsedForMultipleTypes(codeValue);
                        }
                    }
                    inheritedType.inheritanceCode = codeValue;
                    codeMap.Add(codeValue, inheritedType);

                    if (inheritanceAttribute.IsDefault)
                    {
                        if (inheritanceDefault != null)
                        {
                            throw Error.InheritanceTypeHasMultipleDefaults(type);
                        }
                        inheritanceDefault = inheritedType;
                    }
                }

                if (inheritanceDefault == null)
                {
                    throw Error.InheritanceHierarchyDoesNotDefineDefault(type);
                }
            }

            inheritanceTypes = types == null ?
                               new MetaType[]
            {
                this
            }
            .ToList()
            .AsReadOnly() :
            types.Values.ToList().AsReadOnly();

            Validate();
        }