internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
        {
            thisMember = member;

            isMany = TypeSystem.IsSequenceType(thisMember.Type);
            var otherEntityType = isMany ? TypeSystem.GetElementType(thisMember.Type) : thisMember.Type;

            otherType = thisMember.DeclaringType.Model.GetMetaType(otherEntityType);
            thisKey   = attr.ThisKey == null ?
                        thisMember.DeclaringType.IdentityMembers :
                        MakeKeys(thisMember.DeclaringType, attr.ThisKey);
            otherKey             = attr.OtherKey == null ? otherType.IdentityMembers : MakeKeys(otherType, attr.OtherKey);
            thisKeyIsPrimaryKey  = AreEqual(thisKey, thisMember.DeclaringType.IdentityMembers);
            otherKeyIsPrimaryKey = AreEqual(otherKey, otherType.IdentityMembers);
            isForeignKey         = attr.IsForeignKey;

            isUnique     = attr.IsUnique;
            deleteRule   = attr.DeleteRule;
            deleteOnNull = attr.DeleteOnNull;

            // if any key members are not nullable, the association is not nullable
            isNullable = true;
            foreach (var thisKeyMember in thisKey)
            {
                if (!thisKeyMember.CanBeNull)
                {
                    isNullable = false;
                    break;
                }
            }

            // validate DeleteOnNull specification
            if (deleteOnNull && (!isForeignKey || isMany || isNullable))
            {
                throw Error.InvalidDeleteOnNullSpecification(member);
            }

            if ((thisKey.Count != otherKey.Count) && (thisKey.Count > 0) && (otherKey.Count > 0))
            {
                throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
            }

            // determine reverse reference member
            foreach (var otherTypePersistentMember in otherType.PersistentDataMembers)
            {
                var otherTypeMemberAssociationAttribute = ((AttributedMetaModel)thisMember.DeclaringType.Model)
                                                          .TryGetAssociationAttribute(otherTypePersistentMember.Member);
                if ((otherTypeMemberAssociationAttribute != null) &&
                    (otherTypePersistentMember != thisMember) &&
                    (otherTypeMemberAssociationAttribute.Name == attr.Name))
                {
                    otherMember = otherTypePersistentMember;
                    break;
                }
            }

            //validate the number of ThisKey columns is the same as the number of OtherKey columns
        }
예제 #2
0
        private void InitDataMembers()
        {
            if (dataMembers == null)
            {
                dataMemberMap = new Dictionary <MetaPosition, MetaDataMember>();

                var ordinal = 0;
                const BindingFlags flags =
                    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;

                var fields = TypeSystem.GetAllFields(type, flags).ToArray();
                for (int i = 0, n = fields.Length; i < n; i++)
                {
                    var fi         = fields[i];
                    var dataMember = new AttributedMetaDataMember(this, fi, ordinal);
                    ValidatePrimaryKeyMember(dataMember);

                    // must be public or persistent
                    if (dataMember.IsPersistent || fi.IsPublic)
                    {
                        dataMemberMap.Add(new MetaPosition(fi), dataMember);
                        ordinal++;

                        // must be persistent for the rest
                        if (dataMember.IsPersistent)
                        {
                            InitSpecialMember(dataMember);
                        }
                    }
                }

                var properties = TypeSystem.GetAllProperties(type, flags).ToArray();
                for (int i = 0, n = properties.Length; i < n; i++)
                {
                    var property   = properties[i];
                    var dataMember = new AttributedMetaDataMember(this, property, ordinal);
                    ValidatePrimaryKeyMember(dataMember);

                    // must be public or persistent
                    var isPublic = (property.CanRead && property.GetGetMethod(false) != null) &&
                                   (!property.CanWrite || property.GetSetMethod(false) != null);
                    if (dataMember.IsPersistent || isPublic)
                    {
                        dataMemberMap.Add(new MetaPosition(property), dataMember);
                        ordinal++;

                        // must be persistent for the rest
                        if (dataMember.IsPersistent)
                        {
                            InitSpecialMember(dataMember);
                        }
                    }
                }

                dataMembers = new List <MetaDataMember>(dataMemberMap.Values).AsReadOnly();
            }
        }
예제 #3
0
        internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
        {
            this.thisMember = member;

            this.isMany = TypeSystem.IsSequenceType(this.thisMember.Type);
            Type ot = this.isMany ? TypeSystem.GetElementType(this.thisMember.Type) : this.thisMember.Type;

            this.otherType            = this.thisMember.DeclaringType.Model.GetMetaType(ot);
            this.thisKey              = (attr.ThisKey != null) ? MakeKeys(this.thisMember.DeclaringType, attr.ThisKey) : this.thisMember.DeclaringType.IdentityMembers;
            this.otherKey             = (attr.OtherKey != null) ? MakeKeys(otherType, attr.OtherKey) : this.otherType.IdentityMembers;
            this.thisKeyIsPrimaryKey  = AreEqual(this.thisKey, this.thisMember.DeclaringType.IdentityMembers);
            this.otherKeyIsPrimaryKey = AreEqual(this.otherKey, this.otherType.IdentityMembers);
            this.isForeignKey         = attr.IsForeignKey;

            this.isUnique     = attr.IsUnique;
            this.deleteRule   = attr.DeleteRule;
            this.deleteOnNull = attr.DeleteOnNull;

            // if any key members are not nullable, the association is not nullable
            foreach (MetaDataMember mm in thisKey)
            {
                if (!mm.CanBeNull)
                {
                    this.isNullable = false;
                    break;
                }
            }

            // validate DeleteOnNull specification
            if (deleteOnNull == true)
            {
                if (!(isForeignKey && !isMany && !isNullable))
                {
                    throw Error.InvalidDeleteOnNullSpecification(member);
                }
            }

            //validate the number of ThisKey columns is the same as the number of OtherKey columns
            if (this.thisKey.Count != this.otherKey.Count && this.thisKey.Count > 0 && this.otherKey.Count > 0)
            {
                throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
            }

            // determine reverse reference member
            foreach (MetaDataMember omm in this.otherType.PersistentDataMembers)
            {
                AssociationAttribute oattr = (AssociationAttribute)Attribute.GetCustomAttribute(omm.Member, typeof(AssociationAttribute));
                if (oattr != null)
                {
                    if (omm != this.thisMember && oattr.Name == attr.Name)
                    {
                        this.otherMember = omm;
                        break;
                    }
                }
            }
        }
		internal AttributedMetaAssociation(AttributedMetaDataMember member, AssociationAttribute attr)
		{
			this.thisMember = member;

			this.isMany = TypeSystem.IsSequenceType(this.thisMember.Type);
			Type ot = this.isMany ? TypeSystem.GetElementType(this.thisMember.Type) : this.thisMember.Type;
			this.otherType = this.thisMember.DeclaringType.Model.GetMetaType(ot);
			this.thisKey = (attr.ThisKey != null) ? MakeKeys(this.thisMember.DeclaringType, attr.ThisKey) : this.thisMember.DeclaringType.IdentityMembers;
			this.otherKey = (attr.OtherKey != null) ? MakeKeys(otherType, attr.OtherKey) : this.otherType.IdentityMembers;
			this.thisKeyIsPrimaryKey = AreEqual(this.thisKey, this.thisMember.DeclaringType.IdentityMembers);
			this.otherKeyIsPrimaryKey = AreEqual(this.otherKey, this.otherType.IdentityMembers);
			this.isForeignKey = attr.IsForeignKey;

			this.isUnique = attr.IsUnique;
			this.deleteRule = attr.DeleteRule;
			this.deleteOnNull = attr.DeleteOnNull;

			// if any key members are not nullable, the association is not nullable
			foreach(MetaDataMember mm in thisKey)
			{
				if(!mm.CanBeNull)
				{
					this.isNullable = false;
					break;
				}
			}

			// validate DeleteOnNull specification
			if(deleteOnNull == true)
			{
				if(!(isForeignKey && !isMany && !isNullable))
				{
					throw Error.InvalidDeleteOnNullSpecification(member);
				}
			}

			//validate the number of ThisKey columns is the same as the number of OtherKey columns
			if(this.thisKey.Count != this.otherKey.Count && this.thisKey.Count > 0 && this.otherKey.Count > 0)
			{
				throw Error.MismatchedThisKeyOtherKey(member.Name, member.DeclaringType.Name);
			}

			// determine reverse reference member
			foreach(MetaDataMember omm in this.otherType.PersistentDataMembers)
			{
				AssociationAttribute oattr = (AssociationAttribute)Attribute.GetCustomAttribute(omm.Member, typeof(AssociationAttribute));
				if(oattr != null)
				{
					if(omm != this.thisMember && oattr.Name == attr.Name)
					{
						this.otherMember = omm;
						break;
					}
				}
			}
		}
예제 #5
0
		private void InitDataMembers()
		{
			if(this.dataMembers == null)
			{
				this.dataMemberMap = new Dictionary<MetaPosition, MetaDataMember>();

				int ordinal = 0;
				BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;

				FieldInfo[] fis = TypeSystem.GetAllFields(this.type, flags).ToArray();
				if(fis != null)
				{
					for(int i = 0, n = fis.Length; i < n; i++)
					{
						FieldInfo fi = fis[i];
						MetaDataMember mm = new AttributedMetaDataMember(this, fi, ordinal);
						ValidatePrimaryKeyMember(mm);
						// must be public or persistent
						if(!mm.IsPersistent && !fi.IsPublic)
							continue;
						this.dataMemberMap.Add(new MetaPosition(fi), mm);
						ordinal++;
						// must be persistent for the rest
						if(!mm.IsPersistent)
							continue;
						this.InitSpecialMember(mm);
					}
				}

				PropertyInfo[] pis = TypeSystem.GetAllProperties(this.type, flags).ToArray();
				if(pis != null)
				{
					for(int i = 0, n = pis.Length; i < n; i++)
					{
						PropertyInfo pi = pis[i];
						MetaDataMember mm = new AttributedMetaDataMember(this, pi, ordinal);
						ValidatePrimaryKeyMember(mm);
						// must be public or persistent
						bool isPublic = (pi.CanRead && pi.GetGetMethod(false) != null)
										&& (!pi.CanWrite || pi.GetSetMethod(false) != null);
						if(!mm.IsPersistent && !isPublic)
							continue;
						this.dataMemberMap.Add(new MetaPosition(pi), mm);
						ordinal++;
						// must be persistent for the rest
						if(!mm.IsPersistent)
							continue;
						this.InitSpecialMember(mm);
					}
				}

				this.dataMembers = new List<MetaDataMember>(this.dataMemberMap.Values).AsReadOnly();
			}
		}
        private void InitDataMembers() {
            if (this.dataMembers == null) {
                this.dataMemberMap = new Dictionary<MetaPosition, MetaDataMember>();

                int ordinal = 0;
                BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;

                FieldInfo[] fis = TypeSystem.GetAllFields(this.type, flags).ToArray();
                if (fis != null) {
                    for (int i = 0, n = fis.Length; i < n; i++) {
                        FieldInfo fi = fis[i];
                        MetaDataMember mm = new AttributedMetaDataMember(this, fi, ordinal);
                        ValidatePrimaryKeyMember(mm);
                        // must be public or persistent
                        if (!mm.IsPersistent && !fi.IsPublic)
                            continue;
                        this.dataMemberMap.Add(new MetaPosition(fi), mm);
                        ordinal++;
                        // must be persistent for the rest
                        if (!mm.IsPersistent)
                            continue;
                        this.InitSpecialMember(mm);
                    }
                }
                
                PropertyInfo[] pis = TypeSystem.GetAllProperties(this.type, flags).ToArray();
                if (pis != null) {
                    for (int i = 0, n = pis.Length; i < n; i++) {
                        PropertyInfo pi = pis[i];
                        MetaDataMember mm = new AttributedMetaDataMember(this, pi, ordinal);
                        ValidatePrimaryKeyMember(mm);
                        // must be public or persistent
                        bool isPublic = (pi.CanRead && pi.GetGetMethod(false) != null)
                                        && (!pi.CanWrite || pi.GetSetMethod(false) != null);
                        if (!mm.IsPersistent && !isPublic)
                            continue;
                        this.dataMemberMap.Add(new MetaPosition(pi), mm);
                        ordinal++;
                        // must be persistent for the rest
                        if (!mm.IsPersistent)
                            continue;
                        this.InitSpecialMember(mm);
                    }
                }

                this.dataMembers = new List<MetaDataMember>(this.dataMemberMap.Values).AsReadOnly();
            }
        }