internal static Expression <Func <IDataRecord, T> > Optimize(ReadOnlyCollection <string> fieldNames, Expression <Func <IDataRecord, T> > shaper) { EnitityUtility.CheckArgumentNotNull(fieldNames, "fieldNames"); EnitityUtility.CheckArgumentNotNull(shaper, "shaper"); OptimizingExpressionVisitor visitor = new OptimizingExpressionVisitor(fieldNames, shaper.Parameters.Single()); return((Expression <Func <IDataRecord, T> >)visitor.Visit(shaper)); }
private void InitializeShaper(IDataRecord record) { // Determine the layout of the record. if (null != fieldNames) { // If a record layout has already been established, make sure the current // record is compatible with it. ValidateFieldNames(record); } else { // Initialize a new shaper delegate within a lock (first one wins). lock (syncLock) { if (null != fieldNames) { // another thread beat us to it... ValidateFieldNames(record); } else { // if the user didn't provide an explicit shaper, generate a default shaper // based on the element type and the record layout. ReadOnlyCollection <string> recordFieldNames = GetFieldNames(record); Expression <Func <IDataRecord, T> > shaper = userSuppliedShaper ?? GetDefaultShaper(recordFieldNames); // optimize the expression Func <IDataRecord, T> compiledShaper = OptimizingExpressionVisitor .Optimize(recordFieldNames, shaper) .Compile(); // lock down the Materializer instance to use the (first encountered) field information and delegate fieldNames = recordFieldNames; shaperDelegate = compiledShaper; } } } }