Ejemplo n.º 1
0
        /// <summary>
        /// Instantiates an entity and loads its mapped fields with the data from the reader.
        /// </summary>
        /// <param name="entityType">The entity being created and loaded.</param>
        /// <param name="mappings">The field mappings for the passed in entity.</param>
        /// <param name="reader">The open data reader.</param>
        /// <param name="useAltNames">Determines if the column AltName should be used.</param>
        /// <returns>Returns an entity loaded with data.</returns>
        public object CreateAndLoadEntity(Type entityType, ColumnMapCollection mappings, DbDataReader reader, bool useAltName)
        {
            // Create new entity
            object ent = _repos.ReflectionStrategy.CreateInstance(entityType);

            return(LoadExistingEntity(mappings, reader, ent, useAltName));
        }
Ejemplo n.º 2
0
            /// <summary>
            /// Provides a fluent way to manually add column mappings for the current entity..
            /// </summary>
            /// <param name="overwriteExistingMappings">
            /// Overwrites existing mappings if true, or appends if false.
            /// Default is true.
            /// </param>
            /// <returns></returns>
            public ColumnMapBuilder <TEntity> MapProperties(bool overwriteExistingMappings = true)
            {
                var repos = MapRepository.Instance;

                ColumnMapCollection columns = null;

                if (overwriteExistingMappings)
                {
                    columns = new ColumnMapCollection();
                    repos.Columns[_entityType] = columns;
                }
                else
                {
                    if (repos.Columns.ContainsKey(_entityType))
                    {
                        columns = repos.Columns[_entityType];
                    }
                    else
                    {
                        columns = new ColumnMapCollection();
                        repos.Columns[_entityType] = columns;
                    }
                }

                return(new ColumnMapBuilder <TEntity>(_fluentEntity, columns));
            }
Ejemplo n.º 3
0
        public object LoadExistingEntity(ColumnMapCollection mappings, DbDataReader reader, object ent, bool useAltName)
        {
            // Populate entity fields from data reader
            foreach (ColumnMap dataMap in mappings)
            {
                try
                {
                    string colName = dataMap.ColumnInfo.GetColumName(useAltName);
                    int    ordinal = reader.GetOrdinal(colName);
                    object dbValue = reader.GetValue(ordinal);

                    // Handle conversions
                    if (dataMap.Converter != null)
                    {
                        dbValue = dataMap.Converter.FromDB(dataMap, dbValue);
                    }

                    if (dbValue != DBNull.Value && dbValue != null)
                    {
                        dataMap.Setter(ent, dbValue);
                    }
                }
                catch (Exception ex)
                {
                    string msg = string.Format("The DataMapper was unable to load the following field: '{0}'. {1}",
                                               dataMap.ColumnInfo.Name, ex.Message);

                    throw new DataMappingException(msg, ex);
                }
            }

            PrepareLazyLoadedProperties(ent);

            return(ent);
        }
        /// <summary>
        /// Parses and orders the parameters from the query text.
        /// Filters the list of mapped columns to match the parameters found in the sql query.
        /// All parameters starting with the '@' or ':' symbol are matched and returned.
        /// </summary>
        /// <param name="command">The command and parameters that are being parsed.</param>
        /// <returns>A list of mapped columns that are present in the sql statement as parameters.</returns>
        public ColumnMapCollection OrderParameters(DbCommand command)
        {
            if (command.CommandType == CommandType.Text && this.Count > 0)
            {
                string commandTypeString = command.GetType().ToString();
                if (commandTypeString.Contains("Oracle") || commandTypeString.Contains("OleDb"))
                {
                    ColumnMapCollection columns = new ColumnMapCollection();

                    // Find all @Parameters contained in the sql statement
                    string paramPrefix = commandTypeString.Contains("Oracle") ? ":" : "@";
                    string regexString = string.Format(@"{0}[\w-]+", paramPrefix);
                    Regex  regex       = new Regex(regexString);
                    foreach (Match m in regex.Matches(command.CommandText))
                    {
                        ColumnMap matchingColumn = this.Find(c => string.Concat(paramPrefix, c.ColumnInfo.Name.ToLower()) == m.Value.ToLower());
                        if (matchingColumn != null)
                        {
                            columns.Add(matchingColumn);
                        }
                    }

                    return(columns);
                }
            }

            return(this);
        }
Ejemplo n.º 5
0
            /// <summary>
            /// Creates a ColumnMapBuilder that starts out with no pre-populated columns.
            /// All columns must be added manually using the builder.
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public ColumnMapBuilder <TEntity> MapProperties()
            {
                Type entityType             = typeof(TEntity);
                ColumnMapCollection columns = new ColumnMapCollection();

                MapRepository.Instance.Columns[entityType] = columns;
                return(new ColumnMapBuilder <TEntity>(_fluentEntity, columns));
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Creates a ColumnMapBuilder that starts out with no pre-populated columns.
        /// All columns must be added manually using the builder.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public ColumnMapBuilder <T> Columns <T>()
        {
            Type entityType             = typeof(T);
            ColumnMapCollection columns = new ColumnMapCollection();

            MapRepository.Instance.Columns[entityType] = columns;
            return(new ColumnMapBuilder <T>(null, columns));
        }
Ejemplo n.º 7
0
            /// <summary>
            /// Creates column mappings for the given type if they match the predicate.
            /// </summary>
            /// <typeparam name="T">The type that is being built.</typeparam>
            /// <param name="predicate">Determines whether a mapping should be created based on the member info.</param>
            /// <returns><see cref="ColumnMapConfigurator"/></returns>
            public ColumnMapBuilder <TEntity> AutoMapPropertiesWhere(Func <MemberInfo, bool> predicate)
            {
                ConventionMapStrategy strategy = new ConventionMapStrategy(_publicOnly);

                strategy.ColumnPredicate = predicate;
                ColumnMapCollection columns = strategy.MapColumns(_entityType);

                MapRepository.Instance.Columns[_entityType] = columns;
                return(new ColumnMapBuilder <TEntity>(_fluentEntity, columns));
            }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates column mappings for the given type if they match the predicate.
        /// </summary>
        /// <typeparam name="T">The type that is being built.</typeparam>
        /// <param name="predicate">Determines whether a mapping should be created based on the member info.</param>
        /// <returns><see cref="ColumnMapConfigurator"/></returns>
        public ColumnMapBuilder <T> BuildColumns <T>(Func <MemberInfo, bool> predicate)
        {
            Type entityType = typeof(T);
            ConventionMapStrategy strategy = new ConventionMapStrategy(_publicOnly);

            strategy.ColumnPredicate = predicate;
            ColumnMapCollection columns = strategy.MapColumns(entityType);

            MapRepository.Instance.Columns[entityType] = columns;
            return(new ColumnMapBuilder <T>(null, columns));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Creates all parameters for a SP based on the mappings of the entity,
        /// and assigns them values based on the field values of the entity.
        /// </summary>
        public void CreateParameters <T>(T entity, ColumnMapCollection columnMapCollection, bool isAutoQuery)
        {
            ColumnMapCollection mappings = columnMapCollection;

            if (!isAutoQuery)
            {
                // Order columns (applies to Oracle and OleDb only)
                mappings = columnMapCollection.OrderParameters(_db.Command);
            }

            foreach (ColumnMap columnMap in mappings)
            {
                if (columnMap.ColumnInfo.IsAutoIncrement || !columnMap.CanRead)
                {
                    continue;
                }

                var param = _db.Command.CreateParameter();
                param.ParameterName = columnMap.ColumnInfo.Name;
                param.Size          = columnMap.ColumnInfo.Size;
                param.Direction     = columnMap.ColumnInfo.ParamDirection;

                object val = columnMap.Getter(entity);

                param.Value = val ?? DBNull.Value;                 // Convert nulls to DBNulls

                // Handle data type conversions
                if (columnMap.Converter != null)
                {
                    param.Value = columnMap.Converter.ToDB(param.Value);
                }

                // Handle column conversions (specified during fluent mapping)
                if (columnMap.ToDB != null)
                {
                    param.Value = columnMap.ToDB(param.Value);
                }

                // Set the appropriate DbType property depending on the parameter type
                // Note: the columnMap.DBType property was set when the ColumnMap was created
                MapRepository.Instance.DbTypeBuilder.SetDbType(param, columnMap.DBType);

                _db.Command.Parameters.Add(param);
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Instantiates an entity and loads its mapped fields with the data from the reader.
 /// </summary>
 public object CreateAndLoadEntity <T>(ColumnMapCollection mappings, DbDataReader reader, bool useAltName)
 {
     return(CreateAndLoadEntity(typeof(T), mappings, reader, useAltName));
 }
Ejemplo n.º 11
0
 public ColumnMapBuilder(FluentMappings.MappingsFluentEntity <TEntity> fluentEntity, ColumnMapCollection mappedColumns)
 {
     _fluentEntity = fluentEntity;
     MappedColumns = mappedColumns;
 }