protected virtual void Load()
        {
            // global attributes
            var database = GetDatabaseAttribute();
            databaseName = database != null ? database.Name : null;

            // stored procedures
            metaFunctions = new Dictionary<MethodInfo, MetaFunction>();
            var functionAttributes = GetFunctionsAttributes();
            foreach (var functionPair in functionAttributes)
            {
                metaFunctions[functionPair.Key] = new AttributedMetaFunction(functionPair.Key, functionPair.Value);
            }

            // tables
            tables = new Dictionary<Type, MetaTable>();
            var tableAttributes = GetTablesAttributes();
            foreach (var tablePair in tableAttributes)
            {
                var type = new AttributedMetaType(tablePair.Key);
                var table = new AttributedMetaTable(tablePair.Value, type);
                tables[tablePair.Key] = table;
                type.SetMetaTable(table);
            }

            // reverse associations
            foreach (var table in GetTables())
            {
                foreach (var association in table.RowType.Associations)
                {
                    // we cast to call the SetOtherKey method
                    var attributedAssociation = association as AttributedMetaAssociation;
                    if (attributedAssociation != null)
                    {
                        var memberInfo = attributedAssociation.ThisMember.Member;
                        var associationAttribute = memberInfo.GetAttribute<AssociationAttribute>();
                        var memberType = memberInfo.GetMemberType();
                        Type otherTableType;
                        if (memberType.IsGenericType)
                            otherTableType = memberType.GetGenericArguments()[0];
                        else
                            otherTableType = memberType;
                        var otherTable = GetTable(otherTableType);
                        // then we lookup by the attribute if we have a match
                        MetaDataMember otherAssociationMember = null;
                        foreach (var member in otherTableType.GetMembers())
                        {
                            var otherAssociationAttribute = member.GetAttribute<AssociationAttribute>();
                            if (otherAssociationAttribute != null && otherAssociationAttribute.Name == associationAttribute.Name)
                            {
                                otherAssociationMember =
                                    (from a in otherTable.RowType.Associations
                                     where a.ThisMember.Member == member
                                     select a.ThisMember).SingleOrDefault();
                                if (otherAssociationMember == attributedAssociation.ThisMember)
                                {
                                    otherAssociationMember = null;
                                    continue;
                                }
                                break;
                            }
                        }
                        attributedAssociation.SetOtherKey(associationAttribute.OtherKey, table, otherTable, otherAssociationMember);
                    }
                }
            }
        }
Example #2
0
		/// <summary>
		/// Adds the table of the given type to the mappings.
		/// </summary>
		/// <remarks>
		/// The given type must have a <see cref="TableAttribute" /> to be mappable.
		/// </remarks>
		/// <param name="tableType">Type of the table.</param>
		/// <returns>
		/// Returns the <see cref="MetaTable"/> for the given table type or null if it is not mappable.
		/// </returns>
		private MetaTable AddTableType(Type tableType)
		{
			//No need to check base types because framework implementation doesn't do this either
			var tableAttribute = tableType.GetAttribute<TableAttribute>();

			if (tableAttribute == null)
			{
				return null;
			}

			//First set up the table without associations
			var metaType = new AttributedMetaType(tableType);
			var metaTable = new AttributedMetaTable(tableAttribute, metaType, this);
			metaType.SetMetaTable(metaTable);
			_Tables[tableType] = metaTable;

			return metaTable;
		}