Esempio n. 1
0
        /// <summary>
        /// Maps a column of a reader to a type.
        /// </summary>
        /// <param name="type">The type being bound.</param>
        /// <param name="reader">The reader being bound.</param>
        /// <param name="column">The column to bind.</param>
        /// <param name="overrideMapping">An optional column mapper to override the operation</param>
        /// <returns>The mapping for the column</returns>
        private static FieldMapping MapColumn(Type type, IDataReader reader, int column, IColumnMapper overrideMapping)
        {
            string fieldName = null;

            // if an override mapping was used, then allow it the first go at the mapping
            if (overrideMapping != null)
            {
                fieldName = overrideMapping.MapColumn(type, reader, column);
            }

            // allow for custom mappings
            if (fieldName == null)
            {
                fieldName = Tables.Mappers.Select(m => m.MapColumn(type, reader, column)).FirstOrDefault();
            }

            // allow the first column to match the * wildcard on Guardian records
            if (fieldName == null)
            {
                var wildcards = ClassPropInfo.GetMembersForType(type).Where(m => m.ColumnName.StartsWith("*", StringComparison.OrdinalIgnoreCase)).OrderBy(m => m.ColumnName).ToList();
                if (column < wildcards.Count)
                {
                    fieldName = wildcards[column].Name;
                }
            }

            // by default, let all of the transforms transform the name, then search for it
            if (fieldName == null)
            {
                var columnName = reader.GetName(column);
                if (columnName != null)
                {
                    columnName = Tables.Transform(type, columnName);
                }
                fieldName = ClassPropInfo.SearchForMatchingField(type, columnName, Tables);
            }

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

            var member = ClassPropInfo.FindMember(type, fieldName);

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

            return(new FieldMapping(fieldName, member, DbSerializationRule.GetSerializer(reader, column, member)));
        }
Esempio n. 2
0
        /// <summary>
        /// Maps a parameters for a command.
        /// </summary>
        /// <param name="type">The type being bound.</param>
        /// <param name="command">The command being executed.</param>
        /// <param name="parameter">The parameter to bind.</param>
        /// <returns>The mapping for the parameters.</returns>
        private static FieldMapping MapParameter(Type type, IDbCommand command, IDataParameter parameter)
        {
            // allow for a custom mapper
            var fieldName = Parameters.Mappers.Select(m => m.MapParameter(type, command, parameter)).FirstOrDefault();

            // by default, let all of the transforms transform the name, then search for it
            if (fieldName == null)
            {
                var parameterName = Parameters.Transform(type, parameter.ParameterName);
                fieldName = ClassPropInfo.SearchForMatchingField(type, parameterName, Parameters);
            }

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

            var member = ClassPropInfo.FindMember(type, fieldName);

            return(new FieldMapping(fieldName, member, DbSerializationRule.GetSerializer(command, parameter, member)));
        }