public ConstructorMap( ObjectMap objectMap, ConstructorInfo constructorInfo, ColumnInfo columnInfo, object[] row ) { this.objectMap = objectMap; this.constructorInfo = constructorInfo; this.columnInfo = columnInfo; InitBitArrays( Math.Max( objectMap.Fields.Count, row.Length ) ); InitConstructorMap( row ); }
/// <summary> /// Method to scan for and determine the least expensive constructor for a given array of /// column names. /// </summary> /// <param name="columnNames">The column names of the result set</param> /// <param name="row">A sample row used to determine if type conversion is needed</param> /// <returns>A hash which can be used as constructor selector subsequently or 0 if no /// valid constructor could be found for the given columns.</returns> public int DetermineConstructor( string[] columnNames, object[] row ) { int hash = GetFieldComboHashCode( columnNames ); // make a quick exit if we've already processed this column combination if( constructorMaps.ContainsKey( hash ) ) { return hash; } else { if( ! columnInfos.ContainsKey( hash ) ) { columnInfos[ hash ] = new ColumnInfo( objectMap, columnNames ); } ConstructorMap best = null; foreach( ConstructorInfo ci in constructorInfos ) { ConstructorMap cm = new ConstructorMap( objectMap, ci, columnInfos[ hash ] as ColumnInfo, row ); if( cm.IsValid ) { if( best == null || cm.Cost < best.Cost ) { best = cm; } } } if( best != null ) { constructorMaps[ hash ] = best; // warn user that code has non-optimal performance if( best.Cost > 0 ) { Check.LogInfo( LogCategories.Metadata, "Best available constructor for type {0} is non-optimal (cost {1}).", objectMap.Type, best.Cost ); Check.LogInfo( LogCategories.Metadata, "See http://www.mertner.com/confluence/pages/viewpage.action?pageId=240 for additional information." ); } } else // no valid constructor found at all { StringBuilder sb = new StringBuilder(); for( int i = 0; i < columnNames.Length; i++ ) { sb.AppendFormat( "{0}{1}={2}", i % 3 != 0 ? ", " : Environment.NewLine, i, columnNames[ i ] ); } Check.Fail( Error.Unspecified, "No constructor found for type {0} using columns: {1}", objectMap.Type, sb.ToString() ); } return hash; } }