Пример #1
0
        public IEnumerable <T> ExecuteReader <T>(object mapperKey, IDbConnection connection, string statment, params IDataParameter[] dataParameters)
            where T : new()
        {
            IDataReaderObjectMapper mapper = null;

            if (mapperKey == null)
            {
                mapperKey = statment;
            }

            IDbCommand  command = null;
            IDataReader reader  = null;

            try
            {
                command = CreateCommandImpl(statment, connection);

                OpenConnection(connection);
                PopulateParameterImpl(command, false, false, dataParameters);
                Logger.LogFormat(LevelEnum.Verbose, Resources.INFO_EXECUTE_READER, command.CommandText);
                reader = command.ExecuteReader();
                mapper = Mapper.Value.GetMapper <T>(mapperKey, reader);
            }
            catch (Exception ex)
            {
                string msg = string.Format(Resources.ERR_EXECUTING_EXECUTE, command.CommandText);
                Logger.Log(LevelEnum.Error, ex, msg);
                DataException dataException = new DataException(msg, ex);
                ex.Data["CommandText"]   = command.CommandText;
                ex.Data["ComandTimeout"] = command.CommandTimeout;
                ex.Data["Database"]      = connection.Database;
                ex.Data["Parameters"]    = ToString(dataParameters);
                throw dataException;
            }

            while (reader.Read())
            {
                T data = new T();
                mapper.Populate(ref data, reader);
                yield return(data);
            }

            Logger.LogFormat(LevelEnum.Verbose, Resources.INFO_EXECUTE_READER_END, command.CommandText);
            try
            {
                using (command) { }
            }
            catch (Exception ex)
            {
                string msg = string.Format(Resources.ERR_EXECUTING_EXECUTE, statment);
                Logger.Log(LevelEnum.Error, ex, msg);
                DataException dataException = new DataException(msg, ex);
                ex.Data["CommandText"]   = command.CommandText;
                ex.Data["ComandTimeout"] = command.CommandTimeout;
                ex.Data["Parameters"]    = ToString(dataParameters);
                throw dataException;
            }
        }
        /// <summary>
        /// Populate an object of Type T, using the statment and the data from the reader
        /// </summary>
        /// <typeparam name="T">Type of the object to map	</typeparam>
        /// <param name="data">Object to map</param>
        /// <param name="sqlStatement">procedure,direct statment used to get information</param>
        /// <param name="reader">DataReader to process</param>
        public void Populate <T>(ref T data, string sqlStatement, IDataReader reader)
        {
            IDataReaderObjectMapper mapper = null;

            try
            {
                mapper            = GetMapper <T>(sqlStatement, reader);
                mapper.FieldCount = reader.FieldCount;
            }
            catch (Exception ex)
            {
                Logger.LogFormat(LevelEnum.Warn, ex, Resources.ERR_MAPPING_GET, ex.Message);
                throw;
            }
            mapper.Populate(ref data, reader);
        }