internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo mi, int ordinal) { this.declaringType = mi.DeclaringType; this.metaType = metaType; this.member = mi; this.ordinal = ordinal; this.type = TypeSystem.GetMemberType(mi); this.isNullableType = TypeSystem.IsNullableType(this.type); this.attrColumn = (ColumnAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnAttribute)); this.attrAssoc = (AssociationAttribute)Attribute.GetCustomAttribute(mi, typeof(AssociationAttribute)); this.attr = (this.attrColumn != null) ? (DataAttribute)this.attrColumn : (DataAttribute)this.attrAssoc; if (this.attr != null && this.attr.Storage != null) { MemberInfo[] mis = mi.DeclaringType.GetMember(this.attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic); if (mis == null || mis.Length != 1) { throw Error.BadStorageProperty(this.attr.Storage, mi.DeclaringType, mi.Name); } this.storageMember = mis[0]; } Type storageType = this.storageMember != null?TypeSystem.GetMemberType(this.storageMember) : this.type; this.isDeferred = IsDeferredType(storageType); if (attrColumn != null && attrColumn.IsDbGenerated && attrColumn.IsPrimaryKey) { // auto-gen identities must be synced on insert if ((attrColumn.AutoSync != AutoSync.Default) && (attrColumn.AutoSync != AutoSync.OnInsert)) { throw Error.IncorrectAutoSyncSpecification(mi.Name); } } }
internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo member, int ordinal) { declaringType = member.DeclaringType; this.metaType = metaType; this.member = member; this.ordinal = ordinal; type = TypeSystem.GetMemberType(member); isNullableType = TypeSystem.IsNullableType(type); columnAttribute = ((AttributedMetaModel)metaType.Model).TryGetColumnAttribute(member); associationAttribute = ((AttributedMetaModel)metaType.Model).TryGetAssociationAttribute(member); var attr = (columnAttribute == null) ? associationAttribute : (DataAttribute)columnAttribute; if (attr != null && attr.Storage != null) { var mis = member.DeclaringType.GetMember(attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic); if (mis.Length != 1) { throw Error.BadStorageProperty(attr.Storage, member.DeclaringType, member.Name); } storageMember = mis[0]; } var storageType = storageMember == null ? type : TypeSystem.GetMemberType(storageMember); isDeferred = ((AttributedMetaModel)metaType.Model).IsDeferredMember(member, storageType, associationAttribute); // auto-gen identities must be synced on insert if ((columnAttribute != null) && columnAttribute.IsDbGenerated && columnAttribute.IsPrimaryKey && (columnAttribute.AutoSync != AutoSync.Default) && (columnAttribute.AutoSync != AutoSync.OnInsert)) { throw Error.IncorrectAutoSyncSpecification(member.Name); } }
internal AttributedMetaDataMember(AttributedMetaType metaType, MemberInfo mi, int ordinal) { this.declaringType = mi.DeclaringType; this.metaType = metaType; this.member = mi; this.ordinal = ordinal; this.type = TypeSystem.GetMemberType(mi); this.isNullableType = TypeSystem.IsNullableType(this.type); this.attrColumn = (ColumnAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnAttribute)); this.attrAssoc = (AssociationAttribute)Attribute.GetCustomAttribute(mi, typeof(AssociationAttribute)); this.attr = (this.attrColumn != null) ? (DataAttribute)this.attrColumn : (DataAttribute)this.attrAssoc; if(this.attr != null && this.attr.Storage != null) { MemberInfo[] mis = mi.DeclaringType.GetMember(this.attr.Storage, BindingFlags.Instance | BindingFlags.NonPublic); if(mis == null || mis.Length != 1) { throw Error.BadStorageProperty(this.attr.Storage, mi.DeclaringType, mi.Name); } this.storageMember = mis[0]; } Type storageType = this.storageMember != null ? TypeSystem.GetMemberType(this.storageMember) : this.type; this.isDeferred = IsDeferredType(storageType); if(attrColumn != null && attrColumn.IsDbGenerated && attrColumn.IsPrimaryKey) { // auto-gen identities must be synced on insert if((attrColumn.AutoSync != AutoSync.Default) && (attrColumn.AutoSync != AutoSync.OnInsert)) { throw Error.IncorrectAutoSyncSpecification(mi.Name); } } }
private AttributedMetaType CreateInheritedType(Type root, Type type) { MetaType metaType; if (!this.types.TryGetValue(type, out metaType)) { metaType = new AttributedMetaType(this.Model, this.Table, type, this); this.types.Add(type, metaType); if (type != root && type.BaseType != typeof(object)) { this.CreateInheritedType(root, type.BaseType); } } return((AttributedMetaType)metaType); }
private AttributedMetaType CreateInheritedType(Type root, Type type) { MetaType metaType; if (!this.types.TryGetValue(type, out metaType)) { metaType = new AttributedMetaType(this.Model, this.Table, type, this); this.types.Add(type, metaType); if (type != root && type.BaseType != typeof(object)) { this.CreateInheritedType(root, type.BaseType); } } return (AttributedMetaType)metaType; }
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(); }