protected virtual void InitList( Type viaType, params Type[] relationTypes ) { if( listType == GentleListType.StandAlone ) { broker.RetrieveList( containedMap.Type, this ); } else if( listType == GentleListType.OneToMany ) { // no relation objects for 1:n relationships viaInstances = null; mappings = containedMap.GetForeignKeyMappings( parentMap, true ); if( mappings.Count == 0 ) { mappings = viaMap.GetForeignKeyMappings( parentMap, false ); } Check.Verify( mappings.Count > 0, Error.DeveloperError, "The type {0} does not contain a foreign key reference to type {1}.", parentMap.Type, containedMap.Type ); Check.Verify( mappings.Count == 1, Error.NotImplemented, "GentleList for 1:n relations can not be used with composite keys." ); // populate self with any existing entries matching the current parent Key key = new Key( parentMap.Type, true ); IDictionaryEnumerator iterator = mappings.GetEnumerator(); while( iterator.MoveNext() ) { // construct a key to read the data; first obtain the referenced value from // the parent object (this is the constraint value used in the select) FieldMap fm = iterator.Value as FieldMap; object referencedValue = fm.GetValue( parent ); // if class references self make sure to pick the outgoing column if( containedMap.Type == parentMap.Type && ! fm.IsForeignKey ) { fm = iterator.Key as FieldMap; } key[ fm.MemberName ] = referencedValue; } broker.RetrieveList( containedMap.Type, key, this ); } else if( listType == GentleListType.ManyToMany ) { // create relation for n:m management Type[] relatedTypes = Merge( containedMap.Type, relationTypes ); viaInstances = new GentleRelation( broker, viaType, parent, relatedTypes ); // populate the list with any existing entries matching the current relation entries ObjectMap viaMap = ObjectFactory.GetMap( broker, viaType ); SqlBuilder sb = new SqlBuilder( broker, StatementType.Select, containedMap.Type ); // assume the relation object is the child, i.e. refers to the contained type mappings = viaMap.GetForeignKeyMappings( containedMap, true ); if( mappings.Count == 0 ) { mappings = viaMap.GetForeignKeyMappings( containedMap, false ); } Check.Verify( mappings.Count > 0, Error.DeveloperError, "The type {0} does not contain a foreign key reference to type {1}.", viaMap.Type, containedMap.Type ); Check.Verify( mappings.Count == 1, Error.NotImplemented, "GentleList for n:m relations can not be used with composite keys." ); // verify that references point to unique instance //Check.Verify( mappings.Count == parentMap.PrimaryKeyCount, Error.DeveloperError, // "The number of fields ({0}) referencing {1} from {2} must equal the primary key count ({3}).", // mappings.Count, parentMap.Type, containedMap.Type, parentMap.PrimaryKeyCount ); if( viaInstances.Count > 0 ) { foreach( FieldMap remote in mappings.Keys ) { FieldMap local = (FieldMap) mappings[ remote ]; // viaMap.GetForeignKeyFieldMap( containedMap.Type, local.PropertyName ); sb.AddConstraint( Operator.In, local.MemberName, viaInstances, remote.MemberName ); } ObjectFactory.GetCollection( containedMap.Type, sb.GetStatement( true ).Execute(), this ); } } }